package vismon.gui;

import info.clearthought.layout.TableLayout;
import info.clearthought.layout.TableLayoutConstants;
import vismon.util.Constants;

import javax.swing.*;
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 ContourShadingChooser {

    public static int cpShadingColorIndex = 0;

    private JFrame frame;

    private JPanel mainPanel;
    private JPanel northPanel;
    private JPanel southPanel;

    private ButtonGroup buttonGroup;
    private JRadioButton[] colorPaletteRadioButtons;
    private JPanel[] colorPalettePanels;

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

    protected void setUp() {
        buttonGroup = new ButtonGroup();
        colorPaletteRadioButtons = new JRadioButton[Constants.shadingNum];
        for (int i = 0; i < colorPaletteRadioButtons.length; ++i) {
            colorPaletteRadioButtons[i] = new JRadioButton("");
            buttonGroup.add(colorPaletteRadioButtons[i]);
        }
        colorPaletteRadioButtons[cpShadingColorIndex].setSelected(true);

        colorPalettePanels = new JPanel[Constants.shadingNum];
        for (int i = 0; i < colorPalettePanels.length; ++i) {
            colorPalettePanels[i] = new PalettePanel(i);
            colorPalettePanels[i].setBorder(BorderFactory.createLineBorder(Color.GRAY, 2));
        }

        initComponents();
    }

    protected void start() {
        addPanel();

        frame.setVisible(true);
    }

    protected void initComponents() {
        int width = 400;
        int height = 400;

        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("Contour Plot Shading - Color Chooser");
        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) {
                close();
            }
        });

        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.9, 30}};
        mainPanel.setLayout(new TableLayout(mainLayout));

        addNorthPanel();
        addSouthPanel();

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

    protected void addNorthPanel() {
        northPanel = new JPanel();

        double[][] northLayout = new double[][]{{30, 20, TableLayoutConstants.FILL, 20},
                {20, 30, 20, 30, 20, 30, 20, 30, 20, 30, 20, 30}};
        northPanel.setLayout(new TableLayout(northLayout));

        for (int i = 0; i < colorPalettePanels.length; ++i) {
            northPanel.add(colorPaletteRadioButtons[i], "0," + (2 * i + 1));
            northPanel.add(colorPalettePanels[i], "2," + (2 * i + 1));
        }

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

    protected void addSouthPanel() {
        southPanel = new JPanel();
        //southPanel.setBackground(Color.WHITE);
        double[][] southLayout = new double[][]{{140, TableLayoutConstants.FILL, 80},
                {TableLayoutConstants.FILL}};
        southPanel.setLayout(new TableLayout(southLayout));

        JButton setAsDefaultButton = new JButton("Set as Default");
        southPanel.add(setAsDefaultButton, "0,0");

        JButton okButton = new JButton("OK");
        okButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent actionEvent) {
                for (int i = 0; i < colorPaletteRadioButtons.length; ++i)
                    if (colorPaletteRadioButtons[i].isSelected()) {
                        cpShadingColorIndex = i;
                        break;
                    }
                close();
                // MUST DO TO SEE THE SELECTED COLOUR SHADING THE OPTIONS PANEL
                Vismon.getVismonInstance().repaintOptionsPanel();
            }
        });
        southPanel.add(okButton, "2,0");

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

    private void close() {
        frame.setVisible(false);
        frame.dispose();
    }

    private class PalettePanel extends JPanel {

        private int shadingIndex;

        public PalettePanel(int index) {
            super();
            shadingIndex = index;
        }

        public int getShadingIndex() {
            return shadingIndex;
        }

        public void setShadingIndex(int shadingIndex) {
            this.shadingIndex = shadingIndex;
            repaint();
        }

        @Override
        public void paint(Graphics g) {
            super.paint(g);
        }

        @Override
        protected void paintComponent(Graphics g) {
            myPaint(g);
        }

        public void myPaint(Graphics g) {

            int width = this.getWidth();
            int height = this.getHeight();

            int binNum = 8;
            int binWidth = (int) (1.0 * width / binNum);

            for (int j = 0; j < binNum - 1; ++j) {
                g.setColor(Constants.getCPShadingColor(shadingIndex, j));
                g.fillRect(j * binWidth, 0, binWidth, height);
            }

            g.setColor(Constants.getCPShadingColor(shadingIndex, binNum - 1));
            g.fillRect((binNum - 1) * binWidth, 0, width - (binNum - 1) * binWidth, height);
        }

        @Override
        public String toString() {
            return "shadingIndex = " + shadingIndex;
        }
    }

}
