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;
using INT63DC_6CH.In_UserManager;

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

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

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

            this.ParentForm.SystemConfig.Initialization();

            StructSystemConfigurationItem structItem;

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

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

            // bool
            structItem.IsDataBackup = item.IsDataBackup;
            structItem.IsEachNG = item.IsEachNG;
            structItem.IsLogin = item.IsLogin;
            structItem.IsWeightViewForward = item.IsWeightViewForward;
            structItem.IsDataStatistics = item.IsDataStatistics;
            structItem.IsPart11 = item.IsPart11;
            structItem.IsModbus = false;
            structItem.dummyBool2 = false;
            structItem.dummyBool3 = false;
            structItem.dummyBool4 = false;
            structItem.dummyBool5 = false;
            structItem.dummyBool6 = false;
            structItem.dummyBool7 = false;
            structItem.dummyBool8 = false;
            structItem.dummyBool9 = false;
            structItem.dummyBool10 = false;
            structItem.dummyBool11 = false;
            structItem.dummyBool12 = false;
            structItem.dummyBool13 = false;
            structItem.dummyBool14 = false;
            structItem.IsFeedback = false;
            // int
            structItem.AutoLogoutTime = item.AutoLogoutTime;
            structItem.UsbID = item.UsbID;
            structItem.ProductNumber = item.ProductNumber;
            structItem.StoragePeriod = item.StoragePeriod;
            structItem.EquipmentColumns = item.EquipmentColumns;
            structItem.Chattering = item.Chattering;
            structItem.BuzzerRunTime = item.BuzzerRunTime;
            structItem.RelayRunTime = item.RelayRunTime;
            structItem.Sorter1Location = item.Sorter1Location;
            structItem.Sorter2Location = item.Sorter2Location;
            structItem.WeightLimitIgnoreCount = item.WeightLimitIgnoreCount;
            structItem.Serial3BaudRate = item.Serial3BaudRate;
            structItem.Serial3Mode = item.Serial3Mode;
            structItem.EthernetLocalPort = item.EthernetLocalPort;
            structItem.EthernetMode = item.EthernetMode;
            structItem.EthernetOperationMode = item.EthernetOperationMode;
            structItem.EthernetServerPort = item.EthernetServerPort;
            structItem.ModbusTcpAddress1 = item.ModbusTcpAddress1;
            structItem.ModbusTcpAddress2 = item.ModbusTcpAddress2;
            structItem.ModbusTcpStartAddress = item.ModbusTcpStartAddress;
            structItem.NumberOfLoginFailures = item.NumberOfLoginFailures;
            structItem.FeedbackSampleCount = 0;
            structItem.FeedbackIgnorePackages = 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;
            // string
            structItem.Unit = item.Unit;
            structItem.EmptyWeight = item.EmptyWeight;
            structItem.SerialNumber = item.SerialNumber;
            structItem.User_Level1_Name = item.User_Level1_Name;
            structItem.User_Level2_Name = item.User_Level2_Name;
            structItem.User_Level3_Name = item.User_Level3_Name;
            structItem.EthernetServerAddress = item.EthernetServerAddress;
            structItem.dummyString1 = "";
            structItem.dummyString2 = "";
            structItem.dummyString3 = "";
            structItem.dummyString4 = "";
            structItem.dummyString5 = "";
            structItem.dummyString6 = "";
            structItem.dummyString7 = "";
            structItem.dummyString8 = "";
            structItem.dummyString9 = "";
            structItem.dummyString10 = "";
            // enum
            structItem.Language = item.Language;

            this.ParentForm.smartFileIO.WriteStructure(structItem, 0);
            this.ParentForm.smartFileIO.Close(); 
        }
        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.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.Sorter1DelayTime = this.ParentForm.CurrentJudgmentSetItem.Sorter1DelayTime;
                structItem.Sorter2DelayTime = this.ParentForm.CurrentJudgmentSetItem.Sorter2DelayTime;
                structItem.Sorter2RunTime = this.ParentForm.CurrentJudgmentSetItem.Sorter2RunTime;
                structItem.EntryGateDelayTime = this.ParentForm.CurrentJudgmentSetItem.EntryGateDelayTime;
                structItem.EntryGateRunTime = this.ParentForm.CurrentJudgmentSetItem.EntryGateRunTime;
                structItem.DischargeStopperDelayTime = this.ParentForm.CurrentJudgmentSetItem.DischargeStopperDelayTime;
                structItem.DischargeStopperRunTime = this.ParentForm.CurrentJudgmentSetItem.DischargeStopperRunTime;

                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();
        }
        public 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 ResetUserListFile()
        {
            UserManager.UserManager_UserAllDel();
        }
        #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
    }
}