package ex1.database;

import de.schlichtherle.truezip.file.TFile;
import ex1.database.WepsDatabasesProvider.WepsDatabaseInformation;
import java.awt.EventQueue;
import java.util.LinkedList;
import java.util.List;
import javax.swing.Icon;
import javax.swing.ImageIcon;

import usda.weru.util.Util;

/**
 *
 * @author Joseph Levin <joelevin@weru.ksu.edu>
 */
public class WepsDatabasesProvider implements DatabaseProvider<Database, WepsDatabaseInformation> {

    @Override
    public Database getDatabase(WepsDatabaseInformation info) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public WepsDatabaseInformation[] getDatabases() {
/**
                 * Note:  Assertions are not enabled.  These will be useless items
                 * unless assertions are enabled.  Thus, they will be commented out unless
                 * the user wishes to enable specific assertions (feed the virtual machine 
                 * the -ea argument).
                 */
//        assert !EventQueue.isDispatchThread();

        List<WepsDatabaseInformation> infos = new LinkedList<WepsDatabaseInformation>();

        //environment variable
        String envDBPath = System.getenv("WEPS_DATABASES");
        if (envDBPath != null && envDBPath.trim().length() > 0) {
            TFile dir = new TFile(envDBPath);
            harvest(dir, infos, "Environment Variable");
        }

        //if windows then we attempt to figure out where the databases are
        if (Util.isWindows()) {
            //2, %progamdata%\weps\databases
            String programdataPath = System.getenv("programdata");
            if (programdataPath != null && programdataPath.trim().length() > 0) {
                TFile dir = new TFile(programdataPath, "USDA\\WEPS\\Databases");
                harvest(dir, infos, "Program Data");
            }

            //3. %allusersprofile%\Application Data\weps\databases\,
            String allusersprofilePath = System.getenv("allusersprofile");
            if (allusersprofilePath != null && allusersprofilePath.trim().length() > 0) {
                TFile dir = new TFile(allusersprofilePath, "Application Data\\USDA\\WEPS\\Databases");
                harvest(dir, infos, "All Users\\Application Data");
            }
        }

        //current working directory
        TFile db = new TFile("db");
        if (isDatabase(db)) {
            infos.add(new WepsDatabaseInformation(db, "Install Path"));
        }

        return infos.toArray(new WepsDatabaseInformation[infos.size()]);
    }

    private void harvest(TFile dir, List<WepsDatabaseInformation> infos, String source) {
        TFile[] children = dir.listFiles(dir.getArchiveDetector());
        if (children != null) {
            for (TFile db : children) {

                if (isDatabase(dir)) {
                    WepsDatabaseInformation info = new WepsDatabaseInformation(db, source);
                    if (!infos.contains(info)) {
                        infos.add(info);
                    }
                }
            }

        }
    }

    private boolean isDatabase(TFile dir) {
        //TODO: Add more critical testing if the directory is a database.  Ex: don't include any version control files
        return dir.isDirectory() && !dir.isHidden();
    }

    /**
     *
     */
    protected static class WepsDatabaseInformation extends FileDatabaseInformation {

        private final String c_shortDescription;

        /**
         *
         * @param file
         * @param shortDescription
         */
        public WepsDatabaseInformation(TFile file, String shortDescription) {
            super(file);
            c_shortDescription = shortDescription;
        }

        /**
         *
         * @return
         */
        @Override
        public String getShortDescription() {
            return c_shortDescription;
        }

        @Override
        public Icon getIcon() {
            return new ImageIcon(WepsDatabasesProvider.class.getResource("/ex1/database/database.png")); // NOI18N
        }
    }
}
