package ui.view;

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

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

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

/**
 * 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(Languages.getLanguage()[46]);
		setBounds(100, 100, 600, 220);
		setLocationRelativeTo(this.getParent());
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);

		JLabel lblMaximumCapacity = new JLabel(Languages.getLanguage()[47]);
		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(Languages.getLanguage()[48]);
		rdbtnChangeForAll.setBounds(10, 39, 330, 23);
		contentPanel.add(rdbtnChangeForAll);

		rdbtnChangeForNew = new JRadioButton(Languages.getLanguage()[49]);
		rdbtnChangeForNew.setBounds(10, 65, 330, 23);
		contentPanel.add(rdbtnChangeForNew);

		rdbtnChangeForAll1 = new JRadioButton(Languages.getLanguage()[50]);
		rdbtnChangeForAll1.setBounds(10, 95, 400, 23);
		contentPanel.add(rdbtnChangeForAll1);

		JButton btnCancel = new JButton(Languages.getLanguage()[51]);
		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(), Languages.getLanguage()[52]);
					}
				} 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(), Languages.getLanguage()[52]);
						}
					} catch (NumberFormatException eex) {
						JOptionPane.showMessageDialog(new JFrame(), Languages.getLanguage()[53]);
					}
				}
			}
		});
		btnOk1.setBounds(186, 147, 89, 23);
		contentPanel.add(btnOk1);
		this.setTitle(Languages.getLanguage()[54]);
		ButtonGroup bG = new ButtonGroup();
		bG.add(rdbtnChangeForAll1);
		bG.add(rdbtnChangeForNew);
		bG.add(rdbtnChangeForAll);

		lblenterinfiniteFor = new JLabel(Languages.getLanguage()[55]);
		lblenterinfiniteFor.setBounds(237, 11, 360, 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) {
		controller.setMaxCapacity(cap);
		controller.resetSimulation();
	}

	/**
	 * Set Capacity for all existing Edges.
	 * 
	 * @param cap
	 *            the Capacity
	 */
	public void changeForExisting(float cap) {
		/*
		 * for(SubNet sn: controller.getSimManager().getSubNets()){ for(CpsEdge
		 * edge: sn.getEdges()){ edge.setCapacity(cap); } } for(CpsEdge edge:
		 * controller.getSimManager().getBrokenEdges()){ edge.setCapacity(cap);
		 * }
		 */
		for (CpsEdge edge : controller.getModel().getEdgesOnCanvas()) {
			edge.setCapacity(cap);
			edge.setState(true);
		}
		for (AbstractCpsObject abs : controller.getModel().getObjectsOnCanvas()) {
			if (abs instanceof CpsUpperNode) {
				changeInUpperNode((CpsUpperNode) abs, cap);
			}
		}
		controller.resetSimulation();
		controller.calculateStateForCurrentTimeStep();
		canvas.repaint();
	}

	public void changeInUpperNode(CpsUpperNode uN, float cap) {
		for (CpsEdge e : uN.getNodeEdges()) {
			e.setCapacity(cap);
		}
		for (CpsEdge e : uN.getOldEdges()) {
			e.setCapacity(cap);
		}
		for (AbstractCpsObject abs : uN.getNodes()) {
			if (abs instanceof CpsUpperNode) {
				changeInUpperNode((CpsUpperNode) abs, cap);
			}
		}
	}

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

}
