package sim.lib.others;

import java.awt.BorderLayout;
import java.awt.Checkbox;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Label;
import java.awt.Panel;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.BoxLayout;

import sim.util.SimSeparator;

public class ModuleIDProperties extends Container implements ActionListener, ItemListener {
	private TextField editBus = new TextField(10);
	private Checkbox incremental = new Checkbox(); 
	private TextField startID = new TextField(10);
	private TextField manualID = new TextField(10);
	private Label pins = new Label("Pins");
	private int old;
	private int oldid;
	
	
	public ModuleIDProperties(int initial,int initid)
	{
		super();
		this.setLayout(new BorderLayout(0, 15));
		
		this.old = initial;
		this.oldid = initid;
		
		//p = new Panel(new GridBagLayout());
		//c.insets = new Insets(2, 5, 2, 0);
		
		Panel p = new Panel(new BorderLayout());
		p.add(this.pins, BorderLayout.WEST);
		p.add(new SimSeparator(), BorderLayout.CENTER);
		this.add(p, BorderLayout.NORTH);
		
		p = new Panel();
		Panel p2 = new Panel(new FlowLayout(FlowLayout.LEFT, 0, 0));
		p2.add(new Label("Bus Size"));
		p2.add(this.editBus);
		p.add(p2);
		//this.add(p, BorderLayout.CENTER);
		
		//p = new Panel(new FlowLayout(FlowLayout.LEFT, 0, 0));
		p2 = new Panel(new FlowLayout(FlowLayout.LEFT, 0, 0));
		p2.add(new Label("Incremental ID"));
		p2.add(this.incremental);
		//p.add(p2);
		//this.add(p, BorderLayout.CENTER);
		
		//p = new Panel(new FlowLayout(FlowLayout.LEFT, 0, 0));
		//p2 = new Panel(new FlowLayout(FlowLayout.LEFT, 0, 0));
		this.startID.setEnabled(false);
		p2.add(new Label("Starting ID (incremental)"));
		p2.add(this.startID);
		p.add(p2);
		//this.add(p, BorderLayout.CENTER);
		
		//p = new Panel(new FlowLayout(FlowLayout.LEFT, 0, 0));
		p2 = new Panel(new FlowLayout(FlowLayout.LEFT, 0, 0));
		p2.add(new Label("Manual ID"));
		p2.add(this.manualID);
		p.add(p2);
		
		p.setLayout(new BoxLayout(p,BoxLayout.Y_AXIS));
		//p.
		this.add(p, BorderLayout.CENTER);
		
		this.editBus.addActionListener(this);
		this.editBus.setText(Integer.toString(initial));
		
		this.incremental.addItemListener(this);
		if(initid < 0 ){ //negative valid used to indicate incremental id
			this.incremental.setState(true);
			this.startID.setText(Integer.toString(-initid));
			startID.setEnabled(true);
			manualID.setEnabled(false);
		} else {
			this.incremental.setState(false);
			this.manualID.setText(Integer.toString(initid));
			startID.setEnabled(false);
			manualID.setEnabled(true);
		}
	}
	
	public void addNotify()
	{
		super.addNotify();
		this.setSize(500, this.editBus.getPreferredSize().height*7);
	}
	
	/*if (this.incremental.getState()){
		startID.setEnabled(true);
		manualID.setEnabled(false);
	}else{
		startID.setEnabled(false);
		manualID.setEnabled(true);
	}*/
	
	public void itemStateChanged(ItemEvent e){
		if (this.incremental.getState()){
			startID.setEnabled(true);
			manualID.setEnabled(false);
		}else{
			startID.setEnabled(false);
			manualID.setEnabled(true);
		}
	}
	
	public void actionPerformed(ActionEvent e)
	{
		int newBus;
		int newManualID;
		int newStartID;
		
		try
		{
			newBus = Integer.valueOf(this.editBus.getText()).intValue();
			
			if(newBus < 2)
				this.editBus.setText(Integer.toString(this.old));
			else
				this.old = newBus;
			if (this.incremental.getState()) {
				if (this.startID.getText().length() > 0) {
					newStartID = Integer.valueOf(this.startID.getText());
					this.oldid = -newStartID;
				}
			}else{
				newManualID = Integer.valueOf(this.manualID.getText());
				this.oldid = newManualID;
			}
			
		}
		catch(NumberFormatException nfe)
		{
			this.editBus.setText(Integer.toString(this.old));
		}
	}
	
	public int getBusSize()
	{
		int newBus;
		
		try
		{
			newBus = Integer.valueOf(this.editBus.getText()).intValue();
			
			if(newBus >= 2)
				this.old = newBus;
		}
		catch(NumberFormatException nfe)
		{
		}
		
		return this.old;
	}
	
	public int getPinBusSize()
	{
		int newBus;
		
		try
		{
			newBus = Integer.valueOf(this.editBus.getText()).intValue();
			
			if(newBus >= 1)
				this.old = newBus;
		}
		catch(NumberFormatException nfe)
		{
		}
		
		return this.old;
	}
	
	public int getID()
	{
		int newManualID, newStartID;
		
		try
		{
			if (this.incremental.getState()) {
				if (this.startID.getText().length() > 0) {
					newStartID = Integer.valueOf(this.startID.getText());
					return -newStartID;
				}
			}else{
				newManualID = Integer.valueOf(this.manualID.getText());
				return newManualID;
			}
		}
		catch(NumberFormatException nfe)
		{
		}
		
		return 0;
	}
	
	public boolean getIncremental() //true - incremental ID, false - manual ID
	{
		return this.incremental.getState();
	}
	
	public Dimension getPreferredSize()
	{
		return this.getSize();
	}
	
	public Dimension getMinimumSize()
	{
		return this.getSize();
	}

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