import javax.swing.*;
import java.awt.event.*;
import de.uni_paderborn.fujaba.umlrt.realtimestatechart.sdm.*;
import de.uni_paderborn.fujaba.umlrt.realtimestatechart.sdm.rt.*;
import components.Shuttle1.*;

public class CognitiveOperator extends JFrame implements ActionListener{
	
	public static void main(String[] args)
	{
		CognitiveOperator co = new CognitiveOperator(new Shuttle1(), new Shuttle1());
		co.setTitle("Cognitive Operator");
		co.setSize(280, 250);
		co.setVisible(true);
		co.run();
	}
	
	private JButton buildConvoy;
	private JButton breakConvoy;
	private JButton refresh;
	private JLabel rearStateLabel;
	private JLabel frontStateLabel;
	private Shuttle1 rearRole;
	private Shuttle1 frontRole;
	
	public CognitiveOperator(Shuttle1 rearRole, Shuttle1 frontRole)
	{
		this.rearRole = rearRole;
		this.frontRole = frontRole;
		rearRole.setFrontRole(frontRole);
		JPanel panel = new JPanel();
		buildConvoy = new JButton ("Set convoyUseful to true at rear role");
		buildConvoy.addActionListener(this); 
		panel.add(buildConvoy);
		breakConvoy = new JButton ("Set convoyUseful to false at front role");
		breakConvoy.addActionListener(this); 
		panel.add(breakConvoy);
		
		JLabel label = new JLabel ("State of rear Role: ");
		panel.add (label);
		rearStateLabel = new JLabel(getState(rearRole));
		panel.add (rearStateLabel);
		label = new JLabel ("State of front Role: ");
		panel.add (label);
		frontStateLabel = new JLabel(getState(frontRole));
		panel.add (frontStateLabel);
		refresh = new JButton("Refresh");
		refresh.addActionListener(this);
		panel.add(refresh);
		
		getContentPane().add(panel);
		
		FStarter starter = new FStarter ();
		starter.setHandler(rearRole);
		starter.start (); 
		starter = new FStarter ();
		starter.setHandler(frontRole);
		starter.start ();
	}
	
	public void run()
	{
		while (true)
		{
			rearStateLabel.setText(getState(rearRole));
			frontStateLabel.setText(getState(frontRole));
			rearStateLabel.setVisible(false);
			rearStateLabel.setVisible(true);
			frontStateLabel.setVisible(false);
			frontStateLabel.setVisible(true);
			try
			{
				Thread.sleep(500);
			} catch (InterruptedException e) {e.printStackTrace();}
		}
	}
	
	public void actionPerformed(ActionEvent e) 
	{
		if (e.getSource() == buildConvoy)
		{
			rearRole.setConvoyUseful(true);
		}
		else if (e.getSource() == breakConvoy)
		{
			frontRole.setConvoyUseful(false);
		}
		else if (e.getSource() == refresh)
		{
			rearStateLabel.setText(getState(rearRole));
			frontStateLabel.setText(getState(frontRole));
			this.setVisible(false);
			this.setVisible(true);
		}
	}
	
	private String getState(Shuttle1 shuttle)
	{
		Matrix state = shuttle.getCurrentState();
		String s1 = "null";
		String s2 = "null";
		String s3 = "null";
		for (int col = 0; col < state.getNumberOfCols(); col++)
		{
		  switch(state.get(1, col))
		  {
			case Shuttle1.F_STATE_Shuttle1_myFrontRole_answer    : s1 = "answer"; break;
			case Shuttle1.F_STATE_Shuttle1_myFrontRole_noConvoy : s1 = "noConvoy"; break;
			case Shuttle1.F_STATE_Shuttle1_myFrontRole_convoy    : s1 = "convoy"; break;
		  }
		}

		for (int col = 0; col < state.getNumberOfCols(); col++)
		{
		  switch(state.get(1, col))
		  {
			case Shuttle1.F_STATE_Shuttle1_Shuttle_Main_convoyFront : s2 = "convoyFront"; break;
			case Shuttle1.F_STATE_Shuttle1_Shuttle_Main_noConvoy   : s2 = "noConvoy"; break;
			case Shuttle1.F_STATE_Shuttle1_Shuttle_Main_convoyRear  : s2 = "convoyRear"; break;
			
		  }
		}

		for (int col = 0; col < state.getNumberOfCols(); col++)
		{
		  switch(state.get(1, col))
		  {
			case Shuttle1.F_STATE_Shuttle1_myRearRole_convoy   : s3 = "convoy"; break;
			case Shuttle1.F_STATE_Shuttle1_myRearRole_noConvoy : s3 = "noConvoy"; break;
			case Shuttle1.F_STATE_Shuttle1_myRearRole_wait     : s3 = "wait"; break;
		  }
		}
		
		//System.out.println(shuttle.getCurrentState());
		//System.out.println(state.get(1,0) + "_" + state.get(1,1) + "_" + state.get(1,3));
		
		return "(" + s1 + ", " + s2 + ", " + s3 + ")";
	}

}

