/**
 * 
         -------- Copyright (C) [2012]  [Kristofer SCHWEIGER & Niklas FLORIAN1]---------

    This program is free software; you can redistribute it and/or modify it under the terms of the
 *  GNU General Public License as published by the Free Software Foundation; either version 2 of the License,
 *  or (at your option) any later version.
    This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 *  without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 *  See the GNU General Public License for more details.
 * 
    You should have received a copy of the GNU General Public License along with this program; if not, 
 *  write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA

 * 
 * For any licensing issues please contact us under following adress: Mosaik.Projekt.2012@gmail.com
 */

package gui;

import java.awt.BorderLayout;
import java.io.File;

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.JDialog;
import javax.swing.JFileChooser;

import dataBase.TimedAction;
import mosaik.MosaikFunctions;

public class ChooseDatabase extends javax.swing.JDialog {

    {
        // Set Look & Feel
        try {
            javax.swing.UIManager.setLookAndFeel(javax.swing.UIManager.getSystemLookAndFeelClassName());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    private javax.swing.JFileChooser jFileChooser1;
    private MosaikApp mosaik;
    private String source;
    private File databaseDirectory;
    private Configurations configuration;

    public ChooseDatabase(java.awt.Frame parent, boolean modal, String source) {

        super(parent, modal);
        mosaik = (MosaikApp) parent;
        this.source = source;
        initGUI();



    }

    ChooseDatabase(JDialog parent, boolean modal, String source, MosaikApp mosaik) {
        super(parent, modal);
        this.source = source;
        configuration = (Configurations) parent;
        this.mosaik = mosaik;
        initGUI();



    }

    private void initGUI() {

        try {
            {
                this.setTitle("Oeffnen");
                jFileChooser1 = new JFileChooser();
                getContentPane().add(jFileChooser1, BorderLayout.CENTER);

                jFileChooser1.removeChoosableFileFilter(jFileChooser1.getFileFilter());
                jFileChooser1.addChoosableFileFilter(new chooseNewDatabaseFilter());
                jFileChooser1.setFileSelectionMode(JFileChooser.FILES_ONLY);
                jFileChooser1.setMultiSelectionEnabled(false);

                jFileChooser1.addActionListener(new java.awt.event.ActionListener() {

                    public void actionPerformed(
                            java.awt.event.ActionEvent evt) {
                        try {
                            jFileChooser1ActionPerformed(evt);
                        } catch (IOException ex) {
                            Logger.getLogger(ChooseDatabase.class.getName()).log(Level.SEVERE, null, ex);
                        } catch (ClassNotFoundException ex) {
                            Logger.getLogger(ChooseDatabase.class.getName()).log(Level.SEVERE, null, ex);
                        }
                    }
                });

            }
            this.setSize(676, 423);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void jFileChooser1ActionPerformed(java.awt.event.ActionEvent evt) throws IOException, ClassNotFoundException {// GEN-FIRST:event_jFileChooser1ActionPerformed
       
        if (evt.getActionCommand().equals("CancelSelection")) {
            this.setVisible(false);
            this.dispose();
        }
       
        if (evt.getActionCommand().equals("ApproveSelection")) {

            databaseDirectory = jFileChooser1.getSelectedFile();
            System.out.print(databaseDirectory);
            System.out.print(source);


            if (source.equals("chooseDatabase")) {
                System.out.print(databaseDirectory);
                mosaik.databaseDirectory = databaseDirectory;
                
                Thread t = new TimedAction(0) {
					
					@Override
					public void action() throws Exception {
		                mosaik.currentdatabase = MosaikFunctions.loadDatabase(databaseDirectory);
		                mosaik.enableComponents2(true);
					}
				};

                this.setVisible(false);
                try {t.join();} catch (InterruptedException e) {}
                this.dispose();

            } else if (source.equals("chooseFristDatabase")) {
                configuration.jTextField1.setText(databaseDirectory.getAbsolutePath());
                configuration.firstDatabaseDirectory = databaseDirectory;
                System.out.print("daserste");
                this.setVisible(false);
                this.dispose();

            } else if (source.equals("chooseSecondDatabase")) {
                configuration.jTextField2.setText(databaseDirectory.getAbsolutePath());
                configuration.secondDatabaseDirectory = databaseDirectory;

                System.out.print("das zweite");
                this.setVisible(false);
                this.dispose();

            } else if (source.equals("chooseDatabaseToMergeWithDirectory")) {
                configuration.jTextField3.setText(databaseDirectory.getAbsolutePath());
                configuration.existingDatabaseToMergeWithDirectoryOfPictures = databaseDirectory;

                System.out.print("chooseDatabaseToMergeWithDirectory");
                this.setVisible(false);
                this.dispose();

            }
        }// GEN-LAST:event_jFileChooser1ActionPerformed
    }

    /**
     * Filter fï¿½r den Import
     *
     * @author Niklas F.
     */
    class chooseNewDatabaseFilter extends javax.swing.filechooser.FileFilter {

        /**
         * @param f
         */
        @Override
        public boolean accept(File f) {
            if (f.isDirectory()) {
                return true;
            }
            String name = f.getName();
            int i = name.lastIndexOf('.');

            if (i > 0 && i < name.length() - 1) {
                String type = name.substring(i + 1).toLowerCase();		// shorter ;)


                if (type.equals("db")) {
                    return true;
                }

            }
            return false;
        }

        /**
         * @return
         */
        public String getDescription() {

            String ret = ".db";

            return ret;

        }
    }
}
