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

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

        #region Constructor
        public FormFactoryInitialize(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()
        {
            switch (this.ParentForm.SystemConfig.Language)
            {
                case DataStore.E_LanguageID.Korean:
                    break;
                case DataStore.E_LanguageID.English:
                    #region English
                    this.labelTitle.Text = "Initialization";

                    this.labelResetWait.Text = "Wait a minute...";
                    this.labelResetRestart.Text = "Turn the power off and back on"; 
                    #endregion
                    break;
                case DataStore.E_LanguageID.Chinese:
                    #region Chinese
                    this.labelTitle.Text = "回复出厂设置";

                    this.labelResetWait.Text = "请稍等...";
                    this.labelResetRestart.Text = "关掉电源，然后在打开电源"; 
                    #endregion
                    break;
                case DataStore.E_LanguageID.Czech:
                    #region Czech
                    this.labelTitle.Text = "Tovární nastavení";

                    this.labelResetWait.Text = "Vyčkejte...";
                    this.labelResetRestart.Text = "Nejprve zařízení vypněte a pak opět zapněte."; 
                    #endregion
                    break;
                case DataStore.E_LanguageID.German:
                    #region German
                    this.labelTitle.Text = "Werkseinstellung";

                    this.labelResetWait.Text = "Initialisierung wird ausgeführt";
                    this.labelResetRestart.Text = "Mit dem Hauptschalter aus- und einschalten"; 
                    #endregion
                    break;
                case DataStore.E_LanguageID.Japanese:
                    #region Japanese
                    this.labelTitle.Text = "初期化";

                    this.labelResetWait.Text = "初期化中...";
                    this.labelResetRestart.Text = "電源をOFFにしてから電源をONにする";
                    #endregion
                    break;
                default:
                    break;
            }
        }

        public void DisplayRefresh()
        {
            this.ParentForm.SystemConfig.CurrentForm = DataStore.FormStore.FormFactory;

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

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

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

        public void ResetConfigurationFile()
        {
            string fullFilePath = "";

            this.ParentForm.SystemConfig.Initialization();

            #region system1
            StructSystemConfigurationItem1 structItem1;
            fullFilePath = this.ParentForm.PathSystemFileFolder + "system.cfg";
            this.ParentForm.smartFileIO.FilePathName = fullFilePath;

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

            structItem1.IsDataBackup = this.ParentForm.SystemConfig.IsDataBackup;
            structItem1.IsEachNG = this.ParentForm.SystemConfig.IsEachNG;
            structItem1.IsLogin = this.ParentForm.SystemConfig.IsLogin;
            structItem1.IsWeightViewForward = this.ParentForm.SystemConfig.IsWeightViewForward;
            structItem1.IsOptDataStatistics = this.ParentForm.SystemConfig.IsOptDataStatistics;
            structItem1.IsSorterCheckEnable = false;
            structItem1.IsDeleteAfterBackup = false;
            structItem1.DummyBool7 = false;

            structItem1.DecimalPlaces = this.ParentForm.SystemConfig.DecimalPlaces;
            structItem1.EquipmentColumns = this.ParentForm.SystemConfig.EquipmentColumns;
            structItem1.ProductNumber = this.ParentForm.SystemConfig.ProductNumber;
            structItem1.Serial3BaudRate = this.ParentForm.SystemConfig.Serial3BaudRate;
            structItem1.Serial3Mode = this.ParentForm.SystemConfig.Serial3Mode;
            structItem1.UsbID = this.ParentForm.SystemConfig.UsbID;
            structItem1.WeightLimitIgnoreCount = 0;
            structItem1.MotorSelect = this.ParentForm.SystemConfig.MotorSelect;

            structItem1.Unit = this.ParentForm.SystemConfig.Unit;
            structItem1.SerialNumber = this.ParentForm.SystemConfig.SerialNumber;

            structItem1.Language = this.ParentForm.SystemConfig.Language;

            structItem1.DummyBool1 = false;
            structItem1.DummyBool2 = false;
            structItem1.DummyBool3 = false;
            structItem1.DummyBool4 = false;
            structItem1.DummyBool8 = false;
            structItem1.Sorter1Location = this.ParentForm.SystemConfig.Sorter1Location;
            structItem1.Sorter2Location = this.ParentForm.SystemConfig.Sorter2Location;
            structItem1.EXT1_DelayTime = this.ParentForm.SystemConfig.EXT1_DelayTime;
            structItem1.EXT1_RunTime = this.ParentForm.SystemConfig.EXT1_RunTime;
            structItem1.InverterTorque = this.ParentForm.SystemConfig.InverterTorque;
            structItem1.EmptyWeight = this.ParentForm.SystemConfig.EmptyWeight;
            structItem1.ComPort = "";
            structItem1.DummyString3 = "";
            structItem1.DummyString4 = "";
            structItem1.DummyString5 = "";

            this.ParentForm.smartFileIO.WriteStructure(structItem1, 0);
            this.ParentForm.smartFileIO.Close(); 
            #endregion

            #region system2
            StructSystemConfigurationItem2 structItem2;
            fullFilePath = this.ParentForm.PathSystemFileFolder + "system2.cfg";
            this.ParentForm.smartFileIO.FilePathName = fullFilePath;

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

            structItem2.DummyDouble11 = 0.0;
            structItem2.DummyDouble12 = 0.0;
            structItem2.DummyDouble13 = 0.0;
            structItem2.DummyDouble14 = 0.0;
            structItem2.DummyDouble15 = 0.0;
            structItem2.DummyDouble16 = 0.0;
            structItem2.DummyDouble17 = 0.0;
            structItem2.DummyDouble18 = 0.0;
            structItem2.DummyDouble19 = 0.0;
            structItem2.DummyDouble20 = 0.0;
            structItem2.DummyDouble21 = 0.0;
            structItem2.DummyDouble22 = 0.0;

            structItem2.DummyBool1 = false;
            structItem2.DummyBool2 = false;
            structItem2.DummyBool3 = false;
            structItem2.DummyBool4 = false;
            structItem2.DummyBool5 = false;
            structItem2.DummyBool6 = false;
            structItem2.DummyBool7 = false;
            structItem2.IsPart11 = false;
            structItem2.IsLeakSamplingOnce = false;
            structItem2.IsSamplingOnce = this.ParentForm.SystemConfig.IsSamplingOnce;

            structItem2.DummyInt1 = 0;
            structItem2.NumberOfLoginFailures = 0;
            structItem2.AutomaticLogout = 0;
            structItem2.StoragePeriod = 0;
            structItem2.LeakSamplingTime = 0;
            structItem2.Sorter4Location = 0;
            structItem2.ComRetryCNT = 0;
            structItem2.SamplingCount = this.ParentForm.SystemConfig.SamplingCount;
            structItem2.SamplingTime = this.ParentForm.SystemConfig.SamplingTime;
            structItem2.Sorter3Location = this.ParentForm.SystemConfig.Sorter3Location;

            structItem2.DummyDouble1 = 0.0;
            structItem2.DummyDouble2 = 0.0;
            structItem2.DummyDouble3 = 0.0;
            structItem2.DummyDouble4 = 0.0;
            structItem2.DummyDouble5 = 0.0;
            structItem2.DummyDouble6 = 0.0;
            structItem2.DummyDouble7 = 0.0;
            structItem2.DummyDouble8 = 0.0;
            structItem2.DummyDouble9 = 0.0;
            structItem2.DummyDouble10 = 0.0;

            structItem2.DummyString1 = "";
            structItem2.DummyString2 = "";
            structItem2.DummyString3 = "";
            structItem2.DummyString4 = "";
            structItem2.DummyString5 = "";
            structItem2.DummyString6 = "";
            structItem2.DummyString7 = "";
            structItem2.User_Level1_Name = "";
            structItem2.User_Level2_Name = "";
            structItem2.User_Level3_Name = "";

            structItem2.DummyString11 = "";
            structItem2.DummyString12 = "";
            structItem2.DummyString13 = "";
            structItem2.DummyString14 = "";
            structItem2.DummyString15 = "";

            this.ParentForm.smartFileIO.WriteStructure(structItem2, 0);
            this.ParentForm.smartFileIO.Close();
            #endregion
        }
        public void ResetProductFile()
        {
            bool fileCheck = false;
            string fullFilePath = "";
            StructProductItem structItem;

            fullFilePath = this.ParentForm.PathSystemFileFolder + "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.OverRangeLimit = this.ParentForm.CurrentProductItem.OverRangeLimit;
                structItem.UnderRangeLimit = this.ParentForm.CurrentProductItem.UnderRangeLimit;
                structItem.DummyString3 = "";
                structItem.DummyString4 = "";
                structItem.DummyString5 = "";
                structItem.DummyString6 = "";
                structItem.DummyString7 = "";
                structItem.DummyString8 = "";

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

            fullFilePath = this.ParentForm.PathSystemFileFolder + "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.JudgmentCount = this.ParentForm.CurrentJudgmentSetItem.JudgmentCount;
                structItem.FeedSpeed = this.ParentForm.CurrentJudgmentSetItem.FeedSpeed;
                structItem.TurnDelayTime = this.ParentForm.CurrentJudgmentSetItem.TurnDelayTime;
                structItem.Sorter1RunTime = this.ParentForm.CurrentJudgmentSetItem.Sorter1RunTime;
                structItem.DynamicCorrection = this.ParentForm.CurrentJudgmentSetItem.DynamicCorrection;
                structItem.OriginOffset = this.ParentForm.CurrentJudgmentSetItem.OriginOffset;
                structItem.ForcedZeroDelayTime = this.ParentForm.CurrentJudgmentSetItem.ForcedZeroDelayTime;
                structItem.Sorter1DelayTime = this.ParentForm.CurrentJudgmentSetItem.Sorter1DelayTime;
                structItem.Sorter2DelayTime = this.ParentForm.CurrentJudgmentSetItem.Sorter2DelayTime;
                structItem.Sorter2RunTime = this.ParentForm.CurrentJudgmentSetItem.Sorter2RunTime;
                structItem.DummyString1 = "";
                structItem.Sorter4DelayTime = "";
                structItem.Sorter4RunTime = "";
                structItem.Sorter3DelayTime = this.ParentForm.CurrentJudgmentSetItem.Sorter3DelayTime;
                structItem.Sorter3RunTime = this.ParentForm.CurrentJudgmentSetItem.Sorter3RunTime;

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

            fullFilePath = this.ParentForm.PathSystemFileFolder + "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.OverCount7 = 0;
                structItem.PassCount7 = 0;
                structItem.UnderCount7 = 0;
                structItem.ExNGCount7 = 0;

                structItem.OverCount8 = 0;
                structItem.PassCount8 = 0;
                structItem.UnderCount8 = 0;
                structItem.ExNGCount8 = 0;

                structItem.OverCount9 = 0;
                structItem.PassCount9 = 0;
                structItem.UnderCount9 = 0;
                structItem.ExNGCount9 = 0;

                structItem.OverCount10 = 0;
                structItem.PassCount10 = 0;
                structItem.UnderCount10 = 0;
                structItem.ExNGCount10 = 0;

                structItem.OverCount11 = 0;
                structItem.PassCount11 = 0;
                structItem.UnderCount11 = 0;
                structItem.ExNGCount11 = 0;

                structItem.OverCount12 = 0;
                structItem.PassCount12 = 0;
                structItem.UnderCount12 = 0;
                structItem.ExNGCount12 = 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();
            this.ParentForm.smartFileIO.Close();
        }
        public void ResetCountFile2()
        {
            string fullFilePath = "";
            bool fileCheck = false;
            StructCounter2 structItem;

            fullFilePath = this.ParentForm.PathSystemFileFolder + "counterItem2.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.OverCount13 = 0;
                structItem.PassCount13 = 0;
                structItem.UnderCount13 = 0;
                structItem.ExNGCount13 = 0;

                structItem.OverCount14 = 0;
                structItem.PassCount14 = 0;
                structItem.UnderCount14 = 0;
                structItem.ExNGCount14 = 0;

                structItem.OverCount15 = 0;
                structItem.PassCount15 = 0;
                structItem.UnderCount15 = 0;
                structItem.ExNGCount15 = 0;

                structItem.OverCount16 = 0;
                structItem.PassCount16 = 0;
                structItem.UnderCount16 = 0;
                structItem.ExNGCount16 = 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;

                structItem.DummyInt11 = 0;
                structItem.DummyInt12 = 0;
                structItem.DummyInt13 = 0;
                structItem.DummyInt14 = 0;
                structItem.DummyInt15 = 0;
                structItem.DummyInt16 = 0;
                structItem.DummyInt17 = 0;
                structItem.DummyInt18 = 0;
                structItem.DummyInt19 = 0;
                structItem.DummyInt20 = 0;

                structItem.DummyInt21 = 0;
                structItem.DummyInt22 = 0;
                structItem.DummyInt23 = 0;
                structItem.DummyInt24 = 0;
                structItem.DummyInt25 = 0;
                structItem.DummyInt26 = 0;
                structItem.DummyInt27 = 0;
                structItem.DummyInt28 = 0;
                structItem.DummyInt29 = 0;
                structItem.DummyInt30 = 0;

                structItem.DummyInt31 = 0;
                structItem.DummyInt32 = 0;
                structItem.DummyInt33 = 0;
                structItem.DummyInt34 = 0;
                structItem.DummyInt35 = 0;
                structItem.DummyInt36 = 0;
                structItem.DummyInt37 = 0;
                structItem.DummyInt38 = 0;
                structItem.DummyInt39 = 0;
                structItem.DummyInt40 = 0;

                structItem.DummyInt41 = 0;
                structItem.DummyInt42 = 0;

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

            fullFilePath = this.ParentForm.PathSystemFileFolder + "DynamicCorrectionItem.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.DynamicCorrection1 = this.ParentForm.CurrentJudgmentSetItem.DynamicCorrection1;
                structItem.DynamicCorrection2 = this.ParentForm.CurrentJudgmentSetItem.DynamicCorrection2;
                structItem.DynamicCorrection3 = this.ParentForm.CurrentJudgmentSetItem.DynamicCorrection3;
                structItem.DynamicCorrection4 = this.ParentForm.CurrentJudgmentSetItem.DynamicCorrection4;
                structItem.DynamicCorrection5 = this.ParentForm.CurrentJudgmentSetItem.DynamicCorrection5;
                structItem.DynamicCorrection6 = this.ParentForm.CurrentJudgmentSetItem.DynamicCorrection6;
                structItem.DynamicCorrection7 = this.ParentForm.CurrentJudgmentSetItem.DynamicCorrection7;
                structItem.DynamicCorrection8 = this.ParentForm.CurrentJudgmentSetItem.DynamicCorrection8;
                structItem.DynamicCorrection9 = this.ParentForm.CurrentJudgmentSetItem.DynamicCorrection9;
                structItem.DynamicCorrection10 = this.ParentForm.CurrentJudgmentSetItem.DynamicCorrection10;
                structItem.DynamicCorrection11 = this.ParentForm.CurrentJudgmentSetItem.DynamicCorrection11;
                structItem.DynamicCorrection12 = this.ParentForm.CurrentJudgmentSetItem.DynamicCorrection12;

                structItem.DummyDouble1 = 0.0;
                structItem.DummyDouble2 = 0.0;
                structItem.DummyDouble3 = 0.0;
                structItem.DummyDouble4 = 0.0;
                structItem.DummyDouble5 = 0.0;
                structItem.DummyDouble6 = 0.0;
                structItem.DynamicCorrection13 = 0.0;
                structItem.DynamicCorrection14 = 0.0;
                structItem.DynamicCorrection15 = 0.0;
                structItem.DynamicCorrection16 = 0.0;

                structItem.DummyInt1 = 0;
                structItem.DummyInt2 = 0;
                structItem.DummyInt3 = 0;
                structItem.DummyInt4 = 0;
                structItem.Stopper2DelayTime = this.ParentForm.CurrentJudgmentSetItem.Stopper2DelayTime;
                structItem.Stopper2RunTime = this.ParentForm.CurrentJudgmentSetItem.Stopper2RunTime;
                structItem.EntryGateDelayTime = this.ParentForm.CurrentJudgmentSetItem.EntryGateDelayTime;
                structItem.EntryGateRunTime = this.ParentForm.CurrentJudgmentSetItem.EntryGateRunTime;
                structItem.DischargeStopperDelayTime = this.ParentForm.CurrentJudgmentSetItem.DischargeStopperDelayTime;
                structItem.DischargeStopperRunTime = this.ParentForm.CurrentJudgmentSetItem.DischargeStopperRunTime;

                this.ParentForm.smartFileIO.WriteStructure_Begin(structItem, i);
            }
            this.ParentForm.smartFileIO.WriteStructure_End();
            this.ParentForm.smartFileIO.Close();
        }
        #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.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.FormStore.FormMenu);
        }
        #endregion
    }
}