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

using SmartX;
using ITC81DB_2.Controls;
using ITC81DB_2.DialogForms;
using ITC81DB_2_ImageDll;

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

        private List<bool> AlarmListBool;

        private DataStore.MainDisplayStore CurrentDisplay;

        public ControlMainDisplayNormal MainDisplayNormal;
        public ControlMainDisplayDataStatistics MainDisplayDataStatistics;
        public ControlMainDisplayList MainDisplayList;
        public ControlMainDisplayXYChart MainDisplayXYChart;
        public ControlMainDisplayMenu MainDisplayMenu;
        public ControlMainDisplayAlarm MainDisplayAlarm;
        #endregion

        #region Constructor
        public FormMainDisplay(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
        public void InitializeDesign()
        {
            Class1 images = new Class1();

            if (this.ParentForm.SystemConfig.Language == DataStore.LanguageID.English)
            {
                this.buttonZero.DisableImage = new Bitmap(images.GetImage(Class1.ButtonImages.engFormMainDisplayZeroDisable));
                this.buttonZero.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.engFormMainDisplayZeroDown));
                this.buttonZero.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.engFormMainDisplayZeroUp));
                this.buttonWeightInputMode.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.engFormMainDisplayDeviationUp));
                this.buttonWeightInputMode.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.engFormMainDisplayWeightUp));
                this.buttonClear.DisableImage = new Bitmap(images.GetImage(Class1.ButtonImages.engFormMainDisplayClearDisable));
                this.buttonClear.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.engFormMainDisplayClearDown));
                this.buttonClear.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.engFormMainDisplayClearUp));
                this.buttonUser.DisableImage = new Bitmap(images.GetImage(Class1.ButtonImages.engFormMainDisplayLogOffDisable));
                this.buttonUser.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.engFormMainDisplayLogOffDown));
                this.buttonUser.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.engFormMainDisplayLogOffUp));
            }
            else if (this.ParentForm.SystemConfig.Language == DataStore.LanguageID.Chinese)
            {
                this.buttonZero.DisableImage = new Bitmap(images.GetImage(Class1.ButtonImages.chnMainZeroDisable));
                this.buttonZero.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.chnMainZeroDown));
                this.buttonZero.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.chnMainZeroUp));
                this.buttonWeightInputMode.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.chnMainDeviationUp));
                this.buttonWeightInputMode.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.chnMainWeightUp));
                this.buttonClear.DisableImage = new Bitmap(images.GetImage(Class1.ButtonImages.chnMainClearDisable));
                this.buttonClear.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.chnMainClearDown));
                this.buttonClear.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.chnMainClearUp));
                this.buttonUser.DisableImage = new Bitmap(images.GetImage(Class1.ButtonImages.chnMainLogOffDisable));
                this.buttonUser.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.chnMainLogOffDown));
                this.buttonUser.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.chnMainLogOffUp));
            }
            else if (this.ParentForm.SystemConfig.Language == DataStore.LanguageID.Czech)
            {
            }
            else
            {
                this.buttonZero.DisableImage = new Bitmap(images.GetImage(Class1.ButtonImages.korFormMainDisplayZeroDisable));
                this.buttonZero.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.korFormMainDisplayZeroDown));
                this.buttonZero.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.korFormMainDisplayZeroUp));
                this.buttonWeightInputMode.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.korFormMainDisplayDeviationUp));
                this.buttonWeightInputMode.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.korFormMainDisplayWeightUp));
                this.buttonClear.DisableImage = new Bitmap(images.GetImage(Class1.ButtonImages.korFormMainDisplayClearDisable));
                this.buttonClear.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.korFormMainDisplayClearDown));
                this.buttonClear.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.korFormMainDisplayClearUp));
                this.buttonUser.DisableImage = new Bitmap(images.GetImage(Class1.ButtonImages.engFormMainDisplayLogOffDisable));
                this.buttonUser.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.engFormMainDisplayLogOffDown));
                this.buttonUser.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.engFormMainDisplayLogOffUp));
            }

            if (this.ParentForm.CurrentSystemStatus.CurrentWeightInputMode == DataStore.WeightInputMode.Deviation)
                this.buttonWeightInputMode.ButtonDown();
            else
                this.buttonWeightInputMode.ButtonUp();
        }
        private void InitializeControl()
        {
            this.MainDisplayNormal = new ControlMainDisplayNormal(this);
            this.MainDisplayDataStatistics = new ControlMainDisplayDataStatistics(this);
            this.MainDisplayList = new ControlMainDisplayList(this);
            this.MainDisplayXYChart = new ControlMainDisplayXYChart(this);
            this.MainDisplayMenu = new ControlMainDisplayMenu(this);
            this.MainDisplayAlarm = new ControlMainDisplayAlarm();

            this.MainDisplayNormal.Location = new Point(0, 90);
            this.MainDisplayDataStatistics.Location = new Point(0, 90);
            this.MainDisplayList.Location = new Point(0, 90);
            this.MainDisplayXYChart.Location = new Point(0, 90);
            this.MainDisplayMenu.Location = new Point(0, 90);
            this.MainDisplayAlarm.Location = new Point(390, 60);

            this.Controls.Add(this.MainDisplayNormal);
            this.Controls.Add(this.MainDisplayDataStatistics);
            this.Controls.Add(this.MainDisplayList);
            this.Controls.Add(this.MainDisplayXYChart);
            this.Controls.Add(this.MainDisplayMenu);
            this.Controls.Add(this.MainDisplayAlarm);

            if (this.ParentForm.SystemConfig.IsLogin == true)
            {
                this.labelUserID.Visible = true;
                this.labelUserLevel.Visible = true;
                this.buttonUser.Visible = true;
            }
            else
            {
                this.labelUserID.Visible = false;
                this.labelUserLevel.Visible = false;
                this.buttonUser.Visible = false;
            }
        }
        private void DefaultSetting()
        {
            if (this.ParentForm.IsBootingComplete == false)
                this.buttonProductNo.Text = "**";
            else
                this.buttonProductNo.Text = this.ParentForm.SystemConfig.ProductNumber.ToString();

            this.pictureBoxStop.Visible = true;
            this.pictureBoxStart.Visible = false;

            this.InitializeControl();
            this.UpdateDisplay();
            this.DisplayPrintButton(this.ParentForm.SystemConfig);

            // 알람 표시
            this.pictureBoxIconAlarm.Visible = false;
            this.MainDisplayAlarm.Visible = false;
            this.MainDisplayAlarm.BringToFront();
            this.AlarmListBool = new List<bool>();

            if (this.ParentForm.SystemConfig.EquipmentType == 4)
                this.UpdateControlVisible(true);
            else
                this.UpdateControlVisible(false);

            if (this.ParentForm.SystemConfig.IsLogin == true)
                this.UpdateDisplayUser(this.ParentForm.CurrentSystemStatus);
            else
                this.ParentForm.CurrentSystemStatus.CurrentUser.Group = DataStore.UserGroup.Level4Developer;
                        
            this.CurrentDisplay = DataStore.MainDisplayStore.DisplayNormal;
            this.DisplayBottomMain(this.CurrentDisplay);
        }

        private void UpdateDisplayAlarmView(WeightData data1, WeightData data2)
        {
            bool flagAlarm = false;
            bool flagChange = false;

            if (this.AlarmListBool.Count > 0)
            {
                if (this.AlarmListBool[0] != data1.IsLoadCellError) flagChange = true;
                if (this.AlarmListBool[1] != data2.IsLoadCellError) flagChange = true;
                if (this.AlarmListBool[2] != data1.IsEmergencyStop) flagChange = true;
                if (this.AlarmListBool[3] != data1.IsEntrySensorError) flagChange = true;
                if (this.AlarmListBool[4] != data2.IsEntrySensorError) flagChange = true;
                if (this.AlarmListBool[5] != data1.IsEntryNotDetected) flagChange = true;
                if (this.AlarmListBool[6] != data2.IsEntryNotDetected) flagChange = true;
                if (this.AlarmListBool[7] != data1.IsEntryStucked) flagChange = true;
                if (this.AlarmListBool[8] != data2.IsEntryStucked) flagChange = true;
            }

            this.AlarmListBool.Clear();
            this.AlarmListBool.Add(data1.IsLoadCellError);
            this.AlarmListBool.Add(data2.IsLoadCellError);
            this.AlarmListBool.Add(data1.IsEmergencyStop);
            this.AlarmListBool.Add(data1.IsEntrySensorError);
            this.AlarmListBool.Add(data2.IsEntrySensorError);
            this.AlarmListBool.Add(data1.IsEntryNotDetected);
            this.AlarmListBool.Add(data2.IsEntryNotDetected);
            this.AlarmListBool.Add(data1.IsEntryStucked);
            this.AlarmListBool.Add(data2.IsEntryStucked);

            foreach (bool listBool in this.AlarmListBool)
            {
                if (listBool == true)
                {
                    flagAlarm = true;
                    break;
                }
            }

            if (flagAlarm == true)
            {
                if (this.pictureBoxIconAlarm.Visible == false)
                    this.pictureBoxIconAlarm.Visible = true;
            }
            else
            {
                this.pictureBoxIconAlarm.Visible = false;
                this.MainDisplayAlarm.Visible = false;

                return;
            }

            this.MainDisplayAlarm.listBoxMessage.Items.Clear();

            if (data1.IsLoadCellError == true)
                this.MainDisplayAlarm.listBoxMessage.Items.Add("#1 LoadCell Error");
            if (data2.IsLoadCellError == true)
                this.MainDisplayAlarm.listBoxMessage.Items.Add("#2 LoadCell Error");
            if (data1.IsEmergencyStop == true)
                this.MainDisplayAlarm.listBoxMessage.Items.Add("Emergency Stop");
            if (data1.IsEntrySensorError == true)
                this.MainDisplayAlarm.listBoxMessage.Items.Add("#1 Entry Sensor Error");
            if (data2.IsEntrySensorError == true)
                this.MainDisplayAlarm.listBoxMessage.Items.Add("#2 Entry Sensor Error");
            if (data1.IsEntryNotDetected == true)
            {
                this.MainDisplayAlarm.listBoxMessage.Items.Add("#1 Entry Not Detected");
                if (this.ParentForm.SystemConfig.Language == DataStore.LanguageID.Korean)
                    this.PopUpWarning("#1 제품진입 미감지");
                else
                    this.PopUpWarning("#1 Entry not detected");
            }
            if (data2.IsEntryNotDetected == true)
            {
                this.MainDisplayAlarm.listBoxMessage.Items.Add("#2 Entry Not Detected");
                if (this.ParentForm.SystemConfig.Language == DataStore.LanguageID.Korean)
                    this.PopUpWarning("#2 제품진입 미감지");
                else
                    this.PopUpWarning("#2 Entry not detected");
            }
            if (data1.IsEntryStucked == true)
            {
                this.MainDisplayAlarm.listBoxMessage.Items.Add("#1 Entry Sensor Stucked");
                if (this.ParentForm.SystemConfig.Language == DataStore.LanguageID.Korean)
                    this.PopUpWarning("#1 진입센서 이상");
                else
                    this.PopUpWarning("#1 Entry Sensor Stucked");
            }
            if (data2.IsEntryStucked == true)
            {
                this.MainDisplayAlarm.listBoxMessage.Items.Add("#2 Entry Sensor Stucked");
                if (this.ParentForm.SystemConfig.Language == DataStore.LanguageID.Korean)
                    this.PopUpWarning("#2 진입센서 이상");
                else
                    this.PopUpWarning("#2 Entry Sensor Stucked");
            }

            if (flagChange == true && this.MainDisplayAlarm.listBoxMessage.Items.Count > 0)
            {
                // 로그
                if (this.ParentForm.IsCommunicationLogOpen == true)
                {
                    for (int i = 0; i < this.MainDisplayAlarm.listBoxMessage.Items.Count; i++)
                        this.ParentForm.smartFileCommunicationLog.WriteString(
                            string.Format("Alarm ({0:yyyy-MM-dd HH:mm:ss}): {1}", DateTime.Now, this.MainDisplayAlarm.listBoxMessage.Items[i]));
                }
            }
        }
        private void ProductChange(int productNumber)
        {
            this.buttonProductNo.Text = "**";

            this.ParentForm.TransferProductParameter1(productNumber);
        }
        public void RescaleControl()
        {
            this.MainDisplayNormal.RescaleProgressBar1();
            this.MainDisplayNormal.RescaleProgressBar2();
        }
        public void Clear()
        {
            this.ParentForm.CurrentWeightData1.ClearCount();
            this.ParentForm.CurrentWeightData2.ClearCount();
            this.ParentForm.SaveCounterFile1(this.ParentForm.CurrentWeightData1, this.ParentForm.SystemConfig.ProductNumber - 1);
            this.ParentForm.SaveCounterFile2(this.ParentForm.CurrentWeightData2, this.ParentForm.SystemConfig.ProductNumber - 1);

            if (this.MainDisplayNormal != null)
                this.MainDisplayNormal.Clear();

            if (this.MainDisplayDataStatistics != null)
                this.MainDisplayDataStatistics.Clear();

            if (this.MainDisplayList != null)
                this.MainDisplayList.Clear();

            if (this.MainDisplayXYChart != null)
                this.MainDisplayXYChart.Clear();
        }

        public void DisplayPrintButton(SystemConfigurationItem system)
        {
            if (system.SerialCOM1Mode == 3 || system.SerialCOM3Mode == 3)
            {
                this.buttonPrint1.Visible = true;
                this.buttonPrint2.Visible = true;
            }
            else
            {
                this.buttonPrint1.Visible = false;
                this.buttonPrint2.Visible = false;
            }
        }
        private void UpdateDisplay()
        {
            // Icon - 데이터 백업
            if (this.ParentForm.SystemConfig.IsDataBackup == true)
            {
                this.pictureBoxIconUsbEnable.Visible = true;
                this.pictureBoxIconUsbDesable.Visible = false;
            }
            else
            {
                this.pictureBoxIconUsbEnable.Visible = false;
                this.pictureBoxIconUsbDesable.Visible = true;
            }

            // Icon - COM1
            if (this.ParentForm.SystemConfig.SerialCOM1Mode == 0)
            {
                this.pictureBoxCOM1Enable.Visible = false;
                this.pictureBoxCOM1Disable.Visible = true;
            }
            else
            {
                this.pictureBoxCOM1Enable.Visible = true;
                this.pictureBoxCOM1Disable.Visible = false;
            }

            // Icon - COM3
            if (this.ParentForm.SystemConfig.SerialCOM3Mode == 0)
            {
                this.pictureBoxCOM3Enable.Visible = false;
                this.pictureBoxCOM3Disable.Visible = true;
            }
            else
            {
                this.pictureBoxCOM3Enable.Visible = true;
                this.pictureBoxCOM3Disable.Visible = false;
            }
        }
        private void UpdateDisplayUser(SystemStatus status) 
        {
            string id = "", group = "";

            switch (status.CurrentUser.Group)
            {
                case DataStore.UserGroup.LogOff:
                    id = "";
                    if (this.ParentForm.SystemConfig.Language == DataStore.LanguageID.Chinese)
                        group = "关闭";
                    else
                        group = "Off";
                    
                    if (this.labelUserLevel.ForeColor != this.ParentForm.ColorLogOff)
                        this.labelUserLevel.ForeColor = this.labelUserID.ForeColor = this.ParentForm.ColorLogOff;

                    this.buttonUser.ButtonUp();
                    this.buttonProductNo.Enabled = false;
                    this.buttonClear.Enabled = false;
                    this.labelProductName.Enabled = false;

                    this.buttonMenu.Enabled = false;
                    break;
                case DataStore.UserGroup.Level1Operator:
                    id = status.CurrentUser.ID;
                    if (this.ParentForm.SystemConfig.Language == DataStore.LanguageID.Chinese)
                        group = "级别1";
                    else
                        group = "Level1";
                 
                    if (this.labelUserLevel.ForeColor != this.ParentForm.ColorLogOn)
                        this.labelUserLevel.ForeColor = this.labelUserID.ForeColor = this.ParentForm.ColorLogOn;

                    this.buttonUser.ButtonDown();
                    this.buttonProductNo.Enabled = true;
                    this.buttonClear.Enabled = true;
                    this.labelProductName.Enabled = true;

                    if (status.Equipment == DataStore.EquipmentStatus.Stop)
                        this.buttonMenu.Enabled = true;
                    else
                        this.buttonMenu.Enabled = false;
                    break;
                case DataStore.UserGroup.Level2Engineer:
                    id = status.CurrentUser.ID;
                    if (this.ParentForm.SystemConfig.Language == DataStore.LanguageID.Chinese)
                        group = "级别2";
                    else
                        group = "Level2";
                   
                    if (this.labelUserLevel.ForeColor != this.ParentForm.ColorLogOn)
                        this.labelUserLevel.ForeColor = this.labelUserID.ForeColor = this.ParentForm.ColorLogOn;

                    this.buttonUser.ButtonDown();
                    this.buttonProductNo.Enabled = true;
                    this.buttonClear.Enabled = true;
                    this.labelProductName.Enabled = true;

                    if (status.Equipment == DataStore.EquipmentStatus.Stop)
                        this.buttonMenu.Enabled = true;
                    else
                        this.buttonMenu.Enabled = false;
                    break;
                case DataStore.UserGroup.Level3Manager:
                    id = status.CurrentUser.ID;
                    if (this.ParentForm.SystemConfig.Language == DataStore.LanguageID.Chinese)
                        group = "级别3";
                    else
                        group = "Level3";
                   
                    if (this.labelUserLevel.ForeColor != this.ParentForm.ColorLogOn)
                        this.labelUserLevel.ForeColor = this.labelUserID.ForeColor = this.ParentForm.ColorLogOn;

                    this.buttonUser.ButtonDown();
                    this.buttonProductNo.Enabled = true;
                    this.buttonClear.Enabled = true;
                    this.labelProductName.Enabled = true;

                    if (status.Equipment == DataStore.EquipmentStatus.Stop)
                        this.buttonMenu.Enabled = true;
                    else
                        this.buttonMenu.Enabled = false;
                    break;
                case DataStore.UserGroup.Level4Developer:
                    id = status.CurrentUser.ID;
                    if (this.ParentForm.SystemConfig.Language == DataStore.LanguageID.Chinese)
                        group = "级别4";
                    else
                        group = "Level4";

                    if (this.labelUserLevel.ForeColor != this.ParentForm.ColorLogOn)
                        this.labelUserLevel.ForeColor = this.labelUserID.ForeColor = this.ParentForm.ColorLogOn;

                    this.buttonUser.ButtonDown();
                    this.buttonProductNo.Enabled = true;
                    this.buttonClear.Enabled = true;
                    this.labelProductName.Enabled = true;

                    if (status.Equipment == DataStore.EquipmentStatus.Stop)
                        this.buttonMenu.Enabled = true;
                    else
                        this.buttonMenu.Enabled = false;
                    break;
                default:
                    id = "";
                    if (this.ParentForm.SystemConfig.Language == DataStore.LanguageID.Chinese)
                        group = "关闭";
                    else
                        group = "Off";

                    if (this.labelUserLevel.ForeColor != this.ParentForm.ColorLogOff)
                        this.labelUserLevel.ForeColor = this.labelUserID.ForeColor = this.ParentForm.ColorLogOff;

                    this.buttonUser.ButtonUp();
                    this.buttonProductNo.Enabled = false;
                    this.buttonClear.Enabled = false;
                    this.buttonMenu.Enabled = false;
                    this.labelProductName.Enabled = false;
                    break;
            }

            if (this.labelUserLevel.Text != group)
                this.labelUserLevel.Text = group;
            if (this.labelUserID.Text != id)
                this.labelUserID.Text = id;
        }
        public void UpdateEquipmentStatusDisplay(DataStore.EquipmentStatus status)
        {
            this.MainDisplayNormal.UpdateEquipmentStatusDisplay(status);
            this.MainDisplayDataStatistics.UpdateEquipmentStatusDisplay(status);
            this.MainDisplayList.UpdateEquipmentStatusDisplay(status);
            this.MainDisplayXYChart.UpdateEquipmentStatusDisplay(status);
            this.MainDisplayMenu.UpdateEquipmentStatusDisplay(status);

            if (status == DataStore.EquipmentStatus.Start)
            {
                this.pictureBoxStart.Visible = true;
                this.pictureBoxStop.Visible = false;
                this.buttonProductNo.Enabled = false;
                this.labelProductName.Enabled = false;

                this.buttonConveyorUpDown.Enabled = false;

                this.CurrentBottomMenuControlEnable(status);
            }
            else
            {
                this.pictureBoxStart.Visible = false;
                this.pictureBoxStop.Visible = true;

                this.buttonConveyorUpDown.Enabled = true;
                this.buttonConveyorUpDown.ButtonUp();

                if (this.ParentForm.CurrentSystemStatus.CurrentUser.Group == DataStore.UserGroup.LogOff)
                {
                    this.buttonProductNo.Enabled = false;
                    this.labelProductName.Enabled = false;
                }
                else
                {
                    this.buttonProductNo.Enabled = true;
                    this.labelProductName.Enabled = true;
                }

                this.CurrentBottomMenuControlEnable(status);
            }
        }
        public void UpdateCurrentProductDisplay(DataStore.EquipmentStatus status, ProductItem pItem1, ProductItem pItem2, WeightData wData1, WeightData wData2)
        {
            string value = "";

            value = pItem1.Number.ToString();
            if (this.buttonProductNo.Text != value)
                this.buttonProductNo.Text = value;

            value = pItem1.Name;
            if (this.labelProductName.Text != value)
                this.labelProductName.Text = value;

            value = pItem1.LotNo;
            if (this.labelLotNo.Text != value)
                this.labelLotNo.Text = value;

            if (this.MainDisplayNormal != null)
                this.MainDisplayNormal.UpdateCurrentProductDisplay(status, pItem1, pItem2, wData1, wData2);
            if (this.MainDisplayDataStatistics != null)
                this.MainDisplayDataStatistics.UpdateCurrentProductDisplay(status, pItem1, pItem2, wData1, wData2);
            if (this.MainDisplayList != null)
                this.MainDisplayList.UpdateCurrentProductDisplay(status, pItem1, pItem2, wData1, wData2);
            if (this.MainDisplayXYChart != null)
                this.MainDisplayXYChart.UpdateCurrentProductDisplay(status, pItem1, pItem2, wData1, wData2);
            if (this.MainDisplayMenu != null)
                this.MainDisplayMenu.UpdateCurrentProductDisplay(status, pItem1, pItem2, wData1, wData2);
        }
        public void UpdateStopWeightDisplay(DataStore.EquipmentStatus status, WeightData data1, WeightData data2)
        {
            // 알람표시
            this.UpdateDisplayAlarmView(data1, data2);

            if (this.MainDisplayNormal != null)
                this.MainDisplayNormal.UpdateStopWeightDisplay(status, data1, data2);
            if (this.MainDisplayDataStatistics != null)
                this.MainDisplayDataStatistics.UpdateStopWeightDisplay(status, data1, data2);
            if (this.MainDisplayList != null)
                this.MainDisplayList.UpdateStopWeightDisplay(status, data1, data2);
            if (this.MainDisplayXYChart != null)
                this.MainDisplayXYChart.UpdateStopWeightDisplay(status, data1, data2);
            if (this.MainDisplayMenu != null)
                this.MainDisplayMenu.UpdateStopWeightDisplay(status, data1, data2);
        }
        public void UpdateStartWeightDisplay1(DataStore.EquipmentStatus status, WeightData data)
        {
            // 알람표시
            this.UpdateDisplayAlarmView(data, this.ParentForm.CurrentWeightData2);

            if (data.JudgmentStatus == DataStore.JudgmentStatus.Empty)
                return;

            if (this.MainDisplayNormal != null)
                this.MainDisplayNormal.UpdateStartWeightDisplay1(status, data);
            if (this.MainDisplayDataStatistics != null)
                this.MainDisplayDataStatistics.UpdateStartWeightDisplay1(status, data);
            if (this.MainDisplayList != null)
                this.MainDisplayList.UpdateStartWeightDisplay1(status, data);
            if (this.MainDisplayXYChart != null)
                this.MainDisplayXYChart.UpdateStartWeightDisplay1(status, data);
            if (this.MainDisplayMenu != null)
                this.MainDisplayMenu.UpdateStartWeightDisplay1(status, data);
        }
        public void UpdateStartWeightDisplay2(DataStore.EquipmentStatus status, WeightData data)
        {
            // 알람표시
            this.UpdateDisplayAlarmView(this.ParentForm.CurrentWeightData1, data);

            if (data.JudgmentStatus == DataStore.JudgmentStatus.Empty)
                return;

            if (this.MainDisplayNormal != null)
                this.MainDisplayNormal.UpdateStartWeightDisplay2(status, data);
            if (this.MainDisplayDataStatistics != null)
                this.MainDisplayDataStatistics.UpdateStartWeightDisplay2(status, data);
            if (this.MainDisplayList != null)
                this.MainDisplayList.UpdateStartWeightDisplay2(status, data);
            if (this.MainDisplayXYChart != null)
                this.MainDisplayXYChart.UpdateStartWeightDisplay2(status, data);
            if (this.MainDisplayMenu != null)
                this.MainDisplayMenu.UpdateStartWeightDisplay2(status, data);
        }
        public void PopUpWarning(string warning)
        {
            this.labelAlarmWindow.Text = warning;
            this.labelAlarmWindow.BringToFront();
            this.labelAlarmWindow.Visible = true;
        }
        public void PopUpWarningClose()
        {
            if (this.labelAlarmWindow.Visible == true)
            {
                this.labelAlarmWindow.Visible = false;

                this.ParentForm.TransferData(CommunicationCommand.BuzzerOnContinuousDisable, CommunicationID.MainBoard);
            }
        }

        public void CurrentBottomMenuControlEnable(DataStore.EquipmentStatus status)
        {
            if (status == DataStore.EquipmentStatus.Start)
            {
                this.buttonMenu.Enabled = false;
            }
            else
            {
                if (this.ParentForm.CurrentSystemStatus.CurrentUser.Group == DataStore.UserGroup.LogOff)
                    this.buttonMenu.Enabled = false;
                else
                    this.buttonMenu.Enabled = true;
            }
        }
        public void UpdateDisplayMenuButton(DataStore.MainDisplayStore main)
        {
            switch (main)
            {
                case DataStore.MainDisplayStore.DisplayMenu:
                    if (this.buttonStop.ButtonStatus != SmartButton.BUTSTATUS.UP)
                        this.buttonStop.ButtonUp();
                    if (this.buttonStartMenu.ButtonStatus != SmartButton.BUTSTATUS.DOWN)
                        this.buttonStartMenu.ButtonDown();
                    if (this.buttonBargraph.ButtonStatus != SmartButton.BUTSTATUS.UP)
                        this.buttonBargraph.ButtonUp();
                    if (this.buttonLinegraph.ButtonStatus != SmartButton.BUTSTATUS.UP)
                        this.buttonLinegraph.ButtonUp();
                    if (this.buttonList.ButtonStatus != SmartButton.BUTSTATUS.UP)
                        this.buttonList.ButtonUp();
                    if (this.buttonDataStat.ButtonStatus != SmartButton.BUTSTATUS.UP)
                        this.buttonDataStat.ButtonUp();
                    break;
                case DataStore.MainDisplayStore.DisplayNormal:
                    if (this.buttonStop.ButtonStatus != SmartButton.BUTSTATUS.UP)
                        this.buttonStop.ButtonUp();
                    if (this.buttonStartMenu.ButtonStatus != SmartButton.BUTSTATUS.UP)
                        this.buttonStartMenu.ButtonUp();
                    if (this.buttonBargraph.ButtonStatus != SmartButton.BUTSTATUS.DOWN)
                        this.buttonBargraph.ButtonDown();
                    if (this.buttonLinegraph.ButtonStatus != SmartButton.BUTSTATUS.UP)
                        this.buttonLinegraph.ButtonUp();
                    if (this.buttonList.ButtonStatus != SmartButton.BUTSTATUS.UP)
                        this.buttonList.ButtonUp();
                    if (this.buttonDataStat.ButtonStatus != SmartButton.BUTSTATUS.UP)
                        this.buttonDataStat.ButtonUp();
                    break;
                case DataStore.MainDisplayStore.DisplayXYChart:
                    if (this.buttonStop.ButtonStatus != SmartButton.BUTSTATUS.UP)
                        this.buttonStop.ButtonUp();
                    if (this.buttonStartMenu.ButtonStatus != SmartButton.BUTSTATUS.UP)
                        this.buttonStartMenu.ButtonUp();
                    if (this.buttonBargraph.ButtonStatus != SmartButton.BUTSTATUS.UP)
                        this.buttonBargraph.ButtonUp();
                    if (this.buttonLinegraph.ButtonStatus != SmartButton.BUTSTATUS.DOWN)
                        this.buttonLinegraph.ButtonDown();
                    if (this.buttonList.ButtonStatus != SmartButton.BUTSTATUS.UP)
                        this.buttonList.ButtonUp();
                    if (this.buttonDataStat.ButtonStatus != SmartButton.BUTSTATUS.UP)
                        this.buttonDataStat.ButtonUp();
                    break;
                case DataStore.MainDisplayStore.DisplayList:
                    if (this.buttonStop.ButtonStatus != SmartButton.BUTSTATUS.UP)
                        this.buttonStop.ButtonUp();
                    if (this.buttonStartMenu.ButtonStatus != SmartButton.BUTSTATUS.UP)
                        this.buttonStartMenu.ButtonUp();
                    if (this.buttonBargraph.ButtonStatus != SmartButton.BUTSTATUS.UP)
                        this.buttonBargraph.ButtonUp();
                    if (this.buttonLinegraph.ButtonStatus != SmartButton.BUTSTATUS.UP)
                        this.buttonLinegraph.ButtonUp();
                    if (this.buttonList.ButtonStatus != SmartButton.BUTSTATUS.DOWN)
                        this.buttonList.ButtonDown();
                    if (this.buttonDataStat.ButtonStatus != SmartButton.BUTSTATUS.UP)
                        this.buttonDataStat.ButtonUp();
                    break;
                case DataStore.MainDisplayStore.DisplayDataStat:
                    if (this.buttonStop.ButtonStatus != SmartButton.BUTSTATUS.UP)
                        this.buttonStop.ButtonUp();
                    if (this.buttonStartMenu.ButtonStatus != SmartButton.BUTSTATUS.UP)
                        this.buttonStartMenu.ButtonUp();
                    if (this.buttonBargraph.ButtonStatus != SmartButton.BUTSTATUS.UP)
                        this.buttonBargraph.ButtonUp();
                    if (this.buttonLinegraph.ButtonStatus != SmartButton.BUTSTATUS.UP)
                        this.buttonLinegraph.ButtonUp();
                    if (this.buttonList.ButtonStatus != SmartButton.BUTSTATUS.UP)
                        this.buttonList.ButtonUp();
                    if (this.buttonDataStat.ButtonStatus != SmartButton.BUTSTATUS.DOWN)
                        this.buttonDataStat.ButtonDown();
                    break;
                default:
                    break;
            }
        }
        public void DisplayBottomMain(DataStore.MainDisplayStore main)
        {
            switch (main)
            {
                case DataStore.MainDisplayStore.DisplayMenu:
                    this.UpdateDisplayMenuButton(DataStore.MainDisplayStore.DisplayMenu);
                    this.MainDisplayMenu.DisplayRefresh(this.ParentForm.CurrentSystemStatus);
                    this.MainDisplayMenu.BringToFront();
                    break;
                case DataStore.MainDisplayStore.DisplayNormal:
                    this.UpdateDisplayMenuButton(DataStore.MainDisplayStore.DisplayNormal);
                    this.MainDisplayNormal.DisplayRefresh(this.ParentForm.CurrentSystemStatus);
                    this.MainDisplayNormal.BringToFront();
                    break;
                case DataStore.MainDisplayStore.DisplayXYChart:
                    this.UpdateDisplayMenuButton(DataStore.MainDisplayStore.DisplayXYChart);
                    this.MainDisplayXYChart.DisplayRefresh(this.ParentForm.CurrentSystemStatus);
                    this.MainDisplayXYChart.BringToFront();
                    break;
                case DataStore.MainDisplayStore.DisplayList:
                    this.UpdateDisplayMenuButton(DataStore.MainDisplayStore.DisplayList);
                    this.MainDisplayList.DisplayRefresh(this.ParentForm.CurrentSystemStatus);
                    this.MainDisplayList.BringToFront();
                    break;
                case DataStore.MainDisplayStore.DisplayDataStat:
                    this.UpdateDisplayMenuButton(DataStore.MainDisplayStore.DisplayDataStat);
                    this.MainDisplayDataStatistics.DisplayRefresh(this.ParentForm.CurrentSystemStatus);
                    this.MainDisplayDataStatistics.BringToFront();
                    break;
                default:
                    break;
            }
        }
        public void UpdateControlVisible(bool enable)
        {
            this.buttonConveyorUpDown.Visible = enable;
        }

        public void UpdateTimeDisplay()
        {
            this.labelTime.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");        
        }

        public void DisplayRefresh(SystemStatus status)
        {
            this.UpdateDisplayUser(status);
            this.UpdateDisplay();

            this.MainDisplayNormal.DisplayRefresh(status);
            this.MainDisplayList.DisplayRefresh(status);
            this.MainDisplayMenu.DisplayRefresh(status);
            this.MainDisplayXYChart.DisplayRefresh(status);
            this.MainDisplayDataStatistics.DisplayRefresh(status);

            if (this.ParentForm.SystemConfig.EquipmentType == 4)
                this.buttonConveyorUpDown.ButtonUp();

            this.ParentForm.CurrentSystemStatus.CurrentDisplay = DataStore.DisplayStore.MainDisplay;
            this.ParentForm.SetDisplayMode(DataStore.DisplayMode.Normal);

            this.ProductChange(this.ParentForm.SystemConfig.ProductNumber);
        }
        #endregion

        #region Override Member
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            //this.InitializeDesign();
            //this.DefaultSetting();

            //this.smartMemory1.ParentWindow = this;
            //this.smartMemory1.MemoryStatusInfomationShow(SmartX.SmartMemory.VIEWPOSITION.CENTER);
        }
        #endregion

        #region Event Handler
        private void buttonZero_Click(object sender, EventArgs e)
        {
            this.ParentForm.TransferData(CommunicationCommand.Zero, CommunicationID.MainBoard);
        }

        private void buttonProductNo_Click(object sender, EventArgs e)
        {
            DialogFormNumKeyPad myKeyPad = new DialogFormNumKeyPad(this.buttonProductNo.Text, 4, 0, false, this.ParentForm.SystemConfig.Language);

            if (myKeyPad.ShowDialog() == DialogResult.OK)
            {
                if (myKeyPad.doubleValue < 1 || myKeyPad.doubleValue > 1000)
                {
                    // 입력범위를 확인하세요
                    DialogFormMessage myMsg = new DialogFormMessage(1, this.ParentForm.SystemConfig.Language);
                    myMsg.ShowDialog();
                }
                else
                {
                    this.ParentForm.SystemConfig.ProductNumber = myKeyPad.IntValue;
                    this.ParentForm.SaveSystemConfigurationFile(this.ParentForm.SystemConfig);

                    this.ProductChange(this.ParentForm.SystemConfig.ProductNumber);
                }
            }
        }
        private void labelProductName_Click(object sender, EventArgs e)
        {
            int listIndex = 0, listViewIndex = 0;
            Collection<string> productNames = new Collection<string>();

            this.ParentForm.LoadProductName1(ref productNames);

            listIndex = this.ParentForm.SystemConfig.ProductNumber - 1;
            listViewIndex = (listIndex / 10) * 10;
            if (listViewIndex > 990)
                listViewIndex = 990;

            DialogFormProductList product = new DialogFormProductList(productNames, listViewIndex);
            product.Location = new Point(79, 60);

            if (product.ShowDialog() == DialogResult.OK)
            {
                this.ParentForm.SystemConfig.ProductNumber = product.SelectedProductNumber;
                this.ParentForm.SaveSystemConfigurationFile(this.ParentForm.SystemConfig);

                this.ProductChange(product.SelectedProductNumber);
            }
        }

        private void pictureBoxStart_Click(object sender, EventArgs e)
        {
            this.ParentForm.TransferData(CommunicationCommand.Stop, CommunicationID.MainBoard);
        }
        private void pictureBoxStop_Click(object sender, EventArgs e)
        {
            this.ParentForm.TransferData(CommunicationCommand.Start, CommunicationID.MainBoard);
        }

        private void timerPrinting_Tick(object sender, EventArgs e)
        {
            //this.timerPrinting.Enabled = false;
            //this.buttonPrint.Enabled = true;
            //this.buttonClear.Enabled = true;
        }

        private void pictureBoxIconAlarm_Click(object sender, EventArgs e)
        {
            if (this.MainDisplayAlarm.Visible == true)
                this.MainDisplayAlarm.Visible = false;
            else
            {
                this.MainDisplayAlarm.BringToFront();
                this.MainDisplayAlarm.Visible = true;
            }
        }

        private void labelAlarmWindow_Click(object sender, EventArgs e)
        {
            this.PopUpWarningClose();
        }

        private void buttonPrint1_Click(object sender, EventArgs e)
        {
            if (this.buttonPrint1.ButtonStatus == SmartButton.BUTSTATUS.DOWN)
            {
                this.ParentForm.SystemConfig.IsPrint1Enable = true;

                if (this.buttonPrint2.ButtonStatus != SmartButton.BUTSTATUS.DOWN)
                    this.ParentForm.PrintingHeadLine();
            }
            else
            {
                this.ParentForm.SystemConfig.IsPrint1Enable = false;

                if (this.buttonPrint2.ButtonStatus != SmartButton.BUTSTATUS.DOWN)
                {
                    if (this.ParentForm.SystemConfig.SerialCOM1Mode == 3)
                        this.ParentForm.TransferSerialCOM1("\n\n\n\n");
                    else if (this.ParentForm.SystemConfig.SerialCOM3Mode == 3)
                        this.ParentForm.TransferSerialCOM3("\n\n\n\n");
                }
            }

            //this.buttonPrint.Enabled = false;
            //this.buttonClear.Enabled = false;
            //this.timerPrinting.Interval = 10000;
            //this.timerPrinting.Enabled = true;

            //this.ParentForm.PrintingWeight(this.ParentForm.CurrentWeightData, this.ParentForm.CurrentProductItem);
        }
        private void buttonPrint2_Click(object sender, EventArgs e)
        {
            if (this.buttonPrint2.ButtonStatus == SmartButton.BUTSTATUS.DOWN)
            {
                this.ParentForm.SystemConfig.IsPrint2Enable = true;

                if (this.buttonPrint1.ButtonStatus != SmartButton.BUTSTATUS.DOWN)
                    this.ParentForm.PrintingHeadLine();
            }
            else
            {
                this.ParentForm.SystemConfig.IsPrint2Enable = false;

                if (this.buttonPrint1.ButtonStatus != SmartButton.BUTSTATUS.DOWN)
                {
                    if (this.ParentForm.SystemConfig.SerialCOM1Mode == 3)
                        this.ParentForm.TransferSerialCOM1("\n\n\n\n");
                    else if (this.ParentForm.SystemConfig.SerialCOM3Mode == 3)
                        this.ParentForm.TransferSerialCOM3("\n\n\n\n");
                }
            }
        }
        private void buttonClear_Click(object sender, EventArgs e)
        {
            DialogFormYesNo myDlg = new DialogFormYesNo(this.ParentForm.SystemConfig.Language, 8);
            if (myDlg.ShowDialog() == DialogResult.Yes)
            {
                this.Clear();
            }
        }
        private void buttonUser_Click(object sender, EventArgs e)
        {
            if (this.ParentForm.CurrentSystemStatus.CurrentUser.Group == DataStore.UserGroup.LogOff)
            {
                DialogFormLogOn logOn = new DialogFormLogOn(this.ParentForm, this.ParentForm.SystemConfig.Language);
                if (logOn.ShowDialog() == DialogResult.OK)
                {
                    this.DisplayRefresh(this.ParentForm.CurrentSystemStatus);
                    this.buttonUser.ButtonDown();

                    switch (this.CurrentDisplay)
                    {
                        case DataStore.MainDisplayStore.DisplayMenu:
                            this.MainDisplayMenu.DisplayRefresh(this.ParentForm.CurrentSystemStatus);
                            break;
                        case DataStore.MainDisplayStore.DisplayNormal:
                            this.MainDisplayNormal.DisplayRefresh(this.ParentForm.CurrentSystemStatus);
                            break;
                        case DataStore.MainDisplayStore.DisplayXYChart:
                            this.MainDisplayXYChart.DisplayRefresh(this.ParentForm.CurrentSystemStatus);
                            break;
                        case DataStore.MainDisplayStore.DisplayList:
                            this.MainDisplayList.DisplayRefresh(this.ParentForm.CurrentSystemStatus);
                            break;
                        case DataStore.MainDisplayStore.DisplayDataStat:
                            this.MainDisplayDataStatistics.DisplayRefresh(this.ParentForm.CurrentSystemStatus);
                            break;
                        default:
                            break;
                    }
                }
                else
                {
                    this.buttonUser.ButtonUp();
                }
            }
            else
            {
                DialogFormYesNo msg = new DialogFormYesNo(this.ParentForm.SystemConfig.Language, 12);
                if (msg.ShowDialog() == DialogResult.Yes)
                {
                    this.ParentForm.CurrentSystemStatus.CurrentUser.Group = DataStore.UserGroup.LogOff;
                    this.buttonUser.ButtonUp();

                    this.DisplayRefresh(this.ParentForm.CurrentSystemStatus);

                    switch (this.CurrentDisplay)
                    {
                        case DataStore.MainDisplayStore.DisplayMenu:
                            this.MainDisplayMenu.DisplayRefresh(this.ParentForm.CurrentSystemStatus);
                            break;
                        case DataStore.MainDisplayStore.DisplayNormal:
                            this.MainDisplayNormal.DisplayRefresh(this.ParentForm.CurrentSystemStatus);
                            break;
                        case DataStore.MainDisplayStore.DisplayXYChart:
                            this.MainDisplayXYChart.DisplayRefresh(this.ParentForm.CurrentSystemStatus);
                            break;
                        case DataStore.MainDisplayStore.DisplayList:
                            this.MainDisplayList.DisplayRefresh(this.ParentForm.CurrentSystemStatus);
                            break;
                        case DataStore.MainDisplayStore.DisplayDataStat:
                            this.MainDisplayDataStatistics.DisplayRefresh(this.ParentForm.CurrentSystemStatus);
                            break;
                        default:
                            break;
                    }
                }
            }
        }
        private void buttonWeightInputMode_Click(object sender, EventArgs e)
        {
            if (this.buttonWeightInputMode.ButtonStatus == SmartX.SmartButton.BUTSTATUS.DOWN)
                this.ParentForm.CurrentSystemStatus.CurrentWeightInputMode = DataStore.WeightInputMode.Deviation;
            else
                this.ParentForm.CurrentSystemStatus.CurrentWeightInputMode = DataStore.WeightInputMode.Weight;

            this.UpdateCurrentProductDisplay(this.ParentForm.CurrentSystemStatus.Equipment, this.ParentForm.CurrentProductItem1, this.ParentForm.CurrentProductItem2,
                this.ParentForm.CurrentWeightData1, this.ParentForm.CurrentWeightData2);
        }
        private void buttonMenu_Click(object sender, EventArgs e)
        {
            if (this.ParentForm.SystemConfig.IsLogin == false)
            {
                if (this.ParentForm.CurrentSystemStatus.Equipment == DataStore.EquipmentStatus.Stop)
                {
                    ((FormMain)(Owner)).smartForm.Show((int)DataStore.FormStore.FormMenu);
                    this.ParentForm.ChildFormMenu.DisplayRefresh(this.ParentForm.CurrentSystemStatus);
                }
            }
            else
            {
                if (this.ParentForm.CurrentSystemStatus.Equipment == DataStore.EquipmentStatus.Stop)
                {
                    ((FormMain)(Owner)).smartForm.Show((int)DataStore.FormStore.FormMenu);
                    this.ParentForm.ChildFormMenu.DisplayRefresh(this.ParentForm.CurrentSystemStatus);
                }
            }
        }

        private void buttonStop_Click(object sender, EventArgs e)
        {
            this.CurrentDisplay = DataStore.MainDisplayStore.DisplayNormal;

            this.DisplayBottomMain(this.CurrentDisplay);
        }
        private void buttonBargraph_Click(object sender, EventArgs e)
        {
            this.CurrentDisplay = DataStore.MainDisplayStore.DisplayNormal;

            this.DisplayBottomMain(this.CurrentDisplay);
        }
        private void buttonLinegraph_Click(object sender, EventArgs e)
        {
            this.CurrentDisplay = DataStore.MainDisplayStore.DisplayXYChart;

            this.DisplayBottomMain(this.CurrentDisplay);
        }
        private void buttonList_Click(object sender, EventArgs e)
        {
            this.CurrentDisplay = DataStore.MainDisplayStore.DisplayList;

            this.DisplayBottomMain(this.CurrentDisplay);
        }
        private void buttonDataStat_Click(object sender, EventArgs e)
        {
            this.CurrentDisplay = DataStore.MainDisplayStore.DisplayDataStat;

            this.DisplayBottomMain(this.CurrentDisplay);
        }
        private void buttonStartMenu_Click(object sender, EventArgs e)
        {
            this.CurrentDisplay = DataStore.MainDisplayStore.DisplayMenu;

            this.DisplayBottomMain(this.CurrentDisplay);
        }
        #endregion

        private void buttonExit_Click(object sender, EventArgs e)
        {
            this.ParentForm.CloseSmartUartLink();
            this.ParentForm.smartFileCommunicationLog.Close();
            this.ParentForm.Close();
        }

        private void buttonConveyorUpDown_Click(object sender, EventArgs e)
        {
            if (this.buttonConveyorUpDown.ButtonStatus == SmartX.SmartButton.BUTSTATUS.DOWN)
                this.ParentForm.TransferData(CommunicationCommand.MotorDown, CommunicationID.MainBoard);
            else
                this.ParentForm.TransferData(CommunicationCommand.MotorUp, CommunicationID.MainBoard);
        }
    }
}