package gy1szamolasok;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
import javax.swing.AbstractAction;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

public class SzamolasokFrame extends JFrame {

    public SzamolasokFrame() {
        super("Számolások");
        createGUI();
    }

    private List<Integer> numbers = new ArrayList<Integer>();
    private JList list;
    private JPanel cp;

    private void createGUI() {
        setLayout(new BorderLayout());
        JPanel np = new JPanel();
        np.setLayout(new FlowLayout());
        getContentPane().add(np, BorderLayout.NORTH);
        np.add(new JButton(newNumberAction));
        np.add(new JButton(newCounterAction));
        list = new JList(new Vector<Integer>(numbers));
        list.setPreferredSize(new Dimension(70, 30));
        getContentPane().add(new JScrollPane(list), BorderLayout.WEST);
        cp = new JPanel();
        cp.setLayout(new BoxLayout(cp, BoxLayout.PAGE_AXIS));
        cp.setPreferredSize(new Dimension(70, 30));
        getContentPane().add(new JScrollPane(cp), BorderLayout.CENTER);
        setSize(400, 300);
        //pack();
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);
    }

    public List<Integer> getList() {
        return numbers;
    }
    private AbstractAction newNumberAction = new AbstractAction("Új szám") {

        public void actionPerformed(ActionEvent e) {
            int i;
            String input = JOptionPane.showInputDialog(getContentPane(), "Adj meg egy egész számot", "Új szám", JOptionPane.QUESTION_MESSAGE);
            try {
                i = Integer.parseInt(input);
                numbers.add(i);
                list.setListData(new Vector<Integer>(numbers));
            } catch (NumberFormatException nfex) {
            }
        }
    }, newCounterAction = new AbstractAction("Új számoló") {

        public void actionPerformed(ActionEvent e) {
            String[] options = new String[]{"Összeg", "Szorzat", "Átlag"};
            Object selectedOption = JOptionPane.showInputDialog(getContentPane(), "Válassz egy számolótípust", "Új számoló", JOptionPane.QUESTION_MESSAGE, null, options, null);
            if (selectedOption == options[0]) {
                addNewCounter(0);
            } else if (selectedOption == options[1]) {
                addNewCounter(1);
            } else if (selectedOption == options[2]) {
                addNewCounter(2);
            }
        }
    };

    private void addNewCounter(int i) {
        switch (i) {
            case 0:
                cp.add(new Osszeg(this));
                break;
            case 1:
                cp.add(new Szorzat(this));
                break;
            case 2:
                cp.add(new Atlag(this));
                break;
        }
        cp.revalidate();
    }

    /*public static void main(String[] args) {
        new Main();
    }*/

}
