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

using INT89DB_26_ImageDll;

namespace INT89DB_26.Forms
{
    public partial class FormFactoryReset : Form
    {
        #region Field
        private int Time = 0;
        private FormMain m_ParentForm;
        #endregion

        #region Constructor
        public FormFactoryReset(FormMain parent)
        {
            InitializeComponent();

            this.ParentForm = parent;
        }
        #endregion

        #region Property
        public FormMain ParentForm
        {
            get { return this.m_ParentForm; }
            private set { this.m_ParentForm = value; }
        } 
        #endregion

        #region Method
        private void InitializeDesign()
        {
            Class1 images = new Class1();

            if (this.ParentForm.SystemConfig.Language == DataStore.E_LanguageID.Korean)
            {

            }
            else if (this.ParentForm.SystemConfig.Language == DataStore.E_LanguageID.English)
            {
                this.labelTitle.Text = "Factory reset";
                this.labelResetWait.Text = "Wait a minute...";
                this.labelResetRestart.Text = "Turn OFF the power and then turn ON the power";
                this.labelResetFail.Text = "Initialization failed";

                this.buttonBack.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.engBackDown));
                this.buttonBack.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.engBackUp));
            }
            else if (this.ParentForm.SystemConfig.Language == DataStore.E_LanguageID.Chinese)
            {
                this.labelTitle.Text = "回复出厂设置";
                this.labelResetWait.Text = "请稍等...";
                this.labelResetRestart.Text = "关掉电源，然后在打开电源";
                this.labelResetFail.Text = "初始化失敗";

                this.buttonBack.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.chnBackDown));
                this.buttonBack.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.chnBackUp));
            }
            else if (this.ParentForm.SystemConfig.Language == DataStore.E_LanguageID.Czech)
            {

            }
            else
            {

            }
        }

        public void DisplayRefresh()
        {
            this.labelResetWait.Visible = true;
            this.labelResetRestart.Visible = false;

            this.progressBarResetBar.Value = 0;
            //this.ParentForm.TransferData(CommunicationCommand.Initialization, CommunicationID.MainBoard);
            this.progressBarResetBar.Value += 20;
            //this.ResetConfigurationFile();
            this.progressBarResetBar.Value += 20;
            this.ResetProductFile();
            this.progressBarResetBar.Value += 20;
            this.ResetJudgmentSetFile();
            this.progressBarResetBar.Value += 20;
            this.ResetCountFile();
            this.progressBarResetBar.Value = 100;

            this.labelResetWait.Visible = false;
            this.labelResetRestart.Visible = true;
        }

        public void ResetConfigurationFile()
        {
            string fullFilePath = "";
            bool fileCheck = false;
            StructSystemConfigurationItem1 structItem;

            fullFilePath = this.ParentForm.PathSystemFileFolder1 + "system.cfg";
            this.ParentForm.smartFileIO.FilePathName = fullFilePath;

            FileInfo fileInfo = new FileInfo(fullFilePath);
            fileCheck = fileInfo.Exists;

            this.ParentForm.smartFileIO.Open(this.ParentForm.BufferSmartUart);

            this.ParentForm.SystemConfig.Initialization();

            structItem.IsDataBackup = this.ParentForm.SystemConfig.IsDataBackup;
            structItem.DummyBool5 = false;
            structItem.DummyBool6 = false;
            structItem.DummyBool7 = false;
            structItem.IsFeedbackBoard = false;
            structItem.IsFeedback = false;
            structItem.IsLogin = this.ParentForm.SystemConfig.IsLogin;
            structItem.IsWeightViewForward = this.ParentForm.SystemConfig.IsWeightViewForward;
            structItem.DecimalPlaces = this.ParentForm.SystemConfig.DecimalPlaces;
            structItem.InverterQTY = this.ParentForm.SystemConfig.InverterQTY;
            structItem.EquipmentColumns = this.ParentForm.SystemConfig.EquipmentColumns;
            structItem.EquipmentID = this.ParentForm.SystemConfig.EquipmentID;
            structItem.EquipmentMode = this.ParentForm.SystemConfig.EquipmentMode;
            structItem.ProductNumber = this.ParentForm.SystemConfig.ProductNumber;
            structItem.EthernetLocalPort = 0;
            structItem.EthernetServerPort = 0;
            structItem.UsbID = this.ParentForm.SystemConfig.UsbID;
            structItem.DummyString6 = "";
            structItem.Unit = this.ParentForm.SystemConfig.Unit;
            structItem.Language = this.ParentForm.SystemConfig.Language;
            structItem.EthernetMode = 0;
            structItem.EthernetOperationMode = 0;
            structItem.SerialNumber = this.ParentForm.SystemConfig.SerialNumber;

            structItem.DummyBool1 = false;
            structItem.DummyBool2 = false;
            structItem.DummyBool3 = false;
            structItem.DummyBool4 = false;
            structItem.FeedbackSampleCount = 10;
            structItem.FeedbackIgnorePackages = 5;
            structItem.ModbusTcpStartAddress = this.ParentForm.SystemConfig.ModbusTcpStartAddress;
            structItem.Serial3BaudRate = this.ParentForm.SystemConfig.Serial3BaudRate;
            structItem.Serial3Mode = this.ParentForm.SystemConfig.Serial3Mode;
            structItem.EthernetServerAddress = "";
            structItem.DummyString2 = "";
            structItem.DummyString3 = "";
            structItem.DummyString4 = "";
            structItem.DummyString5 = "";

            this.ParentForm.smartFileIO.WriteStructure(structItem, 0);

        }
        public void ResetProductFile()
        {
            bool fileCheck = false;
            string fullFilePath = "";
            StructProductItem structItem;

            fullFilePath = this.ParentForm.PathSystemFileFolder2 + "ProductItem.int";
            this.ParentForm.smartFileIO.FilePathName = fullFilePath;

            FileInfo fileInfo = new FileInfo(fullFilePath);
            fileCheck = fileInfo.Exists;

            this.ParentForm.smartFileIO.Open(this.ParentForm.BufferSmartUart);

            this.ParentForm.CurrentProductItem.Initialization();

            for (int i = 0; i < 1000; i++)
            {
                structItem.Number = i + 1;
                structItem.Name = string.Format("Product {0}", i + 1);
                structItem.LotNo = string.Format("Lot {0}", i + 1);
                structItem.OverRange = this.ParentForm.CurrentProductItem.OverRange;
                structItem.PassRange = this.ParentForm.CurrentProductItem.PassRange;
                structItem.UnderRange = this.ParentForm.CurrentProductItem.UnderRange;
                structItem.TareRange = this.ParentForm.CurrentProductItem.TareRange;
                structItem.DummyString1 = "";
                structItem.DummyString2 = "";
                structItem.DummyString3 = "";
                structItem.DummyString4 = "";
                structItem.DummyString5 = "";
                structItem.DummyString6 = "";
                structItem.DummyString7 = "";
                structItem.DummyString8 = "";

                this.ParentForm.smartFileIO.WriteStructure_Begin(structItem, i);
            }
            this.ParentForm.smartFileIO.WriteStructure_End();
        }
        public void ResetJudgmentSetFile()
        {
            bool fileCheck = false;
            string fullFilePath = "";
            StructJudgmentSetItem structItem;

            fullFilePath = this.ParentForm.PathSystemFileFolder2 + "JudgmentSetItem.int";
            this.ParentForm.smartFileIO.FilePathName = fullFilePath;

            FileInfo fileInfo = new FileInfo(fullFilePath);
            fileCheck = fileInfo.Exists;

            this.ParentForm.smartFileIO.Open(this.ParentForm.BufferSmartUart);

            this.ParentForm.CurrentJudgmentSetItem.Initialization();

            for (int i = 0; i < 1000; i++)
            {
                structItem.Filter = this.ParentForm.CurrentJudgmentSetItem.Filter;
                structItem.JudgmentDelayTime = this.ParentForm.CurrentJudgmentSetItem.JudgmentDelayTime;
                structItem.DoubleDelayTime = this.ParentForm.CurrentJudgmentSetItem.DoubleDelayTime;
                structItem.JudgmentCount = this.ParentForm.CurrentJudgmentSetItem.JudgmentCount;
                structItem.ConveyorSpeed1 = this.ParentForm.CurrentJudgmentSetItem.ConveyorSpeed1;
                structItem.DynamicCorrection = this.ParentForm.CurrentJudgmentSetItem.DynamicCorrection;
                structItem.DummyInt1 = 0;
                structItem.DummyInt2 = 0;
                structItem.DummyInt3 = 0;
                structItem.DummyInt4 = 0;
                structItem.Sorter1Mode = this.ParentForm.CurrentJudgmentSetItem.Sorter1Mode;
                structItem.Sorter1DelayTime = this.ParentForm.CurrentJudgmentSetItem.Sorter1DelayTime;
                structItem.Sorter1OperatingTime = this.ParentForm.CurrentJudgmentSetItem.Sorter1RunTime;
                structItem.DummyInt5 = 0;
                structItem.DummyInt6 = 0;
                structItem.DummyInt7 = 0;
                structItem.DummyInt8 = 0;
                structItem.DummyInt9 = 0;
                structItem.DummyInt10 = 0;
                structItem.DummyInt11 = 0;
                structItem.DummyInt12 = 0;
                structItem.DummyInt13 = 0;
                structItem.ConveyorSpeed4 = this.ParentForm.CurrentJudgmentSetItem.ConveyorSpeed4;
                structItem.ConveyorSpeed3 = this.ParentForm.CurrentJudgmentSetItem.ConveyorSpeed3;
                structItem.ConveyorSpeed2 = this.ParentForm.CurrentJudgmentSetItem.ConveyorSpeed2;
                structItem.DummyInt14 = 0;
                structItem.DummyInt15 = 0;
                structItem.DummyString1 = "";
                structItem.DummyString2 = "";
                structItem.DummyString3 = "";
                structItem.DummyString4 = "";
                structItem.DummyString5 = "";

                this.ParentForm.smartFileIO.WriteStructure_Begin(structItem, i);
            }
            this.ParentForm.smartFileIO.WriteStructure_End();
        }
        public void ResetCountFile()
        {
            string fullFilePath = "";
            bool fileCheck = false;
            StructCounter structItem;

            fullFilePath = this.ParentForm.PathSystemFileFolder2 + "counterItem.int";
            this.ParentForm.smartFileIO.FilePathName = fullFilePath;

            FileInfo fileInfo = new FileInfo(fullFilePath);
            fileCheck = fileInfo.Exists;

            this.ParentForm.smartFileIO.Open(this.ParentForm.BufferSmartUart);

            for (int i = 0; i < 1000; i++)
            {
                structItem.OverCount1 = 0;
                structItem.PassCount1 = 0;
                structItem.UnderCount1 = 0;
                structItem.ExNGCount1 = 0;

                structItem.OverCount2 = 0;
                structItem.PassCount2 = 0;
                structItem.UnderCount2 = 0;
                structItem.ExNGCount2 = 0;

                structItem.OverCount3 = 0;
                structItem.PassCount3 = 0;
                structItem.UnderCount3 = 0;
                structItem.ExNGCount3 = 0;

                structItem.OverCount4 = 0;
                structItem.PassCount4 = 0;
                structItem.UnderCount4 = 0;
                structItem.ExNGCount4 = 0;

                structItem.OverCount5 = 0;
                structItem.PassCount5 = 0;
                structItem.UnderCount5 = 0;
                structItem.ExNGCount5 = 0;

                structItem.OverCount6 = 0;
                structItem.PassCount6 = 0;
                structItem.UnderCount6 = 0;
                structItem.ExNGCount6 = 0;

                structItem.DummyInt1 = 0;
                structItem.DummyInt2 = 0;
                structItem.DummyInt3 = 0;
                structItem.DummyInt4 = 0;
                structItem.DummyInt5 = 0;
                structItem.DummyInt6 = 0;
                structItem.DummyInt7 = 0;
                structItem.DummyInt8 = 0;
                structItem.DummyInt9 = 0;
                structItem.DummyInt10 = 0;

                this.ParentForm.smartFileIO.WriteStructure_Begin(structItem, i);
            }
            this.ParentForm.smartFileIO.WriteStructure_End();
        }
        #endregion

        #region Override Member
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            this.InitializeDesign();
        }
        #endregion

        #region Event Handler
        private void timer1_Tick(object sender, EventArgs e)
        {
            this.Time++;

            if (this.Time == 2)
            {
                this.ParentForm.ChildFormFactoryInitialize.DisplayRefresh();

                this.timer1.Enabled = false;
                this.Time = 0;
            }
        }

        private void buttonBack_Click(object sender, EventArgs e)
        {
            this.ParentForm.ChildFormMenu.DisplayRefresh();
            ((FormMain)(Owner)).smartForm.Show((int)DataStore.E_FormStore.FormMenu);
        }
        #endregion
    }
}