package ui.view;

import javax.swing.ButtonGroup;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JLabel;

import java.awt.BorderLayout;
import java.awt.Font;

import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.border.EmptyBorder;

import classes.CpsEdge;
import ui.controller.Control;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

/**
 * Popup for Editing Edges.
 * 
 * @author Gruppe14
 */
public class EditEdgesPopUp extends JDialog {

	private static final long serialVersionUID = 1L;
	private final JPanel contentPanel = new JPanel();
	private JTextField capacityField;
	private float capacity;
	private JRadioButton rdbtnChangeForAll;
	private JRadioButton rdbtnChangeForNew;
	private JRadioButton rdbtnChangeForAll1;
	private Control controller;
	private MyCanvas canvas;
	private JLabel lblenterinfiniteFor;

	/**
	 * Launch the application.
	 * 
	 * @param args
	 *            standard
	 */
	public static void main(String[] args) {
		try {

			EditEdgesPopUp dialog = new EditEdgesPopUp();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Constructor.
	 */
	public EditEdgesPopUp() {
		super((java.awt.Frame) null, true);
		setModalityType(java.awt.Dialog.ModalityType.APPLICATION_MODAL);
		this.setTitle("Edit Capacities of Edges");
		setBounds(100, 100, 438, 220);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);

		JLabel lblMaximumCapacity = new JLabel("Maximum Capacity:");
		lblMaximumCapacity.setFont(new Font("Tahoma", Font.PLAIN, 11));
		lblMaximumCapacity.setBounds(10, 11, 98, 14);
		contentPanel.add(lblMaximumCapacity);

		capacityField = new JTextField();
		capacityField.setBounds(107, 8, 120, 20);
		contentPanel.add(capacityField);
		capacityField.setColumns(10);

		rdbtnChangeForAll = new JRadioButton("Change for all existing Edges only");
		rdbtnChangeForAll.setBounds(10, 39, 265, 23);
		contentPanel.add(rdbtnChangeForAll);

		rdbtnChangeForNew = new JRadioButton("Change for new created Edges only");
		rdbtnChangeForNew.setBounds(10, 65, 265, 23);
		contentPanel.add(rdbtnChangeForNew);

		rdbtnChangeForAll1 = new JRadioButton("Change for all existing and new created Edges");
		rdbtnChangeForAll1.setBounds(10, 95, 296, 23);
		contentPanel.add(rdbtnChangeForAll1);

		JButton btnCancel = new JButton("Cancel");
		btnCancel.setActionCommand("Cancel");
		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				dispose();
			}
		});
		btnCancel.setBounds(285, 147, 89, 23);
		contentPanel.add(btnCancel);

		JButton btnOk1 = new JButton("OK");
		btnOk1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (capacityField.getText().equals("infinite")) {
					capacity = -1;
					if (rdbtnChangeForAll.isSelected()) {
						changeForExisting(capacity);
						dispose();
					} else if (rdbtnChangeForNew.isSelected()) {
						changeForNew(capacity);
						dispose();
					} else if (rdbtnChangeForAll1.isSelected()) {
						changeForExAndNew(capacity);
						dispose();
					} else {
						JOptionPane.showMessageDialog(new JFrame(), "Please select one of the options");
					}
				} else {
					try {
						capacity = Float.parseFloat(capacityField.getText().toString());
						if (capacity < 0) {
							throw new NumberFormatException();
						}
						if (rdbtnChangeForAll.isSelected()) {
							changeForExisting(capacity);
							dispose();
						} else if (rdbtnChangeForNew.isSelected()) {
							changeForNew(capacity);
							dispose();
						} else if (rdbtnChangeForAll1.isSelected()) {
							changeForExAndNew(capacity);
							dispose();
						} else {
							JOptionPane.showMessageDialog(new JFrame(), "Please select one of the options");
						}
					} catch (NumberFormatException eex) {
						JOptionPane.showMessageDialog(new JFrame(),
								"Please enter a number greater or equal 0 in the Field for Maximum Capacity");
					}
				}
			}
		});
		btnOk1.setBounds(186, 147, 89, 23);
		contentPanel.add(btnOk1);
		this.setTitle("Edit Edge Capacities");
		ButtonGroup bG = new ButtonGroup();
		bG.add(rdbtnChangeForAll1);
		bG.add(rdbtnChangeForNew);
		bG.add(rdbtnChangeForAll);

		lblenterinfiniteFor = new JLabel("(enter \"infinite\" for infinite Capacity)");
		lblenterinfiniteFor.setBounds(237, 11, 175, 14);
		contentPanel.add(lblenterinfiniteFor);
	}

	/**
	 * Set the Canvas.
	 * 
	 * @param can
	 *            the Canvas
	 */
	public void setCanvas(MyCanvas can) {
		canvas = can;
	}

	/**
	 * set the Controller.
	 * 
	 * @param cont
	 *            the Controller
	 */
	public void setController(Control cont) {
		controller = cont;
	}

	/**
	 * set edge capacity for new edges.
	 * 
	 * @param cap
	 *            the capacity
	 */
	public void changeForNew(float cap) {
		canvas.setEdgeCapacity(cap);
	}

	/**
	 * Set Capacity for all existing Edges.
	 * 
	 * @param cap
	 *            the Capacity
	 */
	public void changeForExisting(float cap) {
		for (CpsEdge edge : controller.getModel().getEdgesOnCanvas()) {
			edge.setCapacity(cap);
		}
		controller.calculateStateForCurrentTimeStep();
		canvas.repaint();
	}

	/**
	 * Set the Capacity for all existing and new edges.
	 * 
	 * @param cap
	 *            the capacity
	 */
	public void changeForExAndNew(float cap) {
		changeForNew(cap);
		changeForExisting(cap);
	}

}
