package org.mbari.seepsearch.tools;

import java.awt.Dimension;
import java.awt.Rectangle;
import java.io.File;
import java.io.IOException;

import javax.swing.JProgressBar;

import org.mbari.swing.FileProgressFrame;

/**
 * <p>Provides access to all ROV navigation files. To implement a process on
 * all ROV nav files, implement the internalProcess method.</p>
 *
 * <p>The Seep Search project is a sub-project of the Continental
 * Margin Fluids and Gases project (200010). It involves the analysis of
 * archived ROV data to obtain understanding of seep distritution in Monterey
 * Bay.</p>
 *
 * <p>The Purpose of this Java package is to intercompare ROV Navigation files,
 * VIMS data on seep community observations and the EM-300 bathymetry. We are
 * looking for locations where the ROV navigation files and the EM-300 data
 * have similar depths between the 2 data sets, which implies that the ROV is
 * observing the sea floor.</p><hr><br>
 *
 * <p>MBARI provides this documentation and code &quot;as is&quot;, with no
 * warranty, express or implied, of its quality or consistency. It is provided
 * without support and without obligation on the part of the Monterey Bay
 * Aquarium Research Institute to assist in its use, correction, modification,
 * or enhancement. This information should not be published or distributed to
 * third parties without specific written permission from MBARI.</p><br>
 *
 * Copyright 2002 MBARI.<br>
 * MBARI Proprietary Information. All rights reserved.<br><hr><br>
 *
 * $Log: RovNavFileProcess.java,v $
 * Revision 1.2  2002/08/30 21:45:31  brian
 * Minor modifications
 *
 * Revision 1.1  2002/02/25 16:40:09  brian
 * Modified tools to ignore eastings and northings columns.
 *
 * Revision 1.2  2002/02/09 00:07:19  brian
 * Working tools to calculate ROV altitude. There's a glitch that occurs for a few files. Need to look into this
 *<br><hr><br>
 *
 * @author  : $Author: brian $
 * @version : $Revision: 1.2 $
 */
public abstract class RovNavFileProcess {

    /**
     * Constructor
     * @param rovDataPath the root of the rov directory tree.
     */
    public RovNavFileProcess(String rovDataPath) {
        this.rovDataPath = new File(rovDataPath).getAbsolutePath();
    }

    /**
     * Default constructor.
     * Uses the defualt rovDatapath of "s:/brian/projects/200010/rovdata/"
     */
    public RovNavFileProcess() {
        rovDataPath = "/Tornado/TempNav/2006/nta";
    }

    /**
     * Processes all ROV navigation file. To specify the exact process, implement
     * the <code>internalProcess</code> method.
     * @throws IOException
     */
    public final void process() throws IOException {

        // Loop through all the rov nav files
        for (int i = 1989; i < 2010; i++) {

            // Setup the paths to the rov nav directories
            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();
            }

            // Concaenate the contents of the tibr and vnta directories into
            // A single array
            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;
            } else {
                continue; // If no files are found skip to next loop interation
            }

            // Show the progress graphically
            progressBar.setStringPainted(true);
            progressBar.setMinimum(0);
            progressBar.setMaximum(navFiles.length);
            progressBar.setValue(0);
            progressFrame.pack();
            progressFrame.setVisible(true);
            Dimension d;
            Rectangle rect;

            for (int j = 0; j < navFiles.length; j++) {
                progressBar.setValue(j);
                progressBar.setString(navFiles[j].getName());
                d = progressBar.getSize();
                rect = new Rectangle(0, 0, d.width, d.height);
                progressBar.paintImmediately(rect);
                /** @todo Implement internal process in a subclass */
                internalProcess(navFiles[j]);
            }

            progressBar.setValue(navFiles.length);
            d = progressBar.getSize();
            rect = new Rectangle(0, 0, d.width, d.height);
            progressBar.paintImmediately(rect);
            progressFrame.setVisible(false);
        }

    }

    /**
     * Get the path to the ROV directory root. This is the direcotry that
     * contains all the year directories. Each year directory should in turn contain
     * tibr and/or vnta directories.
     * @return The root of the rov data path
     */
    public String getRovDataPath() {
        return rovDataPath;
    }

    /**
     * Sets the root of the ROV data path
     * @param rovDataPath The root of the ROV data path
     */
    public void setRovDataPath(String rovDataPath) {
        this.rovDataPath = rovDataPath;
    }

    /**
     * This is called in the loop of the process method. Every nav file is passed
     * to this method. It must be overridden in subclasses.
     * @param navFile
     */
    public abstract void internalProcess(File navFile);

    /**
     * The path to root of the ROV nav directory tree
     */
    String rovDataPath;

    /**
     * Frame use for monitoring progress
     */
    FileProgressFrame progressFrame = new FileProgressFrame();

    /**
     * The progress bar contained in the FileProgressFrame
     */
    JProgressBar progressBar = progressFrame.getJProgressBar();

}