package view;

import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class OptionsDialog extends JDialog {

    private static final long serialVersionUID = 1L;

    private ArrayList< ActionListener > listeners;
    private JComboBox<String> display;
    private JButton speichern;
    private JComboBox<String> run;
    private JTextField fSize;
    private int tempFSize = 12;
    
    private static class Options
    {
        public int layoutDisplaySelection = 0;
        public int runStepsSelection = 0;
        public int fontSize = 12;
    }
    
    private Options options;
    
    OptionsDialog()
    {
        setTitle( "Preferences" );
        setLayout( new GridLayout( 4, 2 ) );
        add( new JLabel( "Display layouts:" ) );
        String[] displayValues = { "All", "Only current" };
        display = new JComboBox<String>( displayValues );
        add( display );
        add( new JLabel( "Run steps:" ) );
        String[] runValues = { "All", "Only expanded" };
        run = new JComboBox<String>( runValues );
        add( run );
        add( new JLabel( "Font size:" ) );
        fSize = new JTextField( "12" );
        add( fSize );
        fSize.getDocument().addDocumentListener( new NumberDocumentListener( new NumberDocumentListener.Action() {
            @Override
            public void action(int val) {
                tempFSize = val;
            }
        }, fSize ) );
        add( new JLabel() );
        speichern = new JButton( "Ok" );
        speichern.addActionListener( new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    boolean change = options.layoutDisplaySelection != display.getSelectedIndex() ||
                            options.runStepsSelection != run.getSelectedIndex() ||
                            tempFSize != options.fontSize;
                    options.layoutDisplaySelection = display.getSelectedIndex();
                    options.runStepsSelection = run.getSelectedIndex();
                    options.fontSize = tempFSize;
                    if( change )
                    {
                        for( ActionListener l : listeners )
                        {
                            l.actionPerformed( new ActionEvent(this, 0, "Options changed" ) );
                        }
                    }
                    setVisible( false );
                } catch( Exception e1 )
                {
                    e1.printStackTrace();
                }
            }
            
        });
        add( speichern );
        setSize( 250, 150 );
        listeners = new ArrayList<ActionListener>();
        options = new Options();
        setLocation( Toolkit.getDefaultToolkit().getScreenSize().width / 2 - getWidth() / 2, Toolkit.getDefaultToolkit().getScreenSize().height / 2 - getHeight() / 2 );
    }
    
    public int getLayerDisplayOption()
    {
        return options.layoutDisplaySelection;
    }
    
    public int getRunStepsOption()
    {
        return options.runStepsSelection;
    }
    
    public int getFontSize()
    {
        return options.fontSize;
    }
    
    public void addActionListener( ActionListener al )
    {
        listeners.add( al );
    }
}
