package jp.co.yaskawa.rtc.integratedMotionClient.ui;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

import org.omg.CORBA.IntHolder;
import org.omg.CORBA.StringHolder;

import jp.co.yaskawa.rtc.integratedMotionClient.toController.IntegratedMotionInterface;
import jp.co.yaskawa.rtc.integratedMotionClient.toController.ReturnID;

@SuppressWarnings("serial")
public class ControllPanel extends JPanel {

	final String BUTTON1_TOOLTIP = "";
	final String BUTTON2_TOOLTIP = "";
	final String BUTTON3_TOOLTIP = "";
	
	IntegratedMotionInterface imInterface_;
	MonitorPanel monitor_;
	
	public ControllPanel(IntegratedMotionInterface imif) {
		super();
		imInterface_ = imif;
		setup();
	}
	
	public void setController(IntegratedMotionInterface imif) {
		imInterface_ = imif;
	}
	
	public void setMonitor(MonitorPanel monitor) {
		monitor_ = monitor;
	}

	
	void setup() {
		this.setBorder(BorderFactory.createTitledBorder(""));
		this.setLayout(new FlowLayout(FlowLayout.LEFT));
		
		JButton abortButton = new JButton("abort");
		JButton pauseButton = new JButton("Pause");
		JButton resumeButton = new JButton("Resume");
		
		abortButton.addActionListener(new AbortAction());
		pauseButton.addActionListener(new PauseAction());
		resumeButton.addActionListener(new ResumeAction());
		
		JButton button1 = new JButton("getState");
		JButton button2 = new JButton("getVersion");
		JButton button3 = new JButton("clearAlarm");
		
		button1.addActionListener(new Button1Action());
		button2.addActionListener(new Button2Action());
		button3.addActionListener(new Button3Action());
		
		button1.setToolTipText(BUTTON1_TOOLTIP);
		button2.setToolTipText(BUTTON2_TOOLTIP);
		button3.setToolTipText(BUTTON3_TOOLTIP);
		
		this.add(pauseButton);
		this.add(resumeButton);
		this.add(abortButton);
		this.add(new JLabel("       "));
		this.add(button1);
		this.add(button2);
		this.add(button3);
	}
	
	class AbortAction implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent e) {
			imInterface_.abort();
		}
	}
	
	class PauseAction implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent e) {
			imInterface_.pause();
		}
	}
	
	class ResumeAction implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent e) {
			imInterface_.resume();
		}
	}
	
	class Button1Action implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent e) {
			IntHolder statusId = new IntHolder();
			StringHolder message = new StringHolder();
			imInterface_.getState(statusId, message);
		}
	}
	
	class Button2Action implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent e) {
			StringHolder version = new StringHolder();			
			ReturnID rtn = imInterface_.getVersion(version);
			if(rtn.id == 0){
				monitor_.setMessage("[getVersion]: OK" + version.value);
			}
			else
			{
				monitor_.setMessage("[getVersion]: NG" + Integer.toString(rtn.id));				
			}
		}
	}
	
	class Button3Action implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent e) {
			ReturnID rtn = imInterface_.clearAlarms();
			monitor_.setMessage("[clearAlarm] " + "id: " + Integer.toString(rtn.id) + " comment: " + rtn.comment);				
		}
	}

}
