package sim.lib.others;

import java.awt.*;
import java.awt.event.*;

import sim.util.SimSeparator;

public class ModProperties extends Container implements ItemListener, ActionListener, FocusListener {

	//private TextField editRDI = new TextField(5);
	//private TextField editSelectSize = new TextField(5);
	private TextField editLabel = new TextField(10);
	private Choice displayReg = new Choice();
	private Choice initialReg = new Choice();
	private TextField initialValue = new TextField(5);
	private Checkbox showColour = new Checkbox("Show Value as Colour");
	//private double oldDelay;
	//private int oldRDI;
	//private int oldSize;
	//private int oldRegIndex;
	private String oldRegName;
	private String oldLabel;
	private boolean oldColour;
	private String oldInitRegName;
	private String oldInitRegValue;
	private Label pins = new Label("Pins");
	//private Label simulation = new Label("Simulation");

	public ModProperties(String[] pins, String[] regList, String regName, String modLabel, boolean colour, String inRegName, String inRegValue) {
		super();
		this.setLayout(new BorderLayout(0, 15));
		
		this.initialReg.add(""); //add empty as default
		this.displayReg.add(""); //add empty as default
		int z = 0;
		while (regList[z] != null && z < 50) {
			this.displayReg.add(regList[z]);
			this.initialReg.add(regList[z]);
			z++;
		}

		this.displayReg.select(regName);
		this.displayReg.addItemListener(this);
		
		this.oldColour = colour;
		showColour.setState(colour);

		this.oldLabel = modLabel;
		this.editLabel.addActionListener(this);
		this.editLabel.addFocusListener(this);
		this.editLabel.setText(modLabel);

		this.initialReg.select(inRegName);
		this.initialReg.addItemListener(this);
		
		this.oldInitRegValue = inRegValue;
		this.initialValue.setText(inRegValue);
		this.initialValue.addActionListener(this);
		this.initialValue.addFocusListener(this);
			
		//this.oldRegName = regName;


		/*this.oldDelay = delay;
		 this.editDelay.addActionListener(this);
		 this.editDelay.addFocusListener(this);
		 this.editDelay.setText(Double.toString(delay));

		 this.oldSize = size;
		 this.editSelectSize.addActionListener(this);
		 this.editSelectSize.addFocusListener(this);
		 this.editSelectSize.setText(Integer.toString(size));*/

		// pins
		Panel big = new Panel(new BorderLayout(0, 15));
		Panel p = new Panel(new BorderLayout(0,15));
		GridBagConstraints c = new GridBagConstraints();

		p.add(this.pins, BorderLayout.WEST);
		p.add(new SimSeparator(), BorderLayout.CENTER);
		big.add(p, BorderLayout.NORTH);
		p = new Panel(new GridBagLayout());

		c.fill = GridBagConstraints.NONE;
		c.insets = new Insets(0, 0, 0, 0);

		c.gridy = 0;
		c.gridx = 0;
		c.insets = new Insets(0, 0, 5, 0);
		c.anchor = GridBagConstraints.NORTHWEST;
		p.add(new Label("Display Register"), c);
		c.gridx = 1;
		p.add(this.displayReg, c);

		c.gridy = 1;
		c.gridx = 0;
		c.insets = new Insets(0, 0, 5, 0);
		p.add(new Label("Module Label"), c);
		c.gridx = 1;
		p.add(this.editLabel, c);
		
		c.gridy = 2;
		c.gridx = 0;
		c.insets = new Insets(0, 0, 5, 0);
		p.add(this.showColour, c);
		
		//register initial values
		c.gridy = 4;
		c.gridx = 0;
		c.insets = new Insets(0, 0, 5, 0);
		p.add(new Label("Register Inital Values"), c);
		c.insets = new Insets(0, 0, 0, 0);
		c.gridy = 5;
		c.gridx = 0;
		p.add(this.initialReg, c);
		c.gridx = 1;
		p.add(this.initialValue, c);
		

		//c.anchor = GridBagConstraints.EAST;
		//c.gridx = 1;

		big.add(p, BorderLayout.CENTER);

		c.gridx = 0;
		c.gridy = 0;
		c.insets = new Insets(0, 0, 15, 0);
		c.fill = GridBagConstraints.HORIZONTAL;
		this.add(big, BorderLayout.CENTER);

		// simulation
		/*big = new Panel(new BorderLayout(0, 15));
		 p = new Panel(new BorderLayout());
		 p.add(this.simulation, BorderLayout.WEST);
		 p.add(new SimSeparator(), BorderLayout.CENTER);
		 big.add(p, BorderLayout.NORTH);

		 p = new Panel(new FlowLayout(FlowLayout.LEFT, 0, 0));
		 p.add(new Label("Propagation Delay"));
		 p.add(this.editDelay);
		 big.add(p, BorderLayout.CENTER);

		 this.add(big, BorderLayout.CENTER);*/
	}

	public void addNotify() {
		super.addNotify();
		this.setSize(350, this.getPreferredSize().height * 4 + this.pins.getPreferredSize().height * 2 + 150);
	}

	public void itemStateChanged(ItemEvent e) {
		//Choice source = (Choice) e.getSource();

		//if (source == this.displayReg) {
		this.oldRegName = this.displayReg.getSelectedItem();
		this.oldInitRegName = this.initialReg.getSelectedItem();
		//System.out.println(this.oldRegName);
		//}
	}

	public void actionPerformed(ActionEvent e) {

		if ((TextField) e.getSource() == this.editLabel) {
			this.getLabel();
		}
		
		if ((TextField) e.getSource() == this.initialValue) {
			this.setInitRegValue();
		}
		//if(source == this.editDelay)
		//	this.getDelay();
		//else if(source == this.editBus)
		//this.getBusSize();
	}

	public void focusGained(FocusEvent e) {
	}

	public void focusLost(FocusEvent e) {


		//if(source == this.editDelay)
		//	this.getDelay();
		//else if(source == this.editBus)
		//	this.getBusSize();
		if ((TextField) e.getSource() == this.editLabel) {
			this.getLabel();
		}
		
		if ((TextField) e.getSource() == this.initialValue) {
			this.setInitRegValue();
		}
	}

	/*public int getBusSize()
	 {
	 int newBus;

	 try
	 {
	 newBus = Integer.valueOf(this.editBus.getText()).intValue();

	 if(newBus > 0)
	 this.oldBus = newBus;
	 else
	 this.editBus.setText(Integer.toString(this.oldBus));
	 }
	 catch(NumberFormatException nfe)
	 {
	 this.editBus.setText(Integer.toString(this.oldBus));
	 }

	 return this.oldBus;
	 }*/
	public void setRegName() {
		String newReg = this.displayReg.getSelectedItem();
		this.oldRegName = newReg;
		this.displayReg.select(this.oldRegName);
	}

	public String getRegName() {
		//this.oldRegName = this.displayReg.getSelectedItem();
		/*String newReg = this.displayReg.getSelectedItem();
		 this.oldRegName = newReg;
		 this.displayReg.select(this.oldRegName);*/
		//System.out.println(this.oldRegName);
		return this.oldRegName;
	}

	public String getInitRegName() {
		return this.oldInitRegName;
	}
	
	public void setInitRegName() {
		String newInitRegName = this.initialReg.getSelectedItem();
		this.oldInitRegName = newInitRegName;
		this.initialReg.select(this.oldInitRegName);
	}
	
	public String getInitRegValue() {
		return this.oldInitRegValue;
	}
	
	public void setInitRegValue() {
		String newInitRegValue = this.initialValue.getText();
		this.oldInitRegValue = newInitRegValue;
		this.initialValue.setText(this.oldInitRegValue);
	}
	
	public String getLabel() {
		String newLabel = this.editLabel.getText();
		this.oldLabel = newLabel;
		return this.oldLabel;
	}
	
	public boolean getColour()
	{
		return this.showColour.getState();
	}

	/*public int getInputSize() {
	 int newSize;

	 try {
	 newSize = Integer.valueOf(this.editSelectSize.getText()).intValue();

	 if ((newSize > 0) && (newSize < 7)) {
	 this.oldSize = newSize;
	 } else {
	 this.editSelectSize.setText(Integer.toString(this.oldSize));
	 }
	 } catch (NumberFormatException nfe) {
	 this.editSelectSize.setText(Integer.toString(this.oldSize));
	 }

	 return this.oldSize;
	 }*/

	/*public double getDelay()
	 {
	 double newDelay;

	 try
	 {
	 newDelay = Double.valueOf(this.editDelay.getText()).doubleValue();

	 if(newDelay >= 0)
	 this.oldDelay = newDelay;
	 else
	 this.editDelay.setText(Double.toString(this.oldBus));
	 }
	 catch(NumberFormatException nfe)
	 {
	 this.editDelay.setText(Double.toString(this.oldBus));
	 }

	 return this.oldDelay;
	 }*/
	public Dimension getPreferredSize() {
		return this.getSize();
	}

	public Dimension getMinimumSize() {
		return this.getSize();
	}

	public Dimension getMaximumSize() {
		return this.getSize();
	}
}
