package org.mbari.seepsearch.test;

import java.awt.Dimension;
import java.awt.Rectangle;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

import javax.swing.JProgressBar;

import org.mbari.io.DelimitedFile;
import org.mbari.seepsearch.core.RovAltitudeFilter;
import org.mbari.seepsearch.grid.AsciiNullGridReader;
import org.mbari.seepsearch.grid.AsciiRasterGridWriter;
import org.mbari.seepsearch.grid.GridException;
import org.mbari.seepsearch.grid.RasterGrid;
import org.mbari.swing.FileProgressFrame;

public class GetDiveNumbers {

    public GetDiveNumbers() {
        try {
            createGrid();
        } catch (IOException e) {

        } catch (GridException e) {

        }
    }

    public static void main(String[] args) {
        GetDiveNumbers getDiveNumbers1 = new GetDiveNumbers();
        System.exit(-1);
    }

    private void createGrid() throws IOException, GridException {

        final boolean isUtm = false;
        int zoneNumber = -1;
        final String inGrid = "c:/home/brian/montereyc.asc";
        final String rovDataPath = "s:/brian/projects/200010/rovdata/";
        final String outPath = "s:/brian/projects/200010/rovdata/grids/ascii";
        final String outPostFix = "_lt100m";

        // Read the input file
        File input = new File(inGrid);
        AsciiNullGridReader reader = new AsciiNullGridReader(input);
        reader.read();
        RasterGrid grid = reader.getGrid();

        // Set the filterparamters
        RovAltitudeFilter filter = new RovAltitudeFilter();
        filter.setMaxAltitude(100D);
        filter.setMinAltitude(Double.NaN);


        for (int i = 1989; i < 2002; i++) {

            // Get the output file
            //File output = new File(outPath, "rov" + i + outPostFix + ".asc");
            File output = new File(outPath, "rovAll" + outPostFix + ".asc");
            File dir1 = new File(rovDataPath, i + "");
            File dirVnta = new File(dir1, "vnta");
            File dirTibr = new File(dir1, "tibr");

            File[] filesVnta = null;
            if (dirVnta.exists()) {
                filesVnta = dirVnta.listFiles();
            }

            File[] filesTibr = null;
            if (dirTibr.exists()) {
                filesTibr = dirTibr.listFiles();
            }

            File[] navFiles = null;
            if ((filesVnta != null) & (filesTibr != null)) {
                navFiles = new File[filesVnta.length + filesTibr.length];
                System.arraycopy(filesVnta, 0, navFiles, 0, filesVnta.length);
                System.arraycopy(filesTibr, 0, navFiles, filesVnta.length, filesTibr.length);
            } else if ((filesVnta == null) & (filesTibr != null)) {
                navFiles = filesTibr;
            } else if ((filesVnta != null) & (filesTibr == null)) {
                navFiles = filesVnta;
            }

            FileProgressFrame pf = new FileProgressFrame();
            JProgressBar pb = pf.getJProgressBar();
            pb.setStringPainted(true);
            pb.setMinimum(0);
            pb.setMaximum(navFiles.length + 1);
            pb.setValue(0);
            pf.pack();
            pf.setVisible(true);
            Dimension d;
            Rectangle rect;

            for (int j = 0; j < navFiles.length; j++) {
                pb.setValue(j);
                pb.setString(navFiles[j].getName());
                d = pb.getSize();
                rect = new Rectangle(0, 0, d.width, d.height);
                pb.paintImmediately(rect);
//                GridUtil.PopulateGeoGridWithGeoNavData(grid, navFiles[j], filter,
//                    zoneNumber);
                checkFile(navFiles[j]);
            }

            pb.setValue(navFiles.length);
            pb.setString("Writing " + output.getName());
            d = pb.getSize();
            rect = new Rectangle(0, 0, d.width, d.height);
            pb.paintImmediately(rect);

            if (i == 2001) {
                AsciiRasterGridWriter writer = new AsciiRasterGridWriter(output);
                writer.write(grid, 0, 7);
            }
            pb.setValue(navFiles.length + 1);
            pb.setString("");
            pb = null;
            pf.dispose();
            pf = null;
        }

    }

    private void checkFile(File navFile) throws IOException {

        // Read the nav file
        final DelimitedFile reader = new DelimitedFile(navFile);
        reader.setDelimiter(",");
        reader.setHasColumnNames(true);
        reader.read();
        final double[][] nav = reader.getData();

        // Locate the lat and lon columns
        final String[] columnNames = reader.getColumnNames();
        final List columnList = Arrays.asList(columnNames);
        final int bathyColumn = columnList.indexOf("BenthicZ");
        final int latColumn = columnList.indexOf("Lat");
        final int lonColumn = columnList.indexOf("Lon");
        final int altiColumn = columnList.indexOf("Alti2");
        if ((latColumn == -1) || (lonColumn == -1) || (altiColumn == -1)) {
            //System.err.println("Not an ROV navigation file...skipping");
            return;
        }

        boolean depthCheck = false;
        int bathyZ = 0;
        int alti2 = 0;

        for (int i = 0; i < reader.getNumOfRows(); i++) {
            if ((nav[i][bathyColumn] <= -1000) && (nav[i][altiColumn] <= 50)) {
                depthCheck = true;
            }
            if (depthCheck) {
                // Get dive number and write it out
                System.out.println(reader.getFile().getName());

                break; // break out of the loope
            }
        }

    }
}