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 INT69DB_2A_ImageDll;

namespace INT69DB_2A.Forms
{
    public partial class FormFactoryReset : Form
    {
        #region Field
        private FormMain m_ParentForm;

        private int Time = 0;
        #endregion

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

            this.ParentForm = parent;

            this.InitializeDesign();
            this.DefaultSetting();
        }
        #endregion

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

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

            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 OFF the power and then turn ON the power";
                this.labelResetFail.Text = "Initialization failed";
            }
            else if (this.ParentForm.SystemConfig.Language == DataStore.LanguageID.Chinese)
            {
                this.labelTitle.Text = "回复出厂设置";
                this.labelResetWait.Text = "请稍等...";
                this.labelResetRestart.Text = "关掉电源，然后在打开电源";
                this.labelResetFail.Text = "初始化失敗";
            }
            else if (this.ParentForm.SystemConfig.Language == DataStore.LanguageID.Czech)
            {

            }
            else
            {

            }
        }
        private void DefaultSetting()
        {
            this.progressBarResetBar.Value = 0;
        }

        private void TransmissionRemoveFile()
        {
            bool removeFolderCheck = false;
            FileInfo[] oldFiles, removeOldFiles;
            DirectoryInfo directoryFolderSystem2 = new DirectoryInfo(this.ParentForm.PathSystemFileFolder2);
            DirectoryInfo directoryFolderRemove = new DirectoryInfo(this.ParentForm.PathRemoveFileFolder);

            removeFolderCheck = directoryFolderRemove.Exists;
            oldFiles = directoryFolderSystem2.GetFiles();

            if (removeFolderCheck == false)
                directoryFolderRemove.Create();
            else
            {
                removeOldFiles = directoryFolderRemove.GetFiles();

                if (removeOldFiles.Length >= 100)
                {
                    foreach (FileInfo subFile in removeOldFiles)
                        subFile.Delete();
                }
            }

            foreach (FileInfo subFile in oldFiles)
                subFile.MoveTo(this.ParentForm.PathRemoveFileFolder + DateTime.Now.ToString("yyyyMMddHHmmss") + subFile.Name.ToString());
        }
        public void ResetConfigurationFile()
        {
            string fullFilePath = "";
            bool fileCheck = false;
            StructSystemConfigurationItem 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.IsPingTimer = this.ParentForm.SystemConfig.IsPingTimer;
            structItem.IsFeedingConveyor = this.ParentForm.SystemConfig.IsFeedingConveyor;
            structItem.IsEachNG = this.ParentForm.SystemConfig.IsEachNG;
            structItem.IsDischargeConveyor = this.ParentForm.SystemConfig.IsDischargeConveyor;
            structItem.IsImpellerMotorDirectionCW = this.ParentForm.SystemConfig.IsImpellerMotorDirectionCW;
            structItem.IsPart11 = false;
            structItem.IsWeightViewForward = this.ParentForm.SystemConfig.IsWeightViewForward;
            structItem.DecimalPlaces = this.ParentForm.SystemConfig.DecimalPlaces;
            structItem.EthernetPort = this.ParentForm.SystemConfig.EthernetPort;
            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.AutomaticLogout = 1;
            structItem.DischargeConveyorDirectionCW = (int)this.ParentForm.SystemConfig.DischargeConveyorDirectionCW;
            structItem.UsbID = this.ParentForm.SystemConfig.UsbID;
            structItem.DummyString6 = "";
            structItem.Unit = this.ParentForm.SystemConfig.Unit;
            structItem.Language = this.ParentForm.SystemConfig.Language;
            structItem.ImpellerMotorBaudrate = this.ParentForm.SystemConfig.ImpellerMotorBaudrate;
            structItem.ImpellerMotorAnglePass = this.ParentForm.SystemConfig.ImpellerMotorAnglePass;
            structItem.SerialNumber = this.ParentForm.SystemConfig.SerialNumber;

            // Dummy bool
            structItem.DummyBool1 = false;
            structItem.DummyBool2 = false;
            structItem.IsLogin = this.ParentForm.SystemConfig.IsLogin;
            structItem.IsFeedingConveyorRunPass = this.ParentForm.SystemConfig.IsFeedingConveyorRunPass;

            // Dummy int
            structItem.StepMotorType = this.ParentForm.SystemConfig.StepMotorType;
            structItem.ImpellerMotorAngleNG = this.ParentForm.SystemConfig.ImpellerMotorAngleNG;
            structItem.InputSensorSelect = this.ParentForm.SystemConfig.InputSensorSelect;
            structItem.Serial3BaudRate = this.ParentForm.SystemConfig.Serial3BaudRate;
            structItem.Serial3Mode = this.ParentForm.SystemConfig.Serial3Mode;

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

            this.ParentForm.smartFileIO.WriteStructure(structItem, 0);
            this.ParentForm.smartFileIO.Close();
        }
        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);

                if (i % 50 == 0)
                    this.progressBarResetBar.Value += 1;
            }
            this.ParentForm.smartFileIO.WriteStructure_End();
            this.ParentForm.smartFileIO.Close();
        }
        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.FeedSpeed = this.ParentForm.CurrentJudgmentSetItem.FeedSpeed;
                structItem.DynamicCorrection = this.ParentForm.CurrentJudgmentSetItem.DynamicCorrection;
                structItem.RejectConveyorDelayTime = this.ParentForm.CurrentJudgmentSetItem.RejectConveyorDelayTime;
                structItem.RejectConveyorOperatingTime = 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);

                if (i % 50 == 0)
                    this.progressBarResetBar.Value += 1;
            }
            this.ParentForm.smartFileIO.WriteStructure_End();
            this.ParentForm.smartFileIO.Close();
        }
        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);

                if (i % 50 == 0)
                    this.progressBarResetBar.Value += 1;
            }
            this.ParentForm.smartFileIO.WriteStructure_End();
            this.ParentForm.smartFileIO.Close();
        }

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

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

            this.progressBarResetBar.Value = 0;
            this.TransmissionRemoveFile();
            this.progressBarResetBar.Value += 10;
            this.ResetProductFile();
            this.progressBarResetBar.Value += 10;
            this.ResetJudgmentSetFile();
            this.progressBarResetBar.Value += 10;
            this.ResetCountFile();
            this.progressBarResetBar.Value = 100;

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