package vismon.gui;

import info.clearthought.layout.TableLayout;
import info.clearthought.layout.TableLayoutConstants;
import say.swing.JFontChooser;
import vismon.data.Session;
import vismon.util.Constants;
import vismon.util.Util;

import javax.swing.*;
import javax.swing.border.TitledBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class Preferences {

    private JFrame frame;

    private JPanel mainPanel;
    private JPanel appearancePanel;
    private JPanel plotsPanel;
    private JPanel buttonPanel;
    private JTextField probTextField;

    protected void run() {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                setUp();
                start();
            }
        });
    }

    protected void setUp() {
        initComponents();
    }

    protected void start() {
        addPanel();

        frame.setVisible(true);
    }

    protected void initComponents() {
        int width = 500;
        int height = 300;

        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        int xLocation = (int)((screenSize.getWidth() - width) * 0.5);
        int yLocation = (int)((screenSize.getHeight() - height) * 0.5);

        this.frame = new JFrame("Preferences");
        this.frame.setSize(new Dimension(width, height));
        this.frame.setLocation(xLocation, yLocation);
        this.frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        this.frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent windowEvent) {
                Vismon.getVismonInstance().setPreferences();
            }
        });

        this.frame.getContentPane().setLayout(new TableLayout(new double[][]{{0, -1, 0}, {0, -1, 0}}));
    }

    protected void addPanel() {
        mainPanel = new JPanel();

        double[][] mainLayout = new double[][]{{TableLayoutConstants.FILL},
                {0.45, 0.45, 30}};
        mainPanel.setLayout(new TableLayout(mainLayout));

        addAppearancePanel();
        addPlotsPanel();
        addButtonPanel();

        Container frameContainer = this.frame.getContentPane();
        frameContainer.setLayout(new BorderLayout());
        frameContainer.add(mainPanel, BorderLayout.CENTER);
    }

    protected void addAppearancePanel() {
        appearancePanel = new JPanel();

        TitledBorder appearanceBorder = BorderFactory.createTitledBorder("Appearance");
        appearanceBorder.setTitleJustification(TitledBorder.LEFT);
        appearancePanel.setBorder(appearanceBorder);

        double[][] appearanceLayout = new double[][]{{0.2, TableLayoutConstants.FILL, 0.2},
                {30, 30}};
        appearancePanel.setLayout(new TableLayout(appearanceLayout));

        JLabel plotFontLabel = new JLabel("Plot font:");
        appearancePanel.add(plotFontLabel, "0,0");

        final JTextField plotFontTextField = new JTextField(Util.fontToString(Constants.PLOT_FONT));
        plotFontTextField.setFont(Constants.PLOT_FONT);
        plotFontTextField.setHorizontalAlignment(SwingConstants.CENTER);
        plotFontTextField.setEditable(false);
        plotFontTextField.setSize((int)(appearancePanel.getWidth() * 0.5), plotFontTextField.getHeight());
        appearancePanel.add(plotFontTextField, "1,0");

        JButton plotChangeButton = new JButton("Change");
        plotChangeButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent actionEvent) {
                JFontChooser chooser = new JFontChooser();
                chooser.setSelectedFont(Constants.PLOT_FONT);
                chooser.setVisible(true);
                if (chooser.showDialog(chooser.getParent()) == JFontChooser.OK_OPTION ) {
                    Font f = chooser.getSelectedFont();
                    Constants.setPlotFont(f);
                    plotFontTextField.setText(Util.fontToString(Constants.PLOT_FONT));
                    plotFontTextField.setFont(Constants.PLOT_FONT);
                }
            }
        });
        appearancePanel.add(plotChangeButton, "2,0");


        JLabel overviewFontLabel = new JLabel("Overview font:");
        appearancePanel.add(overviewFontLabel, "0,1");

        final JTextField overviewFontTextField = new JTextField(Util.fontToString(Constants.OVERVIEW_FONT));
        overviewFontTextField.setFont(Constants.OVERVIEW_FONT);
        overviewFontTextField.setHorizontalAlignment(SwingConstants.CENTER);
        overviewFontTextField.setEditable(false);
        appearancePanel.add(overviewFontTextField, "1,1");

        JButton overviewChangeButton = new JButton("Change");
        overviewChangeButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent actionEvent) {
                JFontChooser chooser = new JFontChooser();
                chooser.setSelectedFont(Constants.OVERVIEW_FONT);
                chooser.setVisible(true);
                if (chooser.showDialog(chooser.getParent()) == JFontChooser.OK_OPTION ) {
                    Font f = chooser.getSelectedFont();
                    Constants.setOverviewFont(f);
                    overviewFontTextField.setText(Util.fontToString(f));
                    overviewFontTextField.setFont(f);
                }
            }
        });
        appearancePanel.add(overviewChangeButton, "2,1");

        mainPanel.add(appearancePanel, "0,0");
    }

    protected void addPlotsPanel() {
        plotsPanel = new JPanel();

        TitledBorder plotsBorder = BorderFactory.createTitledBorder("Plots");
        plotsBorder.setTitleJustification(TitledBorder.LEFT);
        plotsPanel.setBorder(plotsBorder);

        double[][] plotsLayout = new double[][]{{0.45, 0.2, 0.05},
                {30}};
        plotsPanel.setLayout(new TableLayout(plotsLayout));

        JLabel probLabel = new JLabel("Probability interval percentage: ");
        plotsPanel.add(probLabel, "0,0");

        probTextField = new JTextField(Session.getErrorBarPercentage() + "");
        probTextField.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent actionEvent) {
                Session.setErrorBarPercentage(Integer.parseInt(probTextField.getText()));
                Vismon.getVismonInstance().getTradeoffsPanel().updateUncertaintyOptions();
            }
        });
        plotsPanel.add(probTextField, "1,0");

        JLabel percentageLabel = new JLabel(" %");
        plotsPanel.add(percentageLabel, "2,0");

        mainPanel.add(plotsPanel, "0,1");
    }

    protected void addButtonPanel() {
        buttonPanel = new JPanel();

        double[][] buttonLayout = new double[][]{{0.4, 0.2, 0.4},
                {30}};
        buttonPanel.setLayout(new TableLayout(buttonLayout));

        JButton closeButton = new JButton("Close");
        closeButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent actionEvent) {
                close();
            }
        });
        buttonPanel.add(closeButton, "1,0");

        mainPanel.add(buttonPanel, "0,2");

    }

    protected void close() {
        Session.setErrorBarPercentage(Integer.parseInt(probTextField.getText()));

        frame.setVisible(false);
        frame.dispose();
    }

}
