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

namespace Project.Dialog
{
    public partial class fSelectJob : Form
    {
        public fSelectJob()
        {
            InitializeComponent();
            this.KeyPreview = true;
            this.FormClosing += __Closing;
            this.lbTitle.MouseMove += LbTitle_MouseMove;
            this.lbTitle.MouseUp += LbTitle_MouseUp;
            this.lbTitle.MouseDown += LbTitle_MouseDown;
            this.KeyDown += (s1, e1) =>
            {
                if (e1.KeyCode == Keys.Escape) this.Close();
            };

            if (System.Diagnostics.Debugger.IsAttached == false)
            {
                //// = new Dialog.fBlurPanel();
                //fb.Show();
            }

            Pub.flag.set(eFlag.SCR_JOBSELECT, true, "selectjbo");
            Pub.dio.IOValueChanged += Dio_IOValueChanged;


            //this.tableLayoutPanel1.Enabled = false;
            // this.tableLayoutPanel2.Enabled = false;
        }

        private void __LoaD(object sender, EventArgs e)
        {
            //210121  -101SID
            this.chkQty1.Checked = Pub.uSetting.Option_QtyUpdate1;
            //  this.chkForce1.Checked = Pub.uSetting.Option_printforce1;
            this.chkUpdatePart.Checked = Pub.uSetting.Option_PartUpdate;
            this.chkconfirm1.Checked = Pub.uSetting.Option_Confirm1;
            this.chkAutoConfirm.Checked = Pub.uSetting.Option_AutoConfirm;
            this.chkVname.Checked = Pub.uSetting.Option_vname;
            //this.chkfixprint1.Checked = Pub.uSetting.Option_FixPrint1;
            //PrintPos1 = Pub.uSetting.Option_PrintPos1;
            //DisplayPrintPos(PrintPos1);


            //작업형태를 다시 시작해준다. - 210329
            Func_SelectJobType(Pub.Result.JobType2);

            //커스터머 목록을 선택
            cmbCustomer.Items.Clear();
            cmbCustomer.Items.Add("[0000] 선택안함");
            if (Pub.setting.OnlineMode)
            {
                var tacustinfo = new DataSet1TableAdapters.Component_Reel_CustInfoTableAdapter();
                var dtcustinfo = tacustinfo.GetData();
                foreach (var dr in dtcustinfo.Where(t => string.IsNullOrEmpty(t.name) == false))
                {
                    cmbCustomer.Items.Add($"[{dr.code}] {dr.name}");
                }
            }
            cmbCustomer.SelectedIndex = 0;


            //210122  -103SID
            //this.chkQty3.Checked = Pub.uSetting.Option_QtyUpdate3;
            //this.chkForce3.Checked = Pub.uSetting.Option_printforce3;
            //this.chkconfirm3.Checked = Pub.uSetting.Option_Confirm3;
            //this.chkfixprint3.Checked = Pub.uSetting.Option_FixPrint3;
            //PrintPos3 = Pub.uSetting.Option_PrintPos3;
            //DisplayPrintPos(PrintPos3, 3);

        }


        private void Dio_IOValueChanged(object sender, arDev.AzinAxt.DIO.IOValueEventArgs e)
        {
            if (Pub.setting.Enable_ButtonStart)
            {
                var pin = (eDIName)e.ArrIDX;
                if (Util_DO.GetIOInput(pin) == true)
                {
                    if (pin == eDIName.BUT_STARTF) this.Confirm();
                    else if (pin == eDIName.BUT_STOPF || pin == eDIName.BUT_RESETF) this.Cancel();
                }
            }
        }


        void Confirm()
        {
            if (this.InvokeRequired)
                this.BeginInvoke(new MethodInvoker(Confirm), null);
            else
            {
                this.Validate();

                if (this.ModeData.isEmpty() || this.ModeData == "--")
                {
                    Util.MsgE("작업 방식이 선택되지 않았습니다");
                    return;
                }
                if (chkAutoConfirm.Checked && this.chkconfirm1.Checked)
                {
                    Util.MsgE("자동확정과 / 사용자 확인 옵션은 동시에 사용할 수 없습니다");
                    return;
                }
                if (this.ModeData == "--")
                {
                    Util.MsgE("사용할 수 없는 작업방법 입니다");
                    return;
                }


                //커스터머정보 확인
                Pub.Result.CustomerRule.Clear();
                var code = cmbCustomer.Text.Substring(1, 4);
                var name = cmbCustomer.Text.Substring(6).Trim();
                if (Pub.setting.OnlineMode)
                {
                    var taRule = new DataSet1TableAdapters.Component_Reel_CustRuleTableAdapter();
                    var dtRule = taRule.GetData(code);
                    foreach (var dr in dtRule)
                    {
                        if (dr.pre.isEmpty() == false || dr.pos.isEmpty() == false)
                        {
                            Pub.Result.CustomerRule.Add(new CCustRule
                            {
                                Code = dr.code,
                                Name = name,
                                Expression = dr.exp,
                                Len = dr.len,
                                Post = dr.pos,
                                Pre = dr.pre,
                            }); ;
                        }
                    }
                }
                //if (chkfixprint1.Checked && this.PrintPos1.isEmpty())
                //{
                //	Util.MsgE("프린트 위치 고정옵션이 켜져 있으나 위치가 지정되지 않았습니다");
                //	return;
                //}

                //if (chkfixprint3.Checked && this.PrintPos3.isEmpty())
                //{
                //    Util.MsgE("103 SID 의 프린트 위치를 지정하세요");
                //    return;
                //}

                //작업형태 지정
                Pub.Result.JobType2 = this.ModeData;
                Pub.Result.JobFirst = true; //처음시작 220117

                //사용자 정보에 저장한다. 210219 - a모드별로 저장한다.
                Pub.uSetting.Xml.set_Data("M" + ModeData, "qtysync", chkQty1.Checked ? "1" : "0");
                Pub.uSetting.Xml.set_Data("M" + ModeData, "qtyman", chkQtyM.Checked ? "1" : "0");
                Pub.uSetting.Xml.set_Data("M" + ModeData, "confirm", chkconfirm1.Checked ? "1" : "0");
                Pub.uSetting.Xml.set_Data("M" + ModeData, "partsync", chkUpdatePart.Checked ? "1" : "0");
                Pub.uSetting.Xml.set_Data("M" + ModeData, "newid", chkNew.Checked ? "1" : "0");
                Pub.uSetting.Xml.set_Data("M" + ModeData, "vname", chkVname.Checked ? "1" : "0");
                Pub.uSetting.Xml.set_Data("M" + ModeData, "autoconf", this.chkAutoConfirm.Checked ? "1" : "0");

                Pub.uSetting.Save();

                //옵션사항 설정 210121
                //if (ModeData == "DRY") Pub.setting.DryRun = true;
                Pub.Result.Option_vname = chkVname.Checked;
                Pub.Result.Option_Confirm1 = chkconfirm1.Checked;
                Pub.Result.Option_QtyUpdate1 = chkQty1.Checked;
                Pub.Result.Option_QtyUpdateM = chkQtyM.Checked;
                Pub.Result.Option_partUpdate = chkUpdatePart.Checked;
                Pub.Result.Option_NewReelID = chkNew.Checked;
                Pub.Result.Option_AutoConf = chkAutoConfirm.Checked;
                this.DialogResult = DialogResult.OK;
            }

        }
        void Cancel()
        {
            if (this.InvokeRequired)
                this.BeginInvoke(new MethodInvoker(Cancel), null);
            else
                this.Close();
        }

        void __Closing(object sender, FormClosingEventArgs e)
        {
            // if (fb != null) fb.Dispose();
            Pub.dio.IOValueChanged -= Dio_IOValueChanged;
            Pub.flag.set(eFlag.SCR_JOBSELECT, false, "selectjbo");
        }

        public string GetCntStr(uint cnt)
        {
            if (cnt > 1000000)
            {
                return (cnt * 1.0 / 1000000.0).ToString("N1") + "M";

            }
            else if (cnt > 1000)
            {
                return (cnt * 1.0 / 1000).ToString("N1") + "K";
            }
            else return cnt.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 tbClose_Click_1(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btOK_Click(object sender, EventArgs e)
        {
            Confirm();
        }

        private void btCancle_Click(object sender, EventArgs e)
        {
            Cancel();
        }



        private void arLabel1_Click(object sender, EventArgs e)
        {



        }

        private void arLabel5_Click(object sender, EventArgs e)
        {
            var lbl = sender as arCtl.arLabel;
            var lblValue = int.Parse(lbl.Tag.ToString());

        }


        private void arLabel18_Click(object sender, EventArgs e)
        {

        }

        private void checkBox4_CheckedChanged(object sender, EventArgs e)
        {
            //선택에 따라서 라디오 박스 업데이트
            //tableLayoutPanel1.Enabled = chkfixprint1.Checked;
            var chk = sender as CheckBox;
            //label26.Text = this.chkfixprint1.Checked ? "부착위치(지정)" : "부착위치(자동)";

        }



        //string PrintPos1 = string.Empty;
        //string PrintPos3 = string.Empty;
        private void pb7_Click(object sender, EventArgs e)
        {
            //부착위치
            var lb = sender as Label;
            //PrintPos1 = lb.Tag.ToString();
            //DisplayPrintPos(PrintPos1);
            //this.radO1.Checked = false; //직접선택했으니 해제 한다
            //label26.Text = this.chkfixprint1.Checked ? "부착위치(지정)" : "부착위치(자동)";
        }
        private void label6_Click(object sender, EventArgs e)
        {
            //부착위치
            var lb = sender as Label;
            //PrintPos3 = lb.Tag.ToString();
            //DisplayPrintPos(PrintPos3, 3);
            //this.radO3.Checked = false; //직접선택했으니 해제 한다

            //label10.Text = this.chkfixprint3.Checked ? "부착위치(지정)" : "부착위치(자동)";
        }
        //void DisplayPrintPos(string v)
        //{
        //	Label[] lbs = null;
        //	lbs = new Label[] { pb7, pb8, pb9, pb4, pb5, pb6, pb1, pb2, pb3 };
        //	//else lbs = new Label[] { pc7, pc8, pc9, pc4, pc5, pc6, pc1, pc2, pc3 };

        //	foreach (Label item in lbs)
        //		if (item.Tag.ToString() == v) item.BackColor = Color.Blue;
        //		else item.BackColor = Color.FromArgb(64, 64, 64);
        //}

        private void btMix_Click(object sender, EventArgs e)
        {
            var lb = sender as arCtl.arLabel;
            Func_SelectJobType(lb.Tag.ToString());
        }

        arCtl.arLabel[] btsMOD;
        string ModeData = string.Empty;
        void Func_SelectJobType(string mode)
        {
            ModeData = mode;

            if (btsMOD == null)
                btsMOD = new arCtl.arLabel[] { bt33, bt61, bt13, bt31, btRe, btManual };

            foreach (var lb in btsMOD)
            {
                if (lb.Tag.ToString() == mode)
                {
                    lb.BackColor = Color.Green;
                    lb.BackColor2 = Color.Lime;
                }
                else
                {
                    lb.BackColor = Color.FromArgb(0, 0, 0);
                    lb.BackColor2 = Color.FromArgb(150, 150, 150);
                }
            }

            //각 모드별 옵션값 확인
            if (mode == "M")    //manual
            {
                this.chkQty1.Checked = false;
                this.chkconfirm1.Checked = true;
                this.chkNew.Checked = false;
                this.chkUpdatePart.Checked = false;
                this.chkAutoConfirm.Checked = false;
                this.chkQtyM.Checked = false;
                groupBox1.Enabled = true;
            }
            else if (mode == "DRY")
            {
                this.chkQty1.Checked = false;
                this.chkconfirm1.Checked = false;
                this.chkNew.Checked = false;
                this.chkUpdatePart.Checked = false;
                this.chkAutoConfirm.Checked = false;
                this.chkQtyM.Checked = false;
                groupBox1.Enabled = false;
            }
            else
            {
                this.chkQty1.Checked = Pub.uSetting.Xml.get_Data("M" + ModeData, "qtysync") == "1";
                this.chkUpdatePart.Checked = Pub.uSetting.Xml.get_Data("M" + ModeData, "partsync") == "1";
                this.chkQtyM.Checked = Pub.uSetting.Xml.get_Data("M" + ModeData, "qtyman") == "1";
                this.chkconfirm1.Checked = Pub.uSetting.Xml.get_Data("M" + ModeData, "confirm") == "1";
                //this.PrintPos1 = Pub.uSetting.Xml.get_Data("M" + ModeData, "printpos");
                this.chkNew.Checked = Pub.uSetting.Xml.get_Data("M" + ModeData, "newid") == "1";
                this.chkAutoConfirm.Checked = Pub.uSetting.Xml.get_Data("M" + ModeData, "autoconf") == "1";
                this.chkVname.Checked = Pub.uSetting.Xml.get_Data("M" + ModeData, "vname") == "1";
                this.groupBox1.Enabled = true;
            }

            //DisplayPrintPos(this.PrintPos1);
            //메뉴얼은 확인창을 꼭 사용한다
        }
    }
}
