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 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 Mosaik_new mosaik;
    private String source;
    private File databaseDirectory;
    private Configuration configuration;

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

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



    }

    ChooseDatabase(JDialog parent, boolean modal, String source, Mosaik_new mosaik) {
        super(parent, modal);
        this.source = source;
        configuration = (Configuration) 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
        // Schlieï¿½t das Fenster
        if (evt.getActionCommand().equals("CancelSelection")) {
            this.setVisible(false);
            this.dispose();
        }
        // Fï¿½hrt den Inmport durch
        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;
                mosaik.currentdatabase = MosaikFunctions.loadDatabase(databaseDirectory);
                mosaik.enableComponents2(true);

                this.setVisible(false);
                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;

        }
    }
}
