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 INT69DC_ImageDll;
using INT69DC_7C.Part11_UserManager;

namespace INT69DC_7C.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()
        {
            if (this.ParentForm.SystemConfig.Language == DataStore.LanguageID.Korean)
            {

            }
            else if (this.ParentForm.SystemConfig.Language == DataStore.LanguageID.English)
            {
                this.labelTitle.Text = "Initialization";

                this.labelResetWait.Text = "Wait a minute...";
                this.labelResetRestart.Text = "Turn the power off and back on";
            }
            else if (this.ParentForm.SystemConfig.Language == DataStore.LanguageID.Chinese)
            {
                this.labelTitle.Text = "回复出厂设置";

                this.labelResetWait.Text = "请稍等...";
                this.labelResetRestart.Text = "关掉电源，然后在打开电源";
            }
            else if (this.ParentForm.SystemConfig.Language == DataStore.LanguageID.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.";
            }
            else if (this.ParentForm.SystemConfig.Language == DataStore.LanguageID.German)
            {
                this.labelTitle.Text = "Werkseinstellung";

                this.labelResetWait.Text = "Initialisierung wird ausgeführt";
                this.labelResetRestart.Text = "Mit dem Hauptschalter aus- und einschalten";
            }
            else if (this.ParentForm.SystemConfig.Language == DataStore.LanguageID.Japanese)
            {
                this.labelTitle.Text = "初期化";

                this.labelResetWait.Text = "しばらくお待ちください。";
                this.labelResetRestart.Text = "再起動が必要です。";
            }
            else
            {

            }
        }

        private void ResetConfigurationFile()
        {
            string fullFilePath = "";
            bool fileCheck = false;
            StructSystemConfigurationItem structItem1;
            StructSystemConfigurationItem1 structItem2;

            fullFilePath = this.ParentForm.PathSystemFileFolder + "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();

            structItem1.IsDataBackup = this.ParentForm.SystemConfig.IsDataBackup;
            structItem1.IsOptFeedbackBoardUse = this.ParentForm.SystemConfig.IsOptFeedbackBoardUse;
            structItem1.IsFeedingConveyor = this.ParentForm.SystemConfig.IsFeedingConveyor;
            structItem1.IsIndividualNG = this.ParentForm.SystemConfig.IsIndividualNG;
            structItem1.IsDischargeConveyor = this.ParentForm.SystemConfig.IsDischargeConveyor;
            structItem1.IsImpellerMotorDirectionCW = this.ParentForm.SystemConfig.IsImpellerMotorDirectionCW;
            structItem1.IsWeightViewForward = this.ParentForm.SystemConfig.IsWeightViewForward;
            structItem1.DecimalPlaces = this.ParentForm.SystemConfig.DecimalPlaces;
            structItem1.FeedbackSampleCount = this.ParentForm.SystemConfig.FeedbackSampleCount;
            structItem1.EquipmentColumns = this.ParentForm.SystemConfig.EquipmentColumns;
            structItem1.EquipmentID = this.ParentForm.SystemConfig.EquipmentID;
            structItem1.EquipmentMode = this.ParentForm.SystemConfig.EquipmentMode;
            structItem1.ProductNumber = this.ParentForm.SystemConfig.ProductNumber;
            structItem1.FeedbackIgnorePackages = this.ParentForm.SystemConfig.FeedbackIgnorePackages;
            structItem1.FeedbackPulseWidth = this.ParentForm.SystemConfig.FeedbackPulseWidth;
            structItem1.Serial3BaudRate = this.ParentForm.SystemConfig.Serial3BaudRate;
            structItem1.Serial3Mode = this.ParentForm.SystemConfig.Serial3Mode;
            structItem1.UsbID = this.ParentForm.SystemConfig.UsbID;
            structItem1.DummyString6 = "";
            structItem1.Unit = this.ParentForm.SystemConfig.Unit;
            structItem1.Language = this.ParentForm.SystemConfig.Language;
            structItem1.ImpellerMotorBaudrate = this.ParentForm.SystemConfig.ImpellerMotorBaudrate;
            structItem1.ImpellerMotorAnglePass = this.ParentForm.SystemConfig.ImpellerMotorAnglePass;
            structItem1.SerialNumber = this.ParentForm.SystemConfig.SerialNumber;

            // Dummy bool
            structItem1.IsOptDataStatistics = this.ParentForm.SystemConfig.IsOptDataStatistics;
            structItem1.IsLogin = this.ParentForm.SystemConfig.IsLogin;
            structItem1.IsIndividualTransmission = this.ParentForm.SystemConfig.IsIndividualTransmission;
            structItem1.IsEmergencyStopEntrySensorError = this.ParentForm.SystemConfig.IsEmergencyStopEntrySensorError;
            structItem1.IsFeedingConveyorRunPass = this.ParentForm.SystemConfig.IsFeedingConveyorRunPass;

            // Dummy int
            structItem1.FeedbackCorrectionRate = this.ParentForm.SystemConfig.FeedbackCorrectionRate;
            structItem1.DischargeConveyorDirectionCW = (int)this.ParentForm.SystemConfig.DischargeConveyorDirectionCW;
            structItem1.AlarmContinuousNG = this.ParentForm.SystemConfig.AlarmContinuousNG;
            structItem1.InputSensorSelect = this.ParentForm.SystemConfig.InputSensorSelect;
            structItem1.ABFoodTimeoutInterval = this.ParentForm.SystemConfig.ABFoodTimeoutInterval;

            // Dummy string
            structItem1.User_Level1_Name = "";
            structItem1.User_Level2_Name = "";
            structItem1.User_Level3_Name = "";
            structItem1.DummyString4 = "";
            structItem1.DummyString5 = "";

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

            #region StructItem2
            structItem2.DummyBool1 = false;
            structItem2.DummyBool2 = false;
            structItem2.DummyBool3 = false;
            structItem2.DummyBool4 = false;
            structItem2.DummyBool5 = false;
            structItem2.DummyBool6 = false;
            structItem2.DummyBool7 = false;
            structItem2.DummyBool8 = false;
            structItem2.DummyBool9 = false;
            structItem2.DummyBool10 = false;
            structItem2.DummyBool11 = false;
            structItem2.DummyBool12 = false;
            structItem2.DummyBool13 = false;
            structItem2.DummyBool14 = false;
            structItem2.IsSerialDLL = false;
            structItem2.IsOptPrinter = this.ParentForm.SystemConfig.IsOptPrinter;
            structItem2.IsOptPart11 = this.ParentForm.SystemConfig.IsOptPart11;
            structItem2.IsAlarmTotalPassCntEnable = this.ParentForm.SystemConfig.IsAlarmTotalPassCntEnable;
            structItem2.IsIntegratedTransmission = this.ParentForm.SystemConfig.IsIntegratedTransmission;
            structItem2.IsAlarmContinuousNGEnable = this.ParentForm.SystemConfig.IsAlarmContinuousNGEnable;

            structItem2.DummyInt1 = 0;
            structItem2.DummyInt2 = 0;
            structItem2.DummyInt3 = 0;
            structItem2.DummyInt4 = 0;
            structItem2.DummyInt5 = 0;
            structItem2.DummyInt6 = 0;
            structItem2.DataStoragePeriod = this.ParentForm.SystemConfig.DataStoragePeriod;
            structItem2.AutomaticLogout = this.ParentForm.SystemConfig.AutomaticLogout;
            structItem2.AlarmTotalPassCnt = this.ParentForm.SystemConfig.AlarmTotalPassCnt;
            structItem2.ModbusTcpStartAddress = this.ParentForm.SystemConfig.ModbusTcpStartAddress;
            structItem2.ModbusRtuSlaveID = this.ParentForm.SystemConfig.ModbusRtuSlaveID;
            structItem2.ModbusTcpAddress1 = this.ParentForm.SystemConfig.ModbusTcpAddress1;
            structItem2.ModbusTcpAddress2 = this.ParentForm.SystemConfig.ModbusTcpAddress2;
            structItem2.EthernetLocalPort = this.ParentForm.SystemConfig.EthernetLocalPort;
            structItem2.EthernetOperationMode = this.ParentForm.SystemConfig.EthernetOperationMode;
            structItem2.EthernetMode = this.ParentForm.SystemConfig.EthernetMode;
            structItem2.EthernetServerPort = this.ParentForm.SystemConfig.EthernetServerPort;
            structItem2.StepMotorType = this.ParentForm.SystemConfig.StepMotorType;
            structItem2.ImpellerMotorAnglePass = this.ParentForm.SystemConfig.ImpellerMotorAnglePass;
            structItem2.ImpellerMotorAngleNG = this.ParentForm.SystemConfig.ImpellerMotorAngleNG;

            structItem2.DummyString1 = "";
            structItem2.DummyString2 = "";
            structItem2.DummyString3 = "";
            structItem2.DummyString4 = "";
            structItem2.DummyString5 = "";
            structItem2.EthernetServerAddress = this.ParentForm.SystemConfig.EthernetServerAddress;
            structItem2.DummyString7 = "";
            structItem2.DummyString8 = "";
            structItem2.DummyString9 = "";
            structItem2.DummyString10 = "";
            #endregion

            this.ParentForm.smartFileIO.WriteStructure(structItem2, 1);

            this.ParentForm.smartFileIO.Close();
        }
        private 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.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();
            this.ParentForm.smartFileIO.Close();
        }
        private 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.DoubleDelayTime = this.ParentForm.CurrentJudgmentSetItem.DoubleDelayTime;
                structItem.JudgmentCount = this.ParentForm.CurrentJudgmentSetItem.JudgmentCount;
                structItem.FeedSpeed = this.ParentForm.CurrentJudgmentSetItem.FeedSpeed;
                structItem.DynamicCorrection = this.ParentForm.CurrentJudgmentSetItem.DynamicCorrection;
                structItem.DischargeConveyorDelayTime = this.ParentForm.CurrentJudgmentSetItem.RejectConveyorDelayTime;
                structItem.DischargeConveyorOperatingTime = this.ParentForm.CurrentJudgmentSetItem.RejectConveyorRunTime;
                structItem.FeedingConveyorDelayTime = this.ParentForm.CurrentJudgmentSetItem.RejectConveyorDelayTime;
                structItem.FeedingConveyorOperatingTime = this.ParentForm.CurrentJudgmentSetItem.RejectConveyorRunTime;
                structItem.Sorter1Mode = this.ParentForm.CurrentJudgmentSetItem.Sorter1Mode;
                structItem.Sorter1DelayTime = this.ParentForm.CurrentJudgmentSetItem.Sorter1DelayTime;
                structItem.Sorter1OperatingTime = this.ParentForm.CurrentJudgmentSetItem.Sorter1RunTime;
                structItem.Sorter2Mode = this.ParentForm.CurrentJudgmentSetItem.Sorter2Mode;
                structItem.Sorter2DelayTime = this.ParentForm.CurrentJudgmentSetItem.Sorter2DelayTime;
                structItem.Sorter2OperatingTime = this.ParentForm.CurrentJudgmentSetItem.Sorter2RunTime;
                structItem.Sorter3Mode = this.ParentForm.CurrentJudgmentSetItem.Sorter3Mode;
                structItem.Sorter3DelayTime = this.ParentForm.CurrentJudgmentSetItem.Sorter3DelayTime;
                structItem.Sorter3OperatingTime = this.ParentForm.CurrentJudgmentSetItem.Sorter3RunTime;
                structItem.Sorter4Mode = this.ParentForm.CurrentJudgmentSetItem.Sorter4Mode;
                structItem.Sorter4DelayTime = this.ParentForm.CurrentJudgmentSetItem.Sorter4DelayTime;
                structItem.Sorter4OperatingTime = this.ParentForm.CurrentJudgmentSetItem.Sorter4RunTime;
                structItem.DummyInt1 = 0;
                structItem.DummyInt2 = 0;
                structItem.DummyInt3 = 0;
                structItem.DummyInt4 = 0;
                structItem.ForcedZeroDelayTime = this.ParentForm.CurrentJudgmentSetItem.ForcedZeroDelayTime;
                structItem.DummyString1 = "";
                structItem.DummyString2 = "";
                structItem.DummyString3 = "";
                structItem.DummyString4 = "";
                structItem.DummyString5 = "";

                this.ParentForm.smartFileIO.WriteStructure_Begin(structItem, i);
            }
            this.ParentForm.smartFileIO.WriteStructure_End();
            this.ParentForm.smartFileIO.Close();
        }
        private void ResetCountFile()
        {
            string fullFilePath = "";
            bool fileCheck = false;
            StructCounter 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();
        }
        private void ResetFeedbackFile()
        {
            string fullFilePath = "";
            bool fileCheck = false;
            StructFeedbackConfiguration structItem;

            fullFilePath = this.ParentForm.PathSystemFileFolder + "FeedbackItem.int";
            this.ParentForm.smartFileIO.FilePathName = fullFilePath;

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

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

            for (int i = 0; i < 1000; i++)
            {
                structItem.HightLimit = this.ParentForm.CurrentFeedbackItem.HighLimit;
                structItem.LowLimit = this.ParentForm.CurrentFeedbackItem.LowLimit;
                structItem.DeadZone = this.ParentForm.CurrentFeedbackItem.DeadZone;

                structItem.DummyBool1 = false;
                structItem.DummyBool2 = false;
                structItem.DummyBool3 = false;

                structItem.DummyInt1 = 1;
                structItem.DummyInt2 = 1;
                structItem.DummyInt3 = 1;

                structItem.DummyString1 = "";
                structItem.DummyString2 = "";
                structItem.DummyString3 = "";

                this.ParentForm.smartFileIO.WriteStructure_Begin(structItem, i);
            }
            this.ParentForm.smartFileIO.WriteStructure_End();
            this.ParentForm.smartFileIO.Close();
        }
        private void ResetUserListFile()
        {
            UserManager.UserManager_UserAllDel();
        }

        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.ResetConfigurationFile();
            this.ResetUserListFile();
            this.progressBarResetBar.Value += 20;
            this.ResetProductFile();
            this.progressBarResetBar.Value += 20;
            this.ResetJudgmentSetFile();
            this.progressBarResetBar.Value += 20;
            this.ResetCountFile();
            this.progressBarResetBar.Value += 20;
            this.ResetFeedbackFile();
            this.progressBarResetBar.Value = 100;

            this.labelResetWait.Visible = false;
            this.labelResetRestart.Visible = true;
        }
        #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
    }
}