/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pnandmd;

import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyEvent;
import java.io.File;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import pnandmd.Point3.TworzSiec2;

/**
 *
 * @author Ania
 */
public class MainWin extends JFrame {

    private JPanel panel1 = new JPanel();
    private JPanel panel2 = new JPanel();
    private JPanel panel3 = new JPanel();
    private MainWin wm;
    private File pdbDir;
    private JTextField text1;
    private JComboBox comBox;
    private JComboBox comBox2;
    private JLabel l3;
    private JTextField f2;
    private JLabel l4;
    private JTextField f3;
    private JLabel l5;
    private JTextField f4;
    private JLabel l6;
    private float param1 = 2;
    private double param2 = 4;
    private int param3 = 4;
    private File PNdir;
    private File markDir;
    private JTextField fileCh2;
    private JTextField fileCh3;
    private JTextField steps;
    private JRadioButton alg1;
    private JRadioButton alg2;
    private JRadioButton alg3;
    private JTextField textField;
    private JRadioButton radio1;

    private JRadioButton radioEnd;
    private JRadioButton radioStep;

    private boolean repeat = false;

    public MainWin() {
        super();
        wm = this;
        Container cp = getContentPane();
        cp.setPreferredSize(new Dimension(500, 600));
        this.setResizable(false);
        JTabbedPane tabelP = new JTabbedPane();
        tabelP.addTab("Create PN", null, panel1, "Create PN using MD trajectory");
        tabelP.setMnemonicAt(0, KeyEvent.VK_1);
        tabelP.addTab("Simulation", null, panel2, "Simulation of PN");
        tabelP.setMnemonicAt(1, KeyEvent.VK_2);
        tabelP.addTab("Create PDB", null, panel3, "Create PDB file based on PN and marking from simulation");
        tabelP.setMnemonicAt(2, KeyEvent.VK_3);

        setElementsPanel1();
        setElementsPanel2();
        setElementsPanel3();
        cp.add(tabelP);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);
        pack();

    }

    private void setElementsPanel1() {
        panel1.setLayout(new BoxLayout(panel1, BoxLayout.PAGE_AXIS));
        JPanel p1 = new JPanel();
        p1.setLayout(new BoxLayout(p1, BoxLayout.LINE_AXIS));
        JLabel l1 = new JLabel("Choose directory with PDB files to create Petri Net:");
        l1.setAlignmentX(Component.LEFT_ALIGNMENT);
        p1.add(Box.createRigidArea(new Dimension(10, 10)));
        p1.add(l1);
        p1.add(Box.createHorizontalGlue());

        JPanel p2 = new JPanel();
        p2.setLayout(new BoxLayout(p2, BoxLayout.LINE_AXIS));
        text1 = new JTextField(System.getProperty("user.home"));
        text1.setPreferredSize(new Dimension(300, 25));
        text1.setMaximumSize(new Dimension(300, 25));
        text1.setEditable(false);
        p2.add(Box.createRigidArea(new Dimension(10, 10)));
        p2.add(text1);
        pdbDir = new File(System.getProperty("user.home"));
        JButton button1 = new JButton("Choose");
        final JFileChooser fileCh = new JFileChooser(System.getProperty("user.home"));
        fileCh.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        button1.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                int returnVal = fileCh.showOpenDialog(wm);
                if (returnVal == JFileChooser.APPROVE_OPTION) {
                    pdbDir = fileCh.getSelectedFile();
                    text1.setText(pdbDir.getAbsolutePath());
                }
            }
        });

        p2.add(Box.createRigidArea(new Dimension(10, 10)));
        p2.add(button1);
        p2.add(Box.createHorizontalGlue());

        JPanel p3 = new JPanel();
        p3.setLayout(new BoxLayout(p3, BoxLayout.LINE_AXIS));
        JLabel l2 = new JLabel("Choose type of the PDB conversion to the Petri net:");
        p3.add(Box.createRigidArea(new Dimension(10, 10)));
        p3.add(l2);
        p3.add(Box.createHorizontalGlue());

        JPanel p4 = new JPanel();
        p4.setLayout(new BoxLayout(p4, BoxLayout.LINE_AXIS));
        comBox = new JComboBox();
        comBox.setMaximumSize(new Dimension(300, 25));
        comBox.setPreferredSize(new Dimension(300, 25));
        comBox.addItem("1. Place - point 1.");
        comBox.addItem("2. Place - point 2.");
        comBox.addItem("3. Place - conformation.");
        comBox.addItem("4. Contacts.");
        comBox.addItem("5. Place - less connections");

        comBox.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                int type = comBox.getSelectedIndex();
                if ((type == 0) || (type == 1) || (type == 4)) {
                    l3.setEnabled(true);
                    f2.setEnabled(true);
                    l4.setEnabled(false);
                    f3.setEnabled(false);
                    l5.setEnabled(false);
                    f4.setEnabled(false);
                    l6.setEnabled(false);
                    alg1.setEnabled(false);
                    alg2.setEnabled(false);
                    alg3.setEnabled(false);
                } else if (type == 2) {
                    l3.setEnabled(false);
                    f2.setEnabled(false);
                    l4.setEnabled(true);
                    f3.setEnabled(true);
                    l5.setEnabled(false);
                    f4.setEnabled(false);
                    l6.setEnabled(true);
                    alg1.setEnabled(true);
                    alg2.setEnabled(true);
                    alg3.setEnabled(true);
                } else if (type == 3) {
                    l3.setEnabled(false);
                    f2.setEnabled(false);
                    l4.setEnabled(false);
                    f3.setEnabled(false);
                    l5.setEnabled(true);
                    f4.setEnabled(true);
                    l6.setEnabled(false);
                    alg1.setEnabled(false);
                    alg2.setEnabled(false);
                    alg3.setEnabled(false);
                }
            }
        });

        p4.add(Box.createRigidArea(new Dimension(10, 10)));
        p4.add(comBox);
        p4.add(Box.createHorizontalGlue());

        JPanel p6 = new JPanel();
        p6.setLayout(new BoxLayout(p6, BoxLayout.LINE_AXIS));
        l3 = new JLabel("Choose range of the network:                                      ");
        f2 = new JTextField("2");
        f2.setMaximumSize(new Dimension(100, 25));
        f2.setPreferredSize(new Dimension(100, 25));
        p6.add(Box.createRigidArea(new Dimension(10, 10)));
        p6.add(l3);
        p6.add(Box.createRigidArea(new Dimension(10, 10)));
        p6.add(f2);
        p6.add(Box.createHorizontalGlue());

        JPanel p7 = new JPanel();
        p7.setLayout(new BoxLayout(p7, BoxLayout.LINE_AXIS));
        l4 = new JLabel("Choose threashold for distinguish conformations:");
        f3 = new JTextField("5");
        f3.setMaximumSize(new Dimension(100, 25));
        f3.setPreferredSize(new Dimension(100, 25));
        l4.setEnabled(false);
        f3.setEnabled(false);

        p7.add(Box.createRigidArea(new Dimension(10, 10)));
        p7.add(l4);
        p7.add(Box.createRigidArea(new Dimension(10, 10)));
        p7.add(f3);
        p7.add(Box.createHorizontalGlue());

        JPanel p9 = new JPanel();
        p9.setLayout(new BoxLayout(p9, BoxLayout.LINE_AXIS));
        l6 = new JLabel("Aligment algorithm:");
        alg1 = new JRadioButton("Full, fast");
        alg2 = new JRadioButton("Full, slow");
        alg3 = new JRadioButton("BioJava very slow");
        ButtonGroup buttonGroup = new ButtonGroup();
        buttonGroup.add(alg1);
        buttonGroup.add(alg2);
        buttonGroup.add(alg3);
        alg1.setSelected(true);
        p9.add(l6);
        p9.add(Box.createRigidArea(new Dimension(10, 10)));
        p9.add(alg1);
        p9.add(Box.createRigidArea(new Dimension(10, 10)));
        p9.add(alg2);
        p9.add(Box.createRigidArea(new Dimension(10, 10)));
        p9.add(alg3);
        alg1.setEnabled(false);
        alg2.setEnabled(false);
        alg3.setEnabled(false);
        l6.setEnabled(false);

        JPanel p8 = new JPanel();
        p8.setLayout(new BoxLayout(p8, BoxLayout.LINE_AXIS));
        l5 = new JLabel("Choose contact distance:                                             ");
        f4 = new JTextField("8");
        f4.setMaximumSize(new Dimension(100, 25));
        f4.setPreferredSize(new Dimension(100, 25));
        l5.setEnabled(false);
        f4.setEnabled(false);
        p8.add(Box.createRigidArea(new Dimension(10, 10)));
        p8.add(l5);
        p8.add(Box.createRigidArea(new Dimension(10, 10)));
        p8.add(f4);
        p8.add(Box.createHorizontalGlue());

        JPanel p5 = new JPanel();
        JButton button2 = new JButton("Calculate");

        button2.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                int type = comBox.getSelectedIndex();
                if (type == 0) {
                    param1 = Float.parseFloat(f2.getText());
                    pnandmd.Point1.TworzSiec2 ts = new pnandmd.Point1.TworzSiec2(pdbDir.getAbsolutePath(), param1);
                    ts.tworzenie();
                    ts.koniec();
                } else if (type == 1) {
                    param1 = Float.parseFloat(f2.getText());
                    pnandmd.Point2.TworzSiec ts = new pnandmd.Point2.TworzSiec(pdbDir.getAbsolutePath(), param1);
                    ts.tworzenie();
                    ts.koniec();
                } else if (type == 2) {
                    param2 = Double.parseDouble(f3.getText());
                    int algType;
                    if (alg1.isSelected()) {
                        algType = 0;
                    } else if (alg2.isSelected()) {
                        algType = 1;
                    } else {
                        algType = 2;
                    }
                    pnandmd.Conformation.TworzSiec ts;
                    if (!repeat) {
                        ts = new pnandmd.Conformation.TworzSiec(pdbDir.getAbsolutePath(), param2, algType);
                        ts.tworzenie();
                        ts.koniec();
                    } else {
                        double progStart = 1.9;
                        double delta = 0.2;
                        double progStop = 3.1;
                        for (double i = progStart; i <= progStop; i = i + delta) {
                            ts = new pnandmd.Conformation.TworzSiec(pdbDir.getAbsolutePath(), i, algType, "" + (i * 10));
                            ts.tworzenie();
                            ts.koniec();
                        }
                    }

                } else if (type == 3) {
                    param3 = Integer.parseInt(f4.getText());
                    pnandmd.Contacts.TworzSiec ts = new pnandmd.Contacts.TworzSiec(pdbDir.getAbsolutePath(), param3);
                    ts.tworzenie();
                    ts.koniec();
                } else if (type == 4) {
                    param1 = Float.parseFloat(f2.getText());
                    TworzSiec2 ts = new TworzSiec2(pdbDir.getAbsolutePath(), param1);
                    ts.tworzenie();
                    ts.koniec();
                }
            }
        });

        p5.setLayout(new BoxLayout(p5, BoxLayout.LINE_AXIS));
        p5.add(button2);

        panel1.add(Box.createRigidArea(new Dimension(10, 30)));
        panel1.add(p1);
        panel1.add(Box.createRigidArea(new Dimension(10, 10)));
        panel1.add(p2);
        panel1.add(Box.createRigidArea(new Dimension(10, 50)));
        panel1.add(p3);
        panel1.add(Box.createRigidArea(new Dimension(10, 10)));
        panel1.add(p4);
        panel1.add(Box.createRigidArea(new Dimension(10, 70)));
        panel1.add(p6);
        panel1.add(Box.createRigidArea(new Dimension(10, 10)));
        panel1.add(p7);
        panel1.add(Box.createRigidArea(new Dimension(10, 10)));
        panel1.add(p9);
        panel1.add(Box.createRigidArea(new Dimension(10, 10)));
        panel1.add(p8);
        panel1.add(Box.createRigidArea(new Dimension(10, 70)));
        panel1.add(p5);
        panel1.add(Box.createVerticalGlue());

    }

    private void setElementsPanel2() {
        panel2.setLayout(new BoxLayout(panel2, BoxLayout.PAGE_AXIS));

        JLabel l1 = new JLabel("Choose directory with PN files:");

        fileCh2 = new JTextField(System.getProperty("user.home"));
        fileCh2.setPreferredSize(new Dimension(300, 25));
        fileCh2.setMaximumSize(new Dimension(300, 25));
        fileCh2.setEditable(false);

        PNdir = new File(System.getProperty("user.home"));
        JButton button1 = new JButton("Choose");
        final JFileChooser fileCh = new JFileChooser(System.getProperty("user.home"));
        fileCh.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        button1.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                int returnVal = fileCh.showOpenDialog(wm);
                if (returnVal == JFileChooser.APPROVE_OPTION) {
                    PNdir = fileCh.getSelectedFile();
                    fileCh2.setText(PNdir.getAbsolutePath());
                }
            }
        });

        JLabel l2 = new JLabel("Choose type of the PN:");

        comBox2 = new JComboBox();
        comBox2.setMaximumSize(new Dimension(300, 25));
        comBox2.setPreferredSize(new Dimension(300, 25));
        comBox2.addItem("1. Regular PN.");
        comBox2.addItem("2. PN with time.");
        comBox2.addItem("3. PN with priority.");
        comBox2.addItem("4. Random-priority PN.");
        comBox2.addItem("5. Guard time PN.");

        JLabel l3 = new JLabel("Steps of simulation: ");
        steps = new JTextField("100");
        steps.setPreferredSize(new Dimension(100, 25));
        steps.setMaximumSize(new Dimension(100, 25));

        radioEnd = new JRadioButton("Write marking at the end");
        radioStep = new JRadioButton("Write marking after every step");
        ButtonGroup group = new ButtonGroup();
        group.add(radioEnd);
        group.add(radioStep);
        radioEnd.setSelected(true);

        JButton button2 = new JButton("Run");
        button2.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {

                Simulation s = new Simulation(PNdir, Integer.parseInt(steps.getText()), comBox2.getSelectedIndex(), radioEnd.isSelected());
                s.simulate();
                s = null;
            }

        });

        JPanel p1 = new JPanel();
        JPanel p2 = new JPanel();
        JPanel p3 = new JPanel();
        JPanel p4 = new JPanel();
        JPanel p5 = new JPanel();
        JPanel p6 = new JPanel();
        JPanel p7 = new JPanel();

        p1.setLayout(new BoxLayout(p1, BoxLayout.LINE_AXIS));
        p2.setLayout(new BoxLayout(p2, BoxLayout.LINE_AXIS));
        p3.setLayout(new BoxLayout(p3, BoxLayout.LINE_AXIS));
        p4.setLayout(new BoxLayout(p4, BoxLayout.LINE_AXIS));
        p5.setLayout(new BoxLayout(p5, BoxLayout.LINE_AXIS));
        p6.setLayout(new BoxLayout(p6, BoxLayout.LINE_AXIS));
        p7.setLayout(new BoxLayout(p7, BoxLayout.LINE_AXIS));

        p1.add(Box.createRigidArea(new Dimension(10, 10)));
        p1.add(l1);
        p1.add(Box.createHorizontalGlue());

        p2.add(Box.createRigidArea(new Dimension(10, 10)));
        p2.add(fileCh2);
        p2.add(Box.createRigidArea(new Dimension(10, 10)));
        p2.add(button1);
        p2.add(Box.createHorizontalGlue());

        p6.add(Box.createRigidArea(new Dimension(10, 10)));
        p6.add(l2);
        p6.add(Box.createHorizontalGlue());

        p3.add(Box.createRigidArea(new Dimension(10, 10)));
        p3.add(comBox2);
        p3.add(Box.createHorizontalGlue());

        p4.add(Box.createRigidArea(new Dimension(10, 10)));
        p4.add(l3);
        p4.add(Box.createRigidArea(new Dimension(10, 10)));
        p4.add(steps);
        p4.add(Box.createHorizontalGlue());

        p7.add(Box.createRigidArea(new Dimension(10, 10)));
        p7.add(radioEnd);
        p7.add(Box.createRigidArea(new Dimension(10, 5)));
        p7.add(Box.createRigidArea(new Dimension(10, 10)));
        p7.add(radioStep);

        p5.setLayout(new BoxLayout(p5, BoxLayout.LINE_AXIS));
        p5.add(button2);

        panel2.add(Box.createRigidArea(new Dimension(10, 30)));
        panel2.add(p1);
        panel2.add(Box.createRigidArea(new Dimension(10, 10)));
        panel2.add(p2);
        panel2.add(Box.createRigidArea(new Dimension(10, 50)));
        panel2.add(p6);
        panel2.add(Box.createRigidArea(new Dimension(10, 10)));
        panel2.add(p3);
        panel2.add(Box.createRigidArea(new Dimension(10, 50)));
        panel2.add(p4);
        panel2.add(Box.createRigidArea(new Dimension(10, 30)));
        panel2.add(p7);
        panel2.add(Box.createRigidArea(new Dimension(10, 30)));
        panel2.add(p5);
        panel2.add(Box.createVerticalGlue());

    }

    private void setElementsPanel3() {
        panel3.setLayout(new BoxLayout(panel3, BoxLayout.PAGE_AXIS));

        JLabel l1 = new JLabel("Choose directory with PN files and marking (only for alg 1, 2 and 3):");

        fileCh3 = new JTextField(System.getProperty("user.home"));
        fileCh3.setPreferredSize(new Dimension(300, 25));
        fileCh3.setMaximumSize(new Dimension(300, 25));
        fileCh3.setEditable(false);

        JButton button1 = new JButton("Choose");
        final JFileChooser fileCh = new JFileChooser(System.getProperty("user.home"));
        fileCh.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        button1.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                int returnVal = fileCh.showOpenDialog(wm);
                if (returnVal == JFileChooser.APPROVE_OPTION) {
                    markDir = fileCh.getSelectedFile();
                    fileCh3.setText(markDir.getAbsolutePath());
                }
            }
        });

        JButton b2 = new JButton("Create");
        b2.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                int placeNr;
                if (radio1.isSelected()) {
                    placeNr = -1;
                } else {
                    placeNr = Integer.parseInt(textField.getText());
                }
                PDBgeneration pdbGen = new PDBgeneration(markDir, placeNr);
            }
        }
        );

        JPanel p1 = new JPanel();
        JPanel p2 = new JPanel();
        JPanel p3 = new JPanel();

        JPanel p4 = new JPanel();

        p1.setLayout(
                new BoxLayout(p1, BoxLayout.LINE_AXIS));
        p2.setLayout(
                new BoxLayout(p2, BoxLayout.LINE_AXIS));
        p3.setLayout(
                new BoxLayout(p3, BoxLayout.LINE_AXIS));
        p4.setLayout(
                new BoxLayout(p4, BoxLayout.LINE_AXIS));

        radio1 = new JRadioButton("Marking.txt in folder");
        JRadioButton radio2 = new JRadioButton("Insert number of one marked place:");
        ButtonGroup group = new ButtonGroup();

        group.add(radio1);

        group.add(radio2);

        radio1.addItemListener(
                new ItemListener() {

                    @Override
                    public void itemStateChanged(ItemEvent e
                    ) {
                        if (e.getStateChange() == ItemEvent.SELECTED) {
                            textField.setEnabled(false);
                        } else {
                            textField.setEnabled(true);
                        }
                    }
                }
        );
        textField = new JTextField();

        textField.setPreferredSize(
                new Dimension(80, 25));
        textField.setMaximumSize(
                new Dimension(80, 25));

        p1.add(Box.createRigidArea(new Dimension(10, 10)));
        p1.add(l1);

        p1.add(Box.createHorizontalGlue());

        p2.add(Box.createRigidArea(new Dimension(10, 10)));
        p2.add(fileCh3);

        p2.add(Box.createRigidArea(new Dimension(10, 10)));
        p2.add(button1);

        p2.add(Box.createHorizontalGlue());

        p3.add(b2);

        p4.add(radio1);

        p4.add(Box.createRigidArea(new Dimension(10, 10)));
        p4.add(radio2);

        p4.add(textField);

        radio1.setSelected(
                true);

        panel3.add(Box.createRigidArea(new Dimension(10, 20)));
        panel3.add(p4);

        panel3.add(Box.createRigidArea(new Dimension(10, 30)));
        panel3.add(p1);

        panel3.add(Box.createRigidArea(new Dimension(10, 10)));
        panel3.add(p2);

        panel3.add(Box.createRigidArea(new Dimension(10, 70)));
        panel3.add(p3);

        panel3.add(Box.createVerticalGlue());
    }

}
