package ui.view;

import ui.controller.Control;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;

/**
 * This Class represents a Popup to edit the shown Information.
 * 
 * @author Gruppe14
 */
public class DisplayedInformationPopUp extends JDialog {

	private static final long serialVersionUID = 1L;
	private final JPanel contentPanel = new JPanel();
	private final JButton btnOk = new JButton("OK");
	private MyCanvas canvas;
	private Control controller;
	private JCheckBox objectEnergyCheckbox;
	private JCheckBox connectionCheckbox;
	private JCheckBox colorizedBorderCheckbox;
	private JCheckBox nodeOfnodeConnectionCheckbox;
	private JPanel toUpdate;

	/**
	 * Constructor.
	 * 
	 * @param canvas
	 *            the Canvas
	 */
    public DisplayedInformationPopUp(MyCanvas canvas, JPanel update, Control cont, JFrame parentFrame) {
        super((java.awt.Frame) null, true);
		setModalityType(java.awt.Dialog.ModalityType.APPLICATION_MODAL);
		this.setTitle("Edit Displayed Informations");
		setBounds(100, 100, 400, 276);
        setLocationRelativeTo(parentFrame);
        getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);
		this.canvas = canvas;
		this.toUpdate = update;
		controller = cont;

		objectEnergyCheckbox = new JCheckBox("Display Total Energy of Objects");
		objectEnergyCheckbox.setBounds(19, 19, 300, 23);
		contentPanel.add(objectEnergyCheckbox);

		connectionCheckbox = new JCheckBox("Display Connection Properties");
		connectionCheckbox.setBounds(19, 57, 300, 23);
		contentPanel.add(connectionCheckbox);

		objectEnergyCheckbox.setSelected(canvas.getShowedInformation()[1]);
		connectionCheckbox.setSelected(canvas.getShowedInformation()[0]);
		btnOk.addActionListener(actionEvent -> {
			setInformation(connectionCheckbox.isSelected(), objectEnergyCheckbox.isSelected(),
					colorizedBorderCheckbox.isSelected(), nodeOfnodeConnectionCheckbox.isSelected());
			controller.getSimManager().getFlexiblePane().recalculate();
			dispose();
		});
		btnOk.setBounds(174, 193, 82, 23);
		contentPanel.add(btnOk);

		JButton btnCancel = new JButton(Languages.getLanguage()[34]);
		btnCancel.setActionCommand(Languages.getLanguage()[34]);
		btnCancel.addActionListener(actionEvent -> dispose());
		btnCancel.setBounds(69, 193, 89, 23);
		contentPanel.add(btnCancel);
		
		colorizedBorderCheckbox = new JCheckBox("Display colorized Border for Objects");
		colorizedBorderCheckbox.setBounds(19, 96, 369, 23);
		contentPanel.add(colorizedBorderCheckbox);
		colorizedBorderCheckbox.setSelected(canvas.getShowedInformation()[3]);
		
		nodeOfnodeConnectionCheckbox = new JCheckBox("Display outside Connections in gouped Nodes");
		nodeOfnodeConnectionCheckbox.setBounds(19, 133, 369, 23);
		contentPanel.add(nodeOfnodeConnectionCheckbox);
		nodeOfnodeConnectionCheckbox.setSelected(canvas.getShowedInformation()[4]);
	}

	/**
	 * Set the Information true or false.
	 * 
	 * @param connection
	 *            conection Information
	 * @param object
	 *            Object Information
	 * @param nodeOfnode 
	 */
	private void setInformation(boolean connection, boolean object, boolean borders, boolean nodeOfnode) {
		canvas.setShowedInformation(connection, object, borders, nodeOfnode);
		canvas.repaint();
		toUpdate.updateUI();
		
	}
}
