using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Project.Dialog
{
    public partial class QuickControl : Form
    {
        public QuickControl()
        {
            InitializeComponent();
            this.FormClosing += __Closing;
            //this.lbTitle.MouseMove += LbTitle_MouseMove;
            //this.lbTitle.MouseUp += LbTitle_MouseUp;
            //this.lbTitle.MouseDown += LbTitle_MouseDown;
        }

        void __Closing(object sender, FormClosingEventArgs e)
        {
            timer1.Stop();
        }
        private void __LoaD(object sender, EventArgs e)
        {
            timer1.Start();
            //button31.Text = ((eAxis)0).ToString();
            //button32.Text = ((eAxis)1).ToString();
            //button33.Text = ((eAxis)2).ToString();
        }
        #region "Mouse Form Move"

        private Boolean fMove = false;
        private Point MDownPos;
        private void LbTitle_MouseMove(object sender, MouseEventArgs e)
        {
            if (fMove)
            {
                Point offset = new Point(e.X - MDownPos.X, e.Y - MDownPos.Y);
                this.Left += offset.X;
                this.Top += offset.Y;
                offset = new Point(0, 0);
            }
        }
        private void LbTitle_MouseUp(object sender, MouseEventArgs e)
        {
            fMove = false;
        }
        private void LbTitle_MouseDown(object sender, MouseEventArgs e)
        {
            MDownPos = new Point(e.X, e.Y);
            fMove = true;
        }

        #endregion
        private void button1_Click(object sender, EventArgs e)
        {
            if (Pub.mot.isHomeSet[(int)eAxis.Z_THETA] == false)
            {
                Util.MsgE("홈 검색이 완료되지 않았으므로 이동할 수 없습니다");
                return;
            }

            var speed = 50.0;
            if (Pub.Result != null &&
                Pub.Result.mModel != null &&
                Pub.Result.mModel.Title != "")
                speed = 0;// Pub.Result.mModel.XSpeed;

            Util_Mot.Move(eAxis.Z_THETA, 0, speed);
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            //감지센서 상태 210208
            button5.BackColor = Util_DO.GetIOInput(eDIName.L_PICK_VAC) ? Color.Lime : Color.FromArgb(70, 70, 70);
            button5.ForeColor = Util_DO.GetIOInput(eDIName.L_PICK_VAC) ? Color.Black : Color.White;

            button6.BackColor = Util_DO.GetIOInput(eDIName.R_PICK_VAC) ? Color.Lime : Color.FromArgb(70, 70, 70);
            button6.ForeColor = Util_DO.GetIOInput(eDIName.R_PICK_VAC) ? Color.Black : Color.White;
        }




        private void button20_Click_1(object sender, EventArgs e)
        {
            //button zero
            var but = sender as Button;
            var motidx = short.Parse(but.Tag.ToString());
            var axis = (eAxis)motidx;

            //전체 이동경로상 오류가 있으면 처리 하지 않는다.
            var ermsg = new System.Text.StringBuilder();

            if (Pub.mot.isHomeSet[(int)axis] == false)
            {
                ermsg.AppendLine("해당 축은 홈 검색이 완료되지 않았습니다");
            }


            if (ermsg.Length > 0)
            {
                Util.MsgE("이동을 할 수 없습니다\n" + ermsg.ToString());
                return;
            }


            var dlg = Util.MsgQ(string.Format("모션 축 ({0})의 위치를 0으로 이동하시겠습까?\n" +
                "위치 0은 일반적으로 홈 위치 입니다", axis));
            if (dlg != DialogResult.Yes) return;
            Pub.log.Add("user:move to zero axis=" + axis.ToString());
            Pub.mot.Move(motidx, 0);
        }

        private void button16_Click_1(object sender, EventArgs e)
        {
            //button stop
            var but = sender as Button;
            var motidx = short.Parse(but.Tag.ToString());
            var axis = (eAxis)motidx;

            //var dlg = Util.MsgQ("모션 {0}({1}) 을 위치 '0'으로 이동 할까요?\n" +
            //    "경로 상 충돌가능성을 확인 한 후 실행하세요");
            //if (dlg != System.Windows.Forms.DialogResult.Yes) return;

            Pub.log.Add("user:motion stop axis=" + axis.ToString());
            Pub.mot.MoveStop("user stop", motidx);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            //ccw
            var but = sender as Button;
            var motidx = short.Parse(but.Tag.ToString());
            var axis = (eAxis)motidx;
            Pub.log.Add("user:motion move -3.0 axis=" + axis.ToString());
            Pub.mot.Move(motidx, -3.0, true);
        }

        private void button4_Click(object sender, EventArgs e)
        {
            //cw
            var but = sender as Button;
            var motidx = short.Parse(but.Tag.ToString());
            var axis = (eAxis)motidx;
            Pub.log.Add("user:motion move +3.0 axis=" + axis.ToString());
            Pub.mot.Move(motidx, 3.0, true);
        }

        private void button40_Click(object sender, EventArgs e)
        {
            var but = sender as Button;
            var butindex = int.Parse(but.Tag.ToString());
            Util_DO.SetPortMotor(butindex, eMotDir.CW, true, "UC");
        }

        private void tbClose_Click_1(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button10_Click_1(object sender, EventArgs e)
        {
            var but = sender as Button;
            var butindex = int.Parse(but.Tag.ToString());
            Util_DO.SetPortMotor(butindex, eMotDir.CCW, true, "UC");
        }

        private void button13_Click_2(object sender, EventArgs e)
        {
            var but = sender as Button;
            var butindex = int.Parse(but.Tag.ToString());
            Util_DO.SetPortMotor(butindex, eMotDir.CCW, false, "UC");
        }

        private void button41_Click(object sender, EventArgs e)
        {
            //front
            var cur = Util_DO.GetIOOutput(eDOName.PICK_VAC1);
            Util_DO.SetPickerVac(!cur, true);
        }

        private void button39_Click(object sender, EventArgs e)
        {

        }

        private void button44_Click(object sender, EventArgs e)
        {
            var cur = Util_DO.GetIOOutput(eDOName.SOL_AIR);
            Util_DO.SetAIR(!cur);
        }

        private void button9_Click(object sender, EventArgs e)
        {
            var but = sender as Button;
            var butindex = int.Parse(but.Tag.ToString());
            Boolean current = false;

            if (butindex == 0) current = Util_DO.GetIOOutput(eDOName.CART_MAG0);
            else if (butindex == 1) current = Util_DO.GetIOOutput(eDOName.CART_MAG1);
            else current = Util_DO.GetIOOutput(eDOName.CART_MAG2);

            Util_DO.SetPortMagnet(butindex, !current);
        }

        private void button51_Click(object sender, EventArgs e)
        {
            var but = sender as Button;
            var tagstr = but.Tag.ToString();
            if (tagstr == "1")
            {
                if (Util_DO.GetIOOutput(eDOName.PRINTL_VACI) == true)
                    Util_DO.SetPrintLVac(ePrintVac.off, true);
                else
                    Util_DO.SetPrintLVac(ePrintVac.inhalation, true);
            }
            else if (tagstr == "2")
                if (Util_DO.GetIOOutput(eDOName.PRINTL_VACO) == true)
                    Util_DO.SetPrintLVac(ePrintVac.off, true);
                else
                    Util_DO.SetPrintLVac(ePrintVac.exhaust, true);
            else
                Util_DO.SetPrintLVac(ePrintVac.off, true);
        }



        private void button53_Click(object sender, EventArgs e)
        {
            var but = sender as Button;
            var tagstr = but.Tag.ToString();
            if (tagstr == "1")
            {
                if (Util_DO.GetIOOutput(eDOName.PRINTR_VACI) == true)
                    Util_DO.SetPrintRVac(ePrintVac.off, true);
                else
                    Util_DO.SetPrintRVac(ePrintVac.inhalation, true);
            }
            else if (tagstr == "2")
                if (Util_DO.GetIOOutput(eDOName.PRINTR_VACO) == true)
                    Util_DO.SetPrintRVac(ePrintVac.off, true);
                else
                    Util_DO.SetPrintRVac(ePrintVac.exhaust, true);
            else
                Util_DO.SetPrintRVac(ePrintVac.off, true);
        }

        private void button57_Click(object sender, EventArgs e)
        {
            //바닥바람
            var curvalu = Util_DO.GetIOOutput(eDOName.PRINTL_AIRON);
            Util_DO.SetOutput(eDOName.PRINTL_AIRON, !curvalu);
        }

        private void button58_Click(object sender, EventArgs e)
        {
            //바닥바람
            var curvalu = Util_DO.GetIOOutput(eDOName.PRINTR_AIRON);
            Util_DO.SetOutput(eDOName.PRINTR_AIRON, !curvalu);
        }

        private void button60_Click(object sender, EventArgs e)
        {
            var cur = Util_DO.GetIOOutput(eDOName.PRINTL_FWD);
            if (cur == false)
            {
                var dlg = Util.MsgQ("프린터(좌) 피커를 전진할까요?");   //210209
                if (dlg != DialogResult.Yes) return;
            }
            Util_DO.SetOutput(eDOName.PRINTL_FWD, !cur);
        }

        private void button59_Click(object sender, EventArgs e)
        {
            var cur = Util_DO.GetIOOutput(eDOName.PRINTR_FWD);
            if (cur == false)
            {
                var dlg = Util.MsgQ("프린터(우) 피커를 전진할까요?");
                if (dlg != DialogResult.Yes) return;
            }
            Util_DO.SetOutput(eDOName.PRINTR_FWD, !cur);
        }

        private void button3_Click_1(object sender, EventArgs e)
        {
            Pub.PrinterL.TestPrint(Pub.setting.DrawOutbox, "ATK4EE1", "", Pub.setting.STDLabelFormat7);
            Pub.log.Add("임시프린트L:" + Pub.PrinterL.LastPrintZPL);
        }

        private void button4_Click_1(object sender, EventArgs e)
        {
            Pub.PrinterR.TestPrint(Pub.setting.DrawOutbox, "ATK4EE1", "", Pub.setting.STDLabelFormat7);
            Pub.log.Add("임시프린트R:" + Pub.PrinterR.LastPrintZPL);
        }

        private void button15_Click(object sender, EventArgs e)
        {
            //왼쪽검증취소
            if (Pub.flag.get(eFlag.RDY_VISION0) == false) return;
            var dlg = Util.MsgQ("QR코드 검증을 취소할까요?");
            if (dlg != DialogResult.Yes) return;

            Pub.flag.set(eFlag.RDY_VISION0, false, "CANCEL");
            Pub.flag.set(eFlag.PORTL_ITEMON, false, "CANCEL");
			Pub.flag.set(eFlag.PRC_VISION0, false, "CANCEL");

            Pub.ResetRunStepSeq(eRunSequence.VISION0);
            Pub.UpdaterunStepSeqStartTime(eRunSequence.COM_VS0);
            Pub.log.Add(string.Format("QR검증({0}) 취소 JGUID={1}", "L", Pub.Result.ItemData[0].guid));
        }

        private void button16_Click(object sender, EventArgs e)
        {
            if (Pub.flag.get(eFlag.RDY_VISION2) == false) return;
            var dlg = Util.MsgQ("QR코드 검증을 취소할까요?");
            if (dlg != DialogResult.Yes) return;

            Pub.flag.set(eFlag.RDY_VISION2, false, "CANCEL");
            Pub.flag.set(eFlag.PORTR_ITEMON, false, "CANCEL");
			Pub.flag.set(eFlag.PRC_VISION2, false, "CANCEL");

			Pub.ResetRunStepSeq(eRunSequence.VISION2);
            Pub.UpdaterunStepSeqStartTime(eRunSequence.COM_VS2);
            Pub.log.Add(string.Format("QR검증({0}) 취소 JGUID={1}", "R", Pub.Result.ItemData[2].guid));
        }

        private void button8_Click(object sender, EventArgs e)
        {
            //왼쪽 -5mm
            var pos = Util_Mot.GetAxPLMPos(eAxisPLMovePos.READY);
            var vel = Pub.setting.MoveYForPaperVaccumeVel;
            var acc = Pub.setting.MoveYForPaperVaccumeAcc;
            Util_Mot.Move(eAxis.PL_MOVE, pos.position + Pub.setting.MoveYForPaperVaccumeValue, vel, acc, false, false, false);
        }

        private void button14_Click(object sender, EventArgs e)
        {
            //왼쪽 +5mm
            var pos = Util_Mot.GetAxPLMPos(eAxisPLMovePos.READY);
            Util_Mot.Move(eAxis.PL_MOVE, pos.position, 50, 200, false, false, false);
        }

        private void button15_Click_1(object sender, EventArgs e)
        {
            //오른쪽 -5mm
            var pos = Util_Mot.GetAxPRMPos(eAxisPRMovePos.READY);
            var vel = Pub.setting.MoveYForPaperVaccumeVel;
            var acc = Pub.setting.MoveYForPaperVaccumeAcc;
            Util_Mot.Move(eAxis.PR_MOVE, pos.position + Pub.setting.MoveYForPaperVaccumeValue, vel, acc, false, false, false);
        }

        private void button16_Click_2(object sender, EventArgs e)
        {
            //오른쪽 +5mm
            var pos = Util_Mot.GetAxPRMPos(eAxisPRMovePos.READY);
            Util_Mot.Move(eAxis.PR_MOVE, pos.position, 50, 200, false, false, false);
        }
    }
}
