/*
 * UploadDialog.java
 *
 * Created on 30-jun-2009, 16:52:31
 */
package uk.ac.reload.editor.upload;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.awt.event.WindowStateListener;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;
import uk.ac.reload.diva.prefs.UserPrefs;
import uk.ac.reload.dweezil.gui.ErrorDialogBox;
import uk.ac.reload.editor.EditorFrame;
import uk.ac.reload.editor.IIcons;
import uk.ac.reload.editor.Messages;
import uk.ac.reload.editor.prefs.EditorPrefs;
import uk.ac.reload.editor.prefs.Upload_PrefsPanel;

/**
 *
 * @author Gonzalo
 */
public class UploadDialog extends JDialog implements IIcons {

    /**
     * The Upload prefs panel
     */
    private Upload_PrefsPanel _uploadPrefsPanel;
    /**
     * The Upload progress panel
     */
    private UploadProgressPanel _uploadProgressPanel;
    /**
     * The User Preferences
     */
    private UserPrefs _userPrefs;
    JTabbedPane jTabbedPane;

    private File courseFolderFile;

    private static UploadDialog _instance;

    /**
     *
     * @return Static instance
     */
    public static UploadDialog getSharedInstance(File courseFolderFile) {
        if (_instance == null) {
            System.out.println("nuevo Upload Dialog");
            _instance = new UploadDialog();
        }
        _instance.courseFolderFile = courseFolderFile;
        System.out.println(_instance.courseFolderFile.getAbsolutePath());
        return _instance;
    }

    /** Creates new form UploadDialog */
    public UploadDialog() {
        super(EditorFrame.getInstance(), "Upload", true);

        //initComponents();

        setSize(580, 370);
        setLocationRelativeTo(EditorFrame.getInstance());
        setResizable(false);

        //Make some panels
        _uploadPrefsPanel = new Upload_PrefsPanel();
        _uploadProgressPanel = new UploadProgressPanel();

        // Get the User Prefs from file
        _userPrefs = EditorPrefs.getInstance();

        jTabbedPane = new JTabbedPane();

        jTabbedPane.add("Progress", _uploadProgressPanel);
        jTabbedPane.add("Upload Preferences", _uploadPrefsPanel);
        getContentPane().add(jTabbedPane, BorderLayout.CENTER);

        // Button Panel
        JPanel buttonPanel = new JPanel();
        buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));

        // Save Button
        JButton btnSave = new JButton("Save");
        btnSave.addActionListener(new SaveClick());
        buttonPanel.add(btnSave);
        
        // Close Button
        JButton btnClose = new JButton("Close"); //$NON-NLS-1$
        btnClose.addActionListener(new CloseClick());
        getRootPane().setDefaultButton(btnClose);
        buttonPanel.add(btnClose);

        // Add bottom Panel
        JPanel bottomPanel = new JPanel(new BorderLayout());
        bottomPanel.add(buttonPanel, BorderLayout.EAST);
        getContentPane().add(bottomPanel, BorderLayout.SOUTH);

        switch(getDefaultCloseOperation()) {
            case WindowConstants.DISPOSE_ON_CLOSE:
                System.out.println("DISPOSE_ON_CLOSE");
            case WindowConstants.DO_NOTHING_ON_CLOSE:
                System.out.println("DO_NOTHING_ON_CLOSE");
            case WindowConstants.EXIT_ON_CLOSE:
                System.out.println("EXIT_ON_CLOSE");
            case WindowConstants.HIDE_ON_CLOSE:
                System.out.println("HIDE_ON_CLOSE");
        }
        setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        
        addWindowListener(new WindowListener() {

            public void windowOpened(WindowEvent e) {
                
            }

            public void windowClosing(WindowEvent e) {
                System.out.println("Cerrando Ventana");
                _uploadProgressPanel.reset();
            }

            public void windowClosed(WindowEvent e) {
                System.out.println("Se cerró la ventana");
            }

            public void windowIconified(WindowEvent e) {
                System.out.println("#######");
            }

            public void windowDeiconified(WindowEvent e) {
                
            }

            public void windowActivated(WindowEvent e) {
                
            }

            public void windowDeactivated(WindowEvent e) {
                
            }
        });

    }

    public UploadDialog(javax.swing.JFrame parent, boolean modal) {
        super(parent, modal);

        //initComponents();

        setSize(580, 370);
        setLocationRelativeTo(EditorFrame.getInstance());
        setResizable(false);

        //Make some panels
        _uploadPrefsPanel = new Upload_PrefsPanel();
        _uploadProgressPanel = new UploadProgressPanel();

        //Tabbed pane
        jTabbedPane = new JTabbedPane();

        jTabbedPane.add("Progress", _uploadProgressPanel);
        jTabbedPane.add("Upload Preferences", _uploadPrefsPanel);
        getContentPane().add(jTabbedPane, BorderLayout.CENTER);

        // Button Panel
        JPanel buttonPanel = new JPanel();
        buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));

        // Save Button
        JButton btnSave = new JButton("Save");
        btnSave.addActionListener(new SaveClick());
        buttonPanel.add(btnSave);
        btnSave.setEnabled(false);

        // Close Button
        JButton btnClose = new JButton("Close"); //$NON-NLS-1$
        btnClose.addActionListener(new CloseClick());
        getRootPane().setDefaultButton(btnClose);
        buttonPanel.add(btnClose);

        // Add bottom Panel
        JPanel bottomPanel = new JPanel(new BorderLayout());
        bottomPanel.add(buttonPanel, BorderLayout.EAST);
        getContentPane().add(bottomPanel, BorderLayout.SOUTH);
    }

    public void showDialog() {
        /*SwingUtilities.invokeLater(new Runnable() {
        public void run() {
        //Update the panels
        _uploadPrefsPanel.setFields(_userPrefs);
        System.out.println("Preferencias actualizadas");
        }
        });*/
        _uploadProgressPanel.setCourseFolder(courseFolderFile);
        _uploadPrefsPanel.setFields(_userPrefs);
        System.out.println("Vamos a invocar setVisible(true)");
        setVisible(true);
        System.out.println("Ya hemos invocado setVisible");
    }

    /**
     * User Pressed Save
     */
    protected void saveConfig() {
        //Update the user prefs
        _uploadPrefsPanel.saveToUserPrefs(_userPrefs);
        try {
            _userPrefs.save();
        } catch (IOException ex) {
            ex.printStackTrace();
            ErrorDialogBox.showWarning(this,
                    Messages.getString("uk.ac.reload.editor.prefs.PrefsDialog.7") + ": ", //$NON-NLS-1$ //$NON-NLS-2$
                    Messages.getString("uk.ac.reload.editor.prefs.PrefsDialog.0"), //$NON-NLS-1$
                    ex);
        }
    }

    /**
     * Save handler
     */
    private class SaveClick extends AbstractAction {

        public void actionPerformed(ActionEvent e) {
            saveConfig();
        }

    }

    /**
     * Close handler
     */
    private class CloseClick extends AbstractAction {

        public void actionPerformed(ActionEvent e) {
            _uploadProgressPanel.reset();
            dispose();
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 530, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 376, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                UploadDialog dialog = new UploadDialog(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables
}
