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;
using AR;

namespace Project
{
    public partial class fSetting : Form
    {
        CommonSetting dummySetting;   //Temporarily store settings and overwrite on completion

        public fSetting()
        {
            InitializeComponent();

            //setting
            dummySetting = new CommonSetting();
            AR.SETTING.Data.CopyTo(dummySetting);

            this.KeyDown += (s1, e1) =>
            {
                if (e1.KeyCode == Keys.Escape)
                    this.Close();
                if (DateTime.Now > PUB.LastInputTime) PUB.LastInputTime = DateTime.Now;
            };
            this.MouseMove += (s1, e1) => { if (DateTime.Now > PUB.LastInputTime) PUB.LastInputTime = DateTime.Now; };
            this.FormClosed += __Closed;

            bsRecipient.DataSource = PUB.mailList;
            bsMailForm.DataSource = PUB.mailForm;
        }

        private void __Closed(object sender, FormClosedEventArgs e)
        {

        }

        private void __Load(object sender, EventArgs e)
        {
            this.Show();

            this.propertyGrid1.SelectedObject = this.dummySetting;
            this.propertyGrid1.Refresh();

            btBuz.BackColor = dummySetting.Disable_Buzzer == false ? Color.Lime : Color.Tomato;
            this.btTWLamp.BackColor = dummySetting.Disable_TowerLamp ? Color.Tomato : Color.Lime;
            this.btRoomLamp.BackColor = dummySetting.Disable_RoomLight == true ? Color.Tomato : Color.Lime;

            //Vacuum usage status
            this.btLeftVac.BackColor = dummySetting.Disable_PLVac ? Color.Tomato : Color.Lime;
            this.btRightVac.BackColor = dummySetting.Disable_PRVac ? Color.Tomato : Color.Lime;
            this.btPickerVac.BackColor = dummySetting.Disable_PKVac ? Color.Tomato : Color.Lime;

            //Port usage status
            this.btPort0.BackColor = dummySetting.Disable_PortL ? Color.Tomato : Color.Lime;
            this.btPort1.BackColor = dummySetting.Disable_PortC ? Color.Tomato : Color.Lime;
            this.btPort2.BackColor = dummySetting.Disable_PortR ? Color.Tomato : Color.Lime;

            //Printer usage status
            this.btPrintL.BackColor = dummySetting.Disable_PrinterL ? Color.Tomato : Color.Lime;
            this.btPrintR.BackColor = dummySetting.Disable_PrinterR ? Color.Tomato : Color.Lime;

            //Unloader QR validation
            this.button3.BackColor = dummySetting.Enable_Unloader_QRValidation ? Color.Lime : Color.Tomato;

            //Card detection sensor
            this.btCartDetL.BackColor = dummySetting.Detect_CartL ? Color.Lime : Color.Tomato;
            this.btCartDetC.BackColor = dummySetting.Detect_CartC ? Color.Lime : Color.Tomato;
            this.btCartDetR.BackColor = dummySetting.Detect_CartR ? Color.Lime : Color.Tomato;

            //Magnet usage
            this.btmag0.BackColor = dummySetting.Enable_Magnet0 ? Color.Lime : Color.Tomato;
            this.btmag1.BackColor = dummySetting.Enable_Magnet1 ? Color.Lime : Color.Tomato;
            this.btmag2.BackColor = dummySetting.Enable_Magnet2 ? Color.Lime : Color.Tomato;

            //Print paper detection
            this.btDetectPrintL.BackColor = dummySetting.Detect_PrintL ? Color.Lime : Color.Tomato;
            this.btDetectPrintR.BackColor = dummySetting.Detect_PrintR ? Color.Lime : Color.Tomato;

            //Function usage
            this.button5.BackColor = dummySetting.Disable_Left == false ? Color.Lime : Color.Tomato;
            this.button6.BackColor = dummySetting.Disable_Right == false ? Color.Lime : Color.Tomato;
            //this.button9.BackColor = dummySetting.Enable_RQAuto ? Color.Lime : Color.Tomato;

            this.button13.BackColor = dummySetting.Disable_PLAir == false ? Color.Lime : Color.Tomato;
            this.button14.BackColor = dummySetting.Disable_PRAir == false ? Color.Lime : Color.Tomato;
            this.btbuzAfterFinish.BackColor = dummySetting.Force_JobEndBuzzer ? Color.Gold : SystemColors.Control;
            this.button7.BackColor = dummySetting.Enable_PickerCylinder ? Color.Lime : Color.Tomato;
            this.btSystemBypass.BackColor = dummySetting.SystemBypass ? Color.DarkBlue : Color.Transparent;
            this.btSystemBypass.ForeColor = dummySetting.SystemBypass ? Color.White : Color.Black;
        }

        private void button1_Click(object sender, EventArgs e)
        {

            if (PUB.PasswordCheck() == false)
            {
                UTIL.MsgE("Password incorrect");
                return;
            }
            //check convmode  - disable port0/2
            //if (dummySetting.Enable_ConveryorMode)
            //{
            //    if (dummySetting.Disable_PortL == false)
            //    {
            //        UTIL.MsgE("컨베이어 사용시에는 포트(L/R)을 사용할 수 없습니다");
            //        return;
            //    }
            //    if (dummySetting.Disable_PortR == false)
            //    {
            //        UTIL.MsgE("컨베이어 사용시에는 포트(L/R)을 사용할 수 없습니다");
            //        return;
            //    }
            //}

            var ChangeL = dummySetting.Disable_Left != AR.SETTING.Data.Disable_Left;
            var ChangeR = dummySetting.Disable_Right != AR.SETTING.Data.Disable_Right;
            if (ChangeL || ChangeR)
            {
                UTIL.MsgI("Left/Right usage options will be applied after restarting the job");
            }

            this.Invalidate();
            var chTable = PUB.userList.GetChanges();
            if (chTable != null)
            {
                string fn = UTIL.MakePath("Data", "users.xml");
                PUB.userList.WriteXml(fn, true);
                PUB.userList.AcceptChanges();
            }

            bsRecipient.EndEdit();
            this.Validate();
            var recpTable = PUB.mailList.GetChanges();
            if (recpTable != null)
            {
                string fn = AppDomain.CurrentDomain.BaseDirectory + "mailList.xml";

                PUB.mailList.WriteXml(fn, true);
                PUB.mailList.AcceptChanges();
            }
            bsMailForm.EndEdit();
            var formTable = PUB.mailForm.GetChanges();
            if (formTable != null)
            {
                string fn = AppDomain.CurrentDomain.BaseDirectory + "mailForm.xml";
                PUB.mailForm.WriteXml(fn, true);
                PUB.mailForm.AcceptChanges();
            }





            try
            {
                dummySetting.CopyTo(SETTING.Data);
                SETTING.Save();
                PUB.log.AddI("Setting Save");
                PUB.log.Add(SETTING.Data.ToString());
            }
            catch (Exception ex)
            {
                PUB.log.AddE("Setting Save Error:" + ex.Message);
                UTIL.MsgE("Error\n" + ex.Message + "\n\nPlease try again");
            }

            //PUB.flag.set(eVarBool.TestRun, btLoaderDetect.BackColor == Color.Lime);
            DialogResult = DialogResult.OK;
        }


        private void button6_Click(object sender, EventArgs e)
        {
            dummySetting.Disable_Buzzer = !dummySetting.Disable_Buzzer;
            btBuz.BackColor = dummySetting.Disable_Buzzer ? Color.Tomato : Color.Lime;
        }



        private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
        {
         
        }


        private void btTWLamp_Click(object sender, EventArgs e)
        {
            var but = sender as Button;
            dummySetting.Disable_TowerLamp = !dummySetting.Disable_TowerLamp;
            but.BackColor = dummySetting.Disable_TowerLamp ? Color.Tomato : Color.Lime;
        }



        private void button2_Click_2(object sender, EventArgs e)
        {
            var but = sender as Button;
            dummySetting.Disable_RoomLight = !dummySetting.Disable_RoomLight;
            but.BackColor = dummySetting.Disable_RoomLight == true ? Color.Tomato : Color.Lime;
        }

     

        private void btLeftVac_Click(object sender, EventArgs e)
        {
            var but = sender as Button;
            dummySetting.Disable_PLVac = !dummySetting.Disable_PLVac;
            but.BackColor = dummySetting.Disable_PLVac ? Color.Tomato : Color.Lime;
        }

        private void btRightVac_Click(object sender, EventArgs e)
        {
            var but = sender as Button;
            dummySetting.Disable_PRVac = !dummySetting.Disable_PRVac;
            but.BackColor = dummySetting.Disable_PRVac ? Color.Tomato : Color.Lime;
        }

        private void btPickerVac_Click(object sender, EventArgs e)
        {
            var but = sender as Button;
            dummySetting.Disable_PKVac = !dummySetting.Disable_PKVac;
            but.BackColor = dummySetting.Disable_PKVac ? Color.Tomato : Color.Lime;
        }

        private void btPort0_Click(object sender, EventArgs e)
        {
            var but = sender as Button;
            dummySetting.Disable_PortL = !dummySetting.Disable_PortL;
            but.BackColor = dummySetting.Disable_PortL ? Color.Tomato : Color.Lime;
        }

        private void btPort1_Click(object sender, EventArgs e)
        {
            var but = sender as Button;
            dummySetting.Disable_PortC = !dummySetting.Disable_PortC;
            but.BackColor = dummySetting.Disable_PortC ? Color.Tomato : Color.Lime;
        }

        private void btPort2_Click(object sender, EventArgs e)
        {
            var but = sender as Button;
            dummySetting.Disable_PortR = !dummySetting.Disable_PortR;
            but.BackColor = dummySetting.Disable_PortR ? Color.Tomato : Color.Lime;
        }

        private void button4_Click_2(object sender, EventArgs e)
        {
            var but = sender as Button;
            dummySetting.Disable_PrinterL = !dummySetting.Disable_PrinterL;
            but.BackColor = dummySetting.Disable_PrinterL ? Color.Tomato : Color.Lime;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            var but = sender as Button;
            dummySetting.Disable_PrinterR = !dummySetting.Disable_PrinterR;
            but.BackColor = dummySetting.Disable_PrinterR ? Color.Tomato : Color.Lime;
        }

        private void button3_Click_1(object sender, EventArgs e)
        {
            var but = sender as Button;
            dummySetting.Enable_Unloader_QRValidation = !dummySetting.Enable_Unloader_QRValidation;
            but.BackColor = dummySetting.Enable_Unloader_QRValidation == false ? Color.Tomato : Color.Lime;
        }

        private void button6_Click_1(object sender, EventArgs e)
        {
            //카트감지l
            var but = sender as Button;
            dummySetting.Detect_CartL = !dummySetting.Detect_CartL;
            but.BackColor = dummySetting.Detect_CartL == false ? Color.Tomato : Color.Lime;
        }

        private void button7_Click(object sender, EventArgs e)
        {
            //카드감지c
            var but = sender as Button;
            dummySetting.Detect_CartC = !dummySetting.Detect_CartC;
            but.BackColor = dummySetting.Detect_CartC == false ? Color.Tomato : Color.Lime;
        }

        private void button5_Click(object sender, EventArgs e)
        {
            //카트감지r
            var but = sender as Button;
            dummySetting.Detect_CartR = !dummySetting.Detect_CartR;
            but.BackColor = dummySetting.Detect_CartR == false ? Color.Tomato : Color.Lime;
        }

        private void button7_Click_1(object sender, EventArgs e)
        {
            var but = sender as Button;
            dummySetting.Enable_Magnet0 = !dummySetting.Enable_Magnet0;
            but.BackColor = dummySetting.Enable_Magnet0 == false ? Color.Tomato : Color.Lime;
        }

        private void button6_Click_2(object sender, EventArgs e)
        {
            var but = sender as Button;
            dummySetting.Enable_Magnet1 = !dummySetting.Enable_Magnet1;
            but.BackColor = dummySetting.Enable_Magnet1 == false ? Color.Tomato : Color.Lime;
        }

        private void button5_Click_1(object sender, EventArgs e)
        {
            var but = sender as Button;
            dummySetting.Enable_Magnet2 = !dummySetting.Enable_Magnet2;
            but.BackColor = dummySetting.Enable_Magnet2 == false ? Color.Tomato : Color.Lime;
        }

        private void btDetectPrintL_Click(object sender, EventArgs e)
        {
            var but = sender as Button;
            dummySetting.Detect_PrintL = !dummySetting.Detect_PrintL;
            but.BackColor = dummySetting.Detect_PrintL == false ? Color.Tomato : Color.Lime;
        }

        private void btDetectPrintR_Click(object sender, EventArgs e)
        {
            var but = sender as Button;
            dummySetting.Detect_PrintR = !dummySetting.Detect_PrintR;
            but.BackColor = dummySetting.Detect_PrintR == false ? Color.Tomato : Color.Lime;
        }

        private void button5_Click_2(object sender, EventArgs e)
        {
            //기능-좌
            var but = sender as Button;
            dummySetting.Disable_Left = !dummySetting.Disable_Left;
            but.BackColor = dummySetting.Disable_Left == true ? Color.Tomato : Color.Lime;
        }

        private void button6_Click_3(object sender, EventArgs e)
        {
            //기능-우
            var but = sender as Button;
            dummySetting.Disable_Right = !dummySetting.Disable_Right;
            but.BackColor = dummySetting.Disable_Right == true ? Color.Tomato : Color.Lime;
        }



        private void btOpenZPL_Click(object sender, EventArgs e)
        {
            var fi = new System.IO.FileInfo( UTIL.MakePath("data","zpl.txt"));
            if (fi.Exists == false)
            {
                //System.IO.File.WriteAllText(fi.FullName, Properties.Settings.Default.ZPL7, System.Text.Encoding.Default);
                UTIL.MsgI("New ZPL file has been created\n" + fi.FullName);
            }
            using (var f = new Dialog.fZPLEditor(fi.FullName))
                f.ShowDialog();
        }


        private void button11_Click(object sender, EventArgs e)
        {
            using (var f = new fSetting_ErrorMessage())
                f.ShowDialog();
        }

        private void button12_Click(object sender, EventArgs e)
        {
            using (var f = new fSetting_IOMessage())
                f.ShowDialog();
        }

        private void button13_Click(object sender, EventArgs e)
        {
            var but = sender as Button;
            dummySetting.Disable_PLAir = !dummySetting.Disable_PLAir;
            but.BackColor = dummySetting.Disable_PLAir ? Color.Tomato : Color.Lime;
        }

        private void button14_Click(object sender, EventArgs e)
        {
            var but = sender as Button;
            dummySetting.Disable_PRAir = !dummySetting.Disable_PRAir;
            but.BackColor = dummySetting.Disable_PRAir ? Color.Tomato : Color.Lime;
        }

        private void button10_Click(object sender, EventArgs e)
        {
            var but = sender as Button;
            dummySetting.Force_JobEndBuzzer = !dummySetting.Force_JobEndBuzzer;
            but.BackColor = dummySetting.Force_JobEndBuzzer ? Color.Gold : SystemColors.Control;
        }

        private void button7_Click_2(object sender, EventArgs e)
        {
            var but = sender as Button;
            dummySetting.Enable_PickerCylinder = !dummySetting.Enable_PickerCylinder;
            but.BackColor = dummySetting.Enable_PickerCylinder == false ? Color.Tomato : Color.Lime;
        }

        private void button8_Click(object sender, EventArgs e)
        {
            var but = sender as Button;
            dummySetting.SystemBypass = !dummySetting.SystemBypass;
            but.BackColor = dummySetting.SystemBypass ? Color.DarkBlue : Color.Transparent;
            but.ForeColor = dummySetting.SystemBypass ? Color.White : Color.Black;
        }
    }
}

