package GUIs;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;

import Division.*;
import Common.*;

/**
 * Author: Max Buegler - max@sarstech.com
 * Date: Oct 14, 2009
 * Time: 5:14:29 PM
 */
public class DivisionGUI extends JFrame {
    private JTextField pl;
    private JTextField money;
    private JTextField[] claims;
    private DivisionCalculator algorithm;
    private JTextField algo;
    private JButton startbutton;
    private JPanel algoPanel;
    private JPanel claimPanel;
    private JComboBox algolist;


    public DivisionGUI(){
        super("WEGA GUI");
        //super.setSize(700,300);
        super.setLayout(new BorderLayout());
        JPanel topPanel=new JPanel();
        JLabel p1=new JLabel("Number of claims:");
        pl=new JTextField("3",2);

        JLabel mn=new JLabel("Money to divide:");
        money=new JTextField("100",5);
        startbutton=new JButton("Start");
        startbutton.addActionListener(new StartListener(this));
        topPanel.add(p1);
        topPanel.add(pl);
        topPanel.add(mn);
        topPanel.add(money);
        topPanel.add(startbutton);
        pl.addActionListener(new claimsListener());
        pl.addKeyListener(new claimsListener());
        super.add(topPanel,BorderLayout.NORTH);
        JPanel mainPanel=new JPanel();
        mainPanel.setLayout(new BorderLayout());
        claimPanel=new JPanel();
        claimPanel.setBorder(BorderFactory.createCompoundBorder(
                        BorderFactory.createTitledBorder("Claims"),
                        BorderFactory.createEmptyBorder(5,5,5,5)));

        mainPanel.add(claimPanel,BorderLayout.CENTER);
        algoPanel=new JPanel();
            algoPanel.setBorder(BorderFactory.createCompoundBorder(
                            BorderFactory.createTitledBorder("Algorithm"),
                            BorderFactory.createEmptyBorder(5,5,5,5)));
            algoPanel.setLayout(new GridBagLayout());
            GridBagConstraints cons=new GridBagConstraints();
            cons.gridx=0;
            cons.gridy=0;
            cons.weightx=0.2;
            cons.weighty=0.5;
            cons.gridheight=1;
            cons.gridwidth=1;
            algolist=new JComboBox(new String[]{
                    "CEA Division",
                    "CEL Division",
                    "Convert to cooperative game",
                    "High claims first",
                    "Low claims first",
                    "Talmud"});
            algolist.addActionListener(new AlgoListener());
            algoPanel.add(algolist,cons);
            cons=new GridBagConstraints();
            cons.gridx=1;
            cons.gridy=0;
            cons.weightx=0.5;
            cons.weighty=0.5;
            cons.gridheight=1;
            cons.gridwidth=3;
            cons.fill=GridBagConstraints.HORIZONTAL;
            algo=new JTextField("(None)",40);
            algo.setEditable(false);
            algoPanel.add(algo,cons);
            cons=new GridBagConstraints();
            cons.gridx=4;
            cons.gridy=0;
            cons.weightx=0.2;
            cons.weighty=0.5;
            cons.gridheight=1;
            cons.gridwidth=1;
            JButton settings=new JButton("Settings...");
            settings.addActionListener(new SettingsListener());
            algo.addMouseListener(new SettingsListener());
            algoPanel.add(settings,cons);


        mainPanel.add(algoPanel,BorderLayout.SOUTH);
        super.add(mainPanel,BorderLayout.CENTER);

        algorithm=new NoAlgorithm();
        new AlgoListener().actionPerformed(null);
        setUpClaimPanel();
        super.setVisible(true);

    }

    class StartListener implements ActionListener {
        private JFrame frame;

        StartListener(JFrame frame) {
            this.frame = frame;
        }

        public void actionPerformed(ActionEvent e) {
            if (startbutton.getText().compareTo("Stop")==0){
                algorithm.stop();
                return;
            }
            final ArrayList<Claimer> claimer=new ArrayList<Claimer>();
            int p=0;
            try{
                p=Integer.parseInt(pl.getText());
            }
            catch(Exception ex){
                JOptionPane.showMessageDialog(frame,"Please enter integer value for number of players");
                return;
            }
            double m=0;
            try{
                m=Double.parseDouble(money.getText());
            }
            catch(Exception ex){
                JOptionPane.showMessageDialog(frame,"Please enter real value for money");
                return;
            }

            for (int x=0;x<p;x++){
                try{
                    claimer.add(new Claimer(Double.parseDouble(claims[x].getText())));
                }
                catch(Exception ex){
                    JOptionPane.showMessageDialog(frame,"Please enter real value into claim cell "+x);
                    return;
                }
            }
            Main.CON.setText("");
            Main.CONFRAME.setVisible(true);
            startbutton.setText("Stop");
            final double mn=m;
            new Thread(){
                public void run(){
                    algorithm.calculateShare(claimer,mn, new ActionListener(){
                        public void actionPerformed(ActionEvent e) {
                            startbutton.setText("Start");
                        }
                    });
                }
            }.start();
        }
    }
    public void finishAlgo(){

    }
    class UpdateDescriptorListener implements ActionListener{
        public void actionPerformed(ActionEvent e) {
            algo.setText(algorithm.getDescriptor());
            algoPanel.updateUI();
        }
    }
    class SettingsListener implements ActionListener, MouseListener {
        public void mousePressed(MouseEvent e) {}
        public void mouseReleased(MouseEvent e) {}
        public void mouseEntered(MouseEvent e) {}
        public void mouseExited(MouseEvent e) {}

        public void mouseClicked(MouseEvent e) {
            actionPerformed(null);
        }

        public void actionPerformed(ActionEvent e) {
            algorithm.settings(new UpdateDescriptorListener());
            algoPanel.updateUI();
        }
    }
    class AlgoListener implements ActionListener{
        public void actionPerformed(ActionEvent e) {
            if (((String)algolist.getSelectedItem()).compareTo("CEA Division")==0)
                algorithm=new CEADivision();
            else if (((String)algolist.getSelectedItem()).compareTo("CEL Division")==0)
                algorithm=new CELDivision();
            else if (((String)algolist.getSelectedItem()).compareTo("High claims first")==0)
                algorithm=new HighClaimsFirstDivision();
            else if (((String)algolist.getSelectedItem()).compareTo("Low claims first")==0)
                algorithm=new LowClaimsFirstDivision();
            else if (((String)algolist.getSelectedItem()).compareTo("Talmud")==0)
                algorithm=new TalmudDivision();
            else if (((String)algolist.getSelectedItem()).compareTo("Convert to cooperative game")==0)
                algorithm=new ConvertToCooperative();
            algo.setText(algorithm.getDescriptor());
            algoPanel.updateUI();
        }
    }

    class claimsListener implements ActionListener, KeyListener {
        public void actionPerformed(ActionEvent e) {
            setUpClaimPanel();
            repaint();
        }

        public void keyTyped(KeyEvent e) {

        }

        public void keyPressed(KeyEvent e) {

        }

        public void keyReleased(KeyEvent e) {
            setUpClaimPanel();
        }
    }

    class focusListener implements FocusListener{
        private int x;

        focusListener(int x) {
            this.x = x;
        }

        public void focusGained(FocusEvent e) {
            claims[x].setSelectionStart(0);
            claims[x].setSelectionEnd(claims[x].getText().length());
        }

        public void focusLost(FocusEvent e) {
            //To change body of implemented methods use File | Settings | File Templates.
        }
    }
    public void setUpClaimPanel(){
        try{
            int p=Integer.parseInt(pl.getText());
            claimPanel.removeAll();
            JTextField[] old=claims;
            claimPanel.setLayout(new GridLayout(1,p));
            claims=new JTextField[p];
            for (int x=0;x<p;x++){
                    JPanel pt=new JPanel();
                    String v="0";
                    if (old!=null&&x<old.length)v=old[x].getText();
                    claims[x]=new JTextField(v,5);
                    claims[x].addFocusListener(new focusListener(x));
                    pt.add(claims[x]);
                    claimPanel.add(pt);
            }
            claimPanel.updateUI();
            pack();
        }catch(Exception e){}

    }
    public static void main(String[] args){
        new WegaGUI();
    }
}

