package tmcsim.simulationmanager.dialogs;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.UIManager;

import tmcsim.simulationmanager.SimulationManagerView;
import timeselector.*;

/**
 * GotoTimeIndexDialog is a dialog used to prompt the user for the
 * simulation time that the simulation will be reset to.  
 * When the dialog is closed, the gotoApplied flag is set to true 
 * if the user has chosen a time to seek to.  Otherwise, the flag is false.
 *
 * @author Matthew Cechini
 * Modifed to use timeselector by jdalbey 3/24/2019
 */
@SuppressWarnings("serial")
public class GotoTimeIndexDialog extends JDialog {
    
    /** 
     *  Boolean flag to designate whether the user has chosen to goto
     *  a new position in the simulation.
     */
    public boolean gotoApplied = false;

    /**
     * Constructor.
     *
     * @param simulationManagerView View object to attach this dialog to.
     * @param initialValue Long value (in seconds) of time to initialize spinner with. 
     */
    public GotoTimeIndexDialog(SimulationManagerView simulationManagerView, long initialValue) {
        super(simulationManagerView, "Goto CAD Time Index", true);
        
        initComponents();   

        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        setLocation(screenSize.width/2 - getWidth()/2, screenSize.height/2 - getHeight()/2);
        
        setSize(new Dimension(250, 150));
        setResizable(false);
        setModal(true);
        setVisible(true);
            
    }   
    
    /**
     * Initialize GUI Swing elements and listeners.
     */
    private void initComponents() {
        
        addWindowListener(new WindowListener() {
            public void windowClosed(WindowEvent e)  {}
            public void windowOpened(WindowEvent e)  {}            
            public void windowIconified(WindowEvent e)  {}         
            public void windowDeiconified(WindowEvent e)  {}    
            public void windowActivated(WindowEvent e)  {}                             
            public void windowDeactivated(WindowEvent e)  {}         
            public void windowClosing(WindowEvent e)  {
                gotoApplied = false;                
            }    
        });     
        
        gotoLabel = new JLabel("Choose the new simulation time:");
        gotoLabel.setAlignmentX(Box.CENTER_ALIGNMENT);  

        okButton = new JButton("OK");
        okButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                gotoApplied = true;
                setVisible(false);  
            }
            
        });
        
        cancelButton = new JButton("Cancel");        
        cancelButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                gotoApplied = false;
                setVisible(false);  
            }           
        });
        
        fldTimeSelection = new TimeSelectionField(false);
        
        buttonBox = new Box(BoxLayout.X_AXIS);
        buttonBox.add(Box.createHorizontalStrut(20));
        buttonBox.add(Box.createHorizontalGlue());
        buttonBox.add(okButton);
        buttonBox.add(Box.createHorizontalStrut(20));
        buttonBox.add(cancelButton);
        buttonBox.add(Box.createHorizontalGlue());
        buttonBox.add(Box.createHorizontalStrut(20));   
        buttonBox.setAlignmentX(Box.CENTER_ALIGNMENT);  
              
        gotoIndexBox = new Box(BoxLayout.Y_AXIS);
        gotoIndexBox.add(Box.createVerticalStrut(10));
        gotoIndexBox.add(Box.createVerticalGlue());
        gotoIndexBox.add(gotoLabel);
        gotoIndexBox.add(Box.createVerticalStrut(10));
        gotoIndexBox.add(fldTimeSelection);
        gotoIndexBox.add(Box.createVerticalStrut(10));
        gotoIndexBox.add(buttonBox);    
        gotoIndexBox.add(Box.createVerticalGlue());   
        gotoIndexBox.add(Box.createVerticalStrut(10)); 
        
        getContentPane().add(gotoIndexBox);
        pack();     
    }       
    
    /**
     * Accessor to the String representation of user selected time. The text
     * field contains a String representation of the time in a format produced
     * by SimpleDateFormat pattern "h:mm:ss", e.g., "3:26:00"
     *
     * @return String containing the selected time.
     */
    public String getGotoTime() 
    {
        String result = fldTimeSelection.getText();
        return result.substring(1, result.length());
    }   
    
    /** Local main for unit testing */
    public static void main(String[] args)
    {
        try
        {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception ex)
        {
            ex.printStackTrace();
        }

        GotoTimeIndexDialog dlg = new GotoTimeIndexDialog(null,0);
        System.out.println(dlg.getGotoTime());
    }
    
    private JLabel gotoLabel;
    
    private JButton cancelButton;;
    private JButton okButton;

    private Box gotoIndexBox;   
    private Box buttonBox;  
    
    private TimeSelectionField fldTimeSelection;
}
