package nl.nikhef.safaripark.extra;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JToggleButton;

import nl.nikhef.sfp.SimSFPDevice;

@SuppressWarnings("serial")
public class SimProviderWindow extends JDialog implements ActionListener {

	
	
	private SimSFPDevice _dev;

	private List<JToggleButton> _toggles = new ArrayList<JToggleButton>();
	
	
	public SimProviderWindow(SimSFPDevice dev, JFrame parent) {
		super(parent);		
		setTitle("Sim SFP Control");
		setModal(false);
		setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		_dev = dev;
		
		setLayout(new GridLayout(_dev.getBayCount(), 1));
		
		for (int i = 0; i < _dev.getBayCount(); i++) 
		{
			JToggleButton toggle = new JToggleButton(String.format("Insert into %d", i));
			toggle.addActionListener(this);
			_toggles.add(toggle);
			add(toggle);						
		}
		pack();
		setVisible(true);
	}


	
	@Override
	public void actionPerformed(ActionEvent e) 
	{
		JToggleButton tbut = (JToggleButton) e.getSource();
		int bay = _toggles.indexOf(e.getSource());
		if (_dev.isModulePresent(bay)) 
		{
			_dev.removeModule(bay);
			tbut.setText(String.format("Insert into %d", bay));
			tbut.setSelected(false);
		} else {
			_dev.placeModule(bay);
			tbut.setText(String.format("Remove from %d", bay));
			tbut.setSelected(true);
		}
	}
	
}
