using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using AR;
using arDev;

namespace Project
{
	public partial class FMain
	{

		private void Joystick_InputChanged(object sender, arDev.Joystick.JoystickRaw.InputChangedEventHandler e)
		{

			var keyName = e.input.ToString();
			if (e.input == HidSharp.Reports.Usage.Button5) keyName = "L-TRIGGER";
			else if (e.input == HidSharp.Reports.Usage.Button6) keyName = "R-TRIGGER";
			else if (e.input == HidSharp.Reports.Usage.Button9) keyName = "SELECT";
			else if (e.input == HidSharp.Reports.Usage.Button10) keyName = "START";
			else if (e.input == HidSharp.Reports.Usage.Button4) keyName = "BUT-Y";
			else if (e.input == HidSharp.Reports.Usage.Button1) keyName = "BUT-X";

			PUB.log.Add("JOYSTICK", string.Format("[{0}] {1}->{2}", keyName, e.oldValue, e.newValue));

			if (PUB.sm.Step != eSMStep.IDLE && PUB.sm.Step != eSMStep.ERROR && PUB.flag.get(eVarBool.FG_MOVE_PICKER) == false && PUB.sm.Step != eSMStep.WAITSTART) return;

			if (e.input == HidSharp.Reports.Usage.Button4) //stop
			{
				if (e.newValue > 0)
				{
					PUB.log.Add("Joystick Click : Stop");
					_BUTTON_STOP();
				}
			}
			else if (e.input == HidSharp.Reports.Usage.Button1) //reset
			{
				if (e.newValue > 0)
				{
					PUB.log.Add("Joystick Click : Reset");
					_BUTTON_RESET();
				}
			}
			else if (e.input == HidSharp.Reports.Usage.Button9)  //select button
			{
				if (e.newValue == 1)
				{
					PUB.Result.JoystickAxisGroup += 1;
					if (PUB.Result.JoystickAxisGroup > 3) PUB.Result.JoystickAxisGroup = 0;
				}
			}
			else if (e.input == HidSharp.Reports.Usage.Button10)    //start button
			{
				if (e.newValue == 1 && PUB.flag.get(eVarBool.FG_SCR_JOBSELECT) == false)
				{
					if (PUB.sm.Step >= eSMStep.IDLE)
					{
						PUB.log.AddI("START BUTTON");
						this._BUTTON_START();
					}
				}
			}
			else if (e.input == HidSharp.Reports.Usage.Button5)  //ltrigger
			{
				AR.SETTING.Data.Enable_SpeedLimit = true;
			}
			else if (e.input == HidSharp.Reports.Usage.Button6)  //ltrigger
			{
				AR.SETTING.Data.Enable_SpeedLimit = false;
			}

			//트리거 L,R을 눌렀을때 홈 가능한 위치라면 홈을 진행한다
			if (e.newValue > 0 && (e.input == HidSharp.Reports.Usage.Button5 || e.input == HidSharp.Reports.Usage.Button6))
			{
				//두개가 동시에 눌렷을때
				if (PUB.joystick.Buttons[4] && PUB.joystick.Buttons[5])
				{
					if (PUB.flag.get(eVarBool.FG_INIT_MOTIO) == false) return;
					if (PUB.sm.isRunning == true) return;
					if (DIO.IsEmergencyOn() == true) return;
					if (PUB.sm.Step == eSMStep.RUN) return;

					PUB.log.AddAT("조이스틱으로 홈 초기화작업");
					Func_sw_initialize();
				}
			}

			//동작중만 아니면 작동하게한다.
			if (PUB.sm.Step != eSMStep.RUN)
			{
				short axisH = 0;
				short axisV = 1;
				if (PUB.Result.JoystickAxisGroup == 1) //왼쪽뭉치
				{
					axisH = 2;
					axisV = 3;
				}
				else if (PUB.Result.JoystickAxisGroup == 2) //오른쪽뭉치
				{
					axisH = 4;
					axisV = 5;
				}
				else if (PUB.Result.JoystickAxisGroup == 3) //theta, picker-Z
				{
					axisH = 6;
					axisV = 1;
				}

				//X축으로 이동하는것은 PX,                
				if (e.input == HidSharp.Reports.Usage.GenericDesktopX)
				{
					if (e.newValue == 0)
					{
						//좌
						PUB.mot.JOG(axisH, arDev.MOT.MOTION_DIRECTION.Negative, 100, 500, false, false);
						PUB.flag.set(eVarBool.FG_JOYSTICK, true, "joystick");
					}
					else if (e.newValue == 127)
					{
						//멈춤
						PUB.mot.MoveStop("Joystick", axisH);
						PUB.flag.set(eVarBool.FG_JOYSTICK, false, "joystick");
					}
					else if (e.newValue == 255)
					{
						//우
						PUB.mot.JOG(axisH, arDev.MOT.MOTION_DIRECTION.Positive, 100, 500, false, false);
						PUB.flag.set(eVarBool.FG_JOYSTICK, true, "joystick");
					}
				}
				else if (e.input == HidSharp.Reports.Usage.GenericDesktopY)
				{
					if (e.newValue == 0)
					{
						//상
						PUB.mot.JOG(axisV, arDev.MOT.MOTION_DIRECTION.Negative, 100, 500, false, false);
						PUB.flag.set(eVarBool.FG_JOYSTICK, true, "joystick");
					}
					else if (e.newValue == 127)
					{
						//멈춤
						PUB.mot.MoveStop("Joystick", axisV);
						PUB.flag.set(eVarBool.FG_JOYSTICK, false, "joystick");
					}
					else if (e.newValue == 255)
					{
						//하
						PUB.mot.JOG(axisV, arDev.MOT.MOTION_DIRECTION.Positive, 100, 500, false, false);
						PUB.flag.set(eVarBool.FG_JOYSTICK, true, "joystick");
					}
				}

			}

		}

		private void Joystick_Disconnected(object sender, EventArgs e)
		{
			PUB.log.AddE("조이스틱 연결 종료");
		}

		private void Joystick_Connected(object sender, EventArgs e)
		{
			PUB.log.AddI("조이스틱 연결 완료");
		}

		private void Joystick_Changed(object sender, EventArgs e)
		{
			PUB.log.AddAT("조이스틱 감지");
		}

	}
}
