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

using INT51DB.Forms;
using INT51DB.DialogForms;
using INT51DB_ImageDll;

namespace INT51DB.Controls
{
    public partial class ControlCenterEquipInitialize : UserControl
    {
        #region Field
        private FormMenu m_ParentForm;
        private DataStore.Step3 InitializeStep;

        private int FlagTimeout;
        #endregion

        #region Constructor
        public ControlCenterEquipInitialize(FormMenu parent)
        {
            InitializeComponent();

            this.ParentForm = parent;

            this.InitializeDesign();
        }
        #endregion

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

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

            if (this.ParentForm.ParentForm.SystemConfig.Language == DataStore.LanguageID.English)
            {
            }
            else if (this.ParentForm.ParentForm.SystemConfig.Language == DataStore.LanguageID.Chinese)
            {
            }
            else if (this.ParentForm.ParentForm.SystemConfig.Language == DataStore.LanguageID.Czech)
            {
            }
            else
            {
            }
        }
        private void DefaultSetting()
        {
            this.FlagTimeout = 0;
            this.timer.Interval = 500;

            this.progressBarInitialize.Value = 0;

            this.InitializeStep = DataStore.Step3.Step1;
            this.UpdateLabelStatus(this.InitializeStep, this.ParentForm.ParentForm.SystemConfig.Language);
        }

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

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

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

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

            this.ParentForm.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.ParentForm.CurrentProductItem.OverRange;
                structItem.PassRange = this.ParentForm.ParentForm.CurrentProductItem.PassRange;
                structItem.UnderRange = this.ParentForm.ParentForm.CurrentProductItem.UnderRange;
                structItem.TareRange = this.ParentForm.ParentForm.CurrentProductItem.TareRange;
                structItem.DummyString1 = "";
                structItem.DummyString2 = "";
                structItem.DummyString3 = "";
                structItem.DummyString4 = "";

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

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

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

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

            this.ParentForm.ParentForm.CurrentJudgmentSetItem.Initialization();

            for (int i = 0; i < 1000; i++)
            {
                structItem.Filter = this.ParentForm.ParentForm.CurrentJudgmentSetItem.Filter;
                structItem.DownDelayTime = this.ParentForm.ParentForm.CurrentJudgmentSetItem.DownDelayTime;
                structItem.JudgmentDelayTime = this.ParentForm.ParentForm.CurrentJudgmentSetItem.JudgmentDelayTime;
                structItem.UpDelayTime = this.ParentForm.ParentForm.CurrentJudgmentSetItem.UpDelayTime;
                structItem.JudgmentCount = this.ParentForm.ParentForm.CurrentJudgmentSetItem.JudgmentCount;
                structItem.FeedSpeed = this.ParentForm.ParentForm.CurrentJudgmentSetItem.FeedSpeed;
                structItem.DynamicCorrection = this.ParentForm.ParentForm.CurrentJudgmentSetItem.DynamicCorrection;

                structItem.Sorter1Mode = this.ParentForm.ParentForm.CurrentJudgmentSetItem.Sorter1Mode;
                structItem.Sorter1DelayTime = this.ParentForm.ParentForm.CurrentJudgmentSetItem.Sorter1DelayTime;
                structItem.Sorter1RunTime = this.ParentForm.ParentForm.CurrentJudgmentSetItem.Sorter1RunTime;
                structItem.Sorter2Mode = this.ParentForm.ParentForm.CurrentJudgmentSetItem.Sorter2Mode;
                structItem.Sorter2DelayTime = this.ParentForm.ParentForm.CurrentJudgmentSetItem.Sorter2DelayTime;
                structItem.Sorter2RunTime = this.ParentForm.ParentForm.CurrentJudgmentSetItem.Sorter2RunTime;
                structItem.DummyInt1 = 0;
                structItem.DummyInt2 = 0;
                structItem.DummyInt3 = 0;
                structItem.DummyInt4 = 0;
                structItem.DummyInt5 = 0;
                structItem.DummyString1 = "";
                structItem.DummyString2 = "";
                structItem.DummyString3 = "";
                structItem.DummyString4 = "";
                structItem.DummyString5 = "";

                this.ParentForm.ParentForm.smartFileIO.WriteStructure_Begin(structItem, i);
            }
            this.ParentForm.ParentForm.smartFileIO.WriteStructure_End();
            this.ParentForm.ParentForm.smartFileIO.Close();
        }
        private void ResetCountFile()
        {
            string fullFilePath = "";
            bool fileCheck = false;
            StructCounterItem structItem;
            WeightData item = new WeightData();

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

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

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

            item.ClearCount();

            for (int i = 0; i < 1000; i++)
            {
                structItem.OverCount = item.OverCount;
                structItem.OverSumWeight = item.OverSumWeight;
                structItem.PassCount = item.PassCount;
                structItem.PassSumWeight = item.PassSumWeight;
                structItem.UnderCount = item.UnderCount;
                structItem.UnderSumWeight = item.UnderSumWeight;
                structItem.ExNGCount = item.ExNGCount;

                structItem.NormalDistribution1 = item.CollectionNormalDistributionCount[0];
                structItem.NormalDistribution2 = item.CollectionNormalDistributionCount[1];
                structItem.NormalDistribution3 = item.CollectionNormalDistributionCount[2];
                structItem.NormalDistribution4 = item.CollectionNormalDistributionCount[3];
                structItem.NormalDistribution5 = item.CollectionNormalDistributionCount[4];
                structItem.NormalDistribution6 = item.CollectionNormalDistributionCount[5];
                structItem.NormalDistribution7 = item.CollectionNormalDistributionCount[6];
                structItem.NormalDistribution8 = item.CollectionNormalDistributionCount[7];
                structItem.NormalDistribution9 = item.CollectionNormalDistributionCount[8];
                structItem.NormalDistribution10 = item.CollectionNormalDistributionCount[9];

                structItem.StartTime = item.StartTime;
                structItem.StopTime = item.StopTime;

                structItem.DummyInt1 = 0;
                structItem.DummyInt2 = 0;
                structItem.DummyInt3 = 0;
                structItem.DummyInt4 = 0;
                structItem.DummyInt5 = 0;

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

        private void InitializeMainBoard(DialogResult result)
        {
            if (result == DialogResult.Yes)
            {
                this.timer.Enabled = true;
                this.progressBarInitialize.Value = 0;

                this.InitializeStep = DataStore.Step3.Step2;
                this.UpdateLabelStatus(this.InitializeStep, this.ParentForm.ParentForm.SystemConfig.Language);

                this.ParentForm.ParentForm.TransferData(CommunicationCommand.Initialization, CommunicationID.MainBoard);
            }
        }
        private void InitializeLcd(DialogResult result)
        {
            if (result == DialogResult.Yes)
            {
                this.InitializeStep = DataStore.Step3.Step2;
                this.UpdateLabelStatus(this.InitializeStep, this.ParentForm.ParentForm.SystemConfig.Language);

                this.progressBarInitialize.Value = 0;
                this.progressBarInitialize.Value += 20;
                //this.ResetConfigurationFile();
                this.progressBarInitialize.Value += 20;
                this.ResetProductFile();
                this.progressBarInitialize.Value += 20;
                this.ResetJudgmentSetFile();
                this.progressBarInitialize.Value += 20;
                this.ResetCountFile();
                this.progressBarInitialize.Value = 100;

                this.InitializeStep = DataStore.Step3.Step3;
                this.UpdateLabelStatus(this.InitializeStep, this.ParentForm.ParentForm.SystemConfig.Language);

                this.ParentForm.Enabled = false;
            }
        }
        private void InitializeMainBoardLcd(DialogResult result)
        {
            if (result == DialogResult.Yes)
            {
                this.InitializeStep = DataStore.Step3.Step2;
                this.UpdateLabelStatus(this.InitializeStep, this.ParentForm.ParentForm.SystemConfig.Language);

                this.progressBarInitialize.Value = 0;
                //this.ResetConfigurationFile();
                this.progressBarInitialize.Value += 15;
                this.ResetProductFile();
                this.progressBarInitialize.Value += 15;
                this.ResetJudgmentSetFile();
                this.progressBarInitialize.Value += 15;
                this.ResetCountFile();
                this.progressBarInitialize.Value += 15;

                this.timer.Enabled = true;
                this.ParentForm.ParentForm.TransferData(CommunicationCommand.Initialization, CommunicationID.MainBoard);
            }
        }

        private void UpdateLabelStatus(DataStore.Step3 step, DataStore.LanguageID language)
        {
            switch (step)
            {
                case DataStore.Step3.Step1:
                    #region Step1
                    switch (language)
                    {
                        case DataStore.LanguageID.Korean:
                            this.labelMessage1.Text = "";
                            this.labelMessage2.Text = "";
                            break;
                        case DataStore.LanguageID.English:
                            this.labelMessage1.Text = "";
                            this.labelMessage2.Text = "";
                            break;
                        case DataStore.LanguageID.Chinese:
                            break;
                        case DataStore.LanguageID.Czech:
                            break;
                        default:
                            break;
                    }
                    #endregion
                    break;
                case DataStore.Step3.Step2:
                    #region Step2
                    switch (language)
                    {
                        case DataStore.LanguageID.Korean:
                            this.labelMessage1.Text = "초기화 중...";
                            this.labelMessage2.Text = "";
                            break;
                        case DataStore.LanguageID.English:
                            this.labelMessage1.Text = "Initializing...";
                            this.labelMessage2.Text = "";
                            break;
                        case DataStore.LanguageID.Chinese:
                            break;
                        case DataStore.LanguageID.Czech:
                            break;
                        default:
                            break;
                    }
                    #endregion
                    break;
                case DataStore.Step3.Step3:
                    #region Step3
                    switch (language)
                    {
                        case DataStore.LanguageID.Korean:
                            this.labelMessage1.Text = "초기화 완료";
                            this.labelMessage2.Text = "전원을 끄고 다시 켜주세요.";
                            break;
                        case DataStore.LanguageID.English:
                            this.labelMessage1.Text = "Initialization complete";
                            this.labelMessage2.Text = "Reset the power.";
                            break;
                        case DataStore.LanguageID.Chinese:
                            break;
                        case DataStore.LanguageID.Czech:
                            break;
                        default:
                            break;
                    }
                    #endregion
                    break;
                default:
                    break;
            }

            this.Refresh();
        }
        public void UpdateDisplayMainBoardInitialization()
        {
            this.timer.Enabled = false;

            this.progressBarInitialize.Value = 100;

            this.InitializeStep = DataStore.Step3.Step3;
            this.UpdateLabelStatus(this.InitializeStep, this.ParentForm.ParentForm.SystemConfig.Language);

            this.ParentForm.Enabled = false;
        }

        public void DisplayRefresh(DataStore.UserGroup group)
        {
            this.ParentForm.ParentForm.CurrentSystemStatus.CurrentDisplay = DataStore.DisplayStore.EquipInitialize;
            this.ParentForm.ParentForm.SetDisplayMode(DataStore.DisplayMode.Menu);
            this.ParentForm.DisplayTitleRoot(this.ParentForm.ParentForm.CurrentSystemStatus);

            this.DefaultSetting();
        }
        #endregion

        #region Event Handler
        private void buttonMainBoardLcd_Click(object sender, EventArgs e)
        {
            // 설정된 모든 데이터가 초기화됩니다.
            DialogFormYesNo myDlg = new DialogFormYesNo(this.ParentForm.ParentForm.SystemConfig.Language, 5);
            DialogResult dr = myDlg.ShowDialog();
            myDlg.Close();
            
            this.Refresh();
            this.InitializeMainBoardLcd(dr);
        }

        private void buttonMainBoard_Click(object sender, EventArgs e)
        {
            // 메인보드에 설정된 데이터가 초기화됩니다.
            DialogFormYesNo myDlg = new DialogFormYesNo(this.ParentForm.ParentForm.SystemConfig.Language, 7);
            DialogResult dr = myDlg.ShowDialog();
            myDlg.Close();

            this.Refresh();
            this.InitializeMainBoard(dr);
        }

        private void buttonLcd_Click(object sender, EventArgs e)
        {
            // LCD에 설정된 데이터가 초기화됩니다.
            DialogFormYesNo myDlg = new DialogFormYesNo(this.ParentForm.ParentForm.SystemConfig.Language, 6);
            DialogResult dr = myDlg.ShowDialog();
            myDlg.Close();

            this.Refresh();
            this.InitializeLcd(dr);
        }    

        private void timer_Tick(object sender, EventArgs e)
        {
            this.FlagTimeout++;
            if (this.FlagTimeout == 10)
            {
                this.timer.Enabled = false;

                // 메인보드 통신 상태 확인해주세요
                DialogFormMessage message = new DialogFormMessage(22, this.ParentForm.ParentForm.SystemConfig.Language);
                message.ShowDialog();
            }

            this.progressBarInitialize.Value += 2;
        }
        #endregion
    }
}
