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

using SmartX;
using ITC81DB_0H.Forms;
using ITC81DB_2H_ImageDll;

namespace ITC81DB_0H.Controls
{
    public partial class ControlMainDisplayWeightSmall : UserControl
    {
        #region Field
        private FormMainDisplay m_ParentForm;
        #endregion

        #region Constructor
        public ControlMainDisplayWeightSmall(FormMainDisplay parent)
        {
            InitializeComponent();

            this.ParentForm = parent;

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

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

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

            if (this.ParentForm.ParentForm.SystemConfig1.Language == DataStore.LanguageID.English)
            {

            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == DataStore.LanguageID.Chinese)
            {
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == DataStore.LanguageID.Czech)
            {
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == DataStore.LanguageID.Russian)
            {
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == DataStore.LanguageID.German)
            {
            }
            else
            {

            }
        }
        private void DefaultSetting()
        {
            this.labelWeight.Text = "0.0";

            // 아이콘, 단위 등 화면 갱신
            this.UpdateDisplay();
        }

        public void SetIconNET(ProductItem item)
        {
            double dValue = 0.0;

            dValue = Helper.StringToWeight(item.TareRange, this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces);
            if (dValue != 0.0)
                this.labelIconNET.Visible = true;
            else
                this.labelIconNET.Visible = false;
        }
        private void UpdateDisplay()
        {
            int value = 0;

            // Icon - 데이터 백업
            if (this.ParentForm.ParentForm.SystemConfig1.IsDataBackup == true)
            {
                this.pictureBoxIconUsbEnable.Visible = true;
                this.pictureBoxIconUsbDesable.Visible = false;
            }
            else
            {
                this.pictureBoxIconUsbEnable.Visible = false;
                this.pictureBoxIconUsbDesable.Visible = true;
            }

            // 단위
            if (this.labelUnit.Text != this.ParentForm.ParentForm.SystemConfig1.Unit)
                this.labelUnit.Text = this.ParentForm.ParentForm.SystemConfig1.Unit;

            // Icon - Ch1 
            if (this.ParentForm.ParentForm.SystemConfig1.SerialCOM1Mode == 0)
            {
                this.pictureBoxCh1Enable.Visible = false;
                this.pictureBoxCh1Disable.Visible = true;
            }
            else
            {
                this.pictureBoxCh1Enable.Visible = true;
                this.pictureBoxCh1Disable.Visible = false;
            }

            // Icon - Ch2
            if (this.ParentForm.ParentForm.SystemConfig1.SerialCOM3Mode == 0)
            {
                this.pictureBoxCh2Enable.Visible = false;
                this.pictureBoxCh2Disable.Visible = true;
            }
            else
            {
                this.pictureBoxCh2Enable.Visible = true;
                this.pictureBoxCh2Disable.Visible = false;
            }

            // Icon - Ch3
            if (this.ParentForm.ParentForm.SystemConfig1.SerialCOM4Mode == 0)
            {
                this.pictureBoxCh3Enable.Visible = false;
                this.pictureBoxCh3Disable.Visible = true;
            }
            else
            {
                this.pictureBoxCh3Enable.Visible = true;
                this.pictureBoxCh3Disable.Visible = false;
            }

            // 옵션보드
            try
            {
                value = int.Parse(this.ParentForm.ParentForm.CurrentSystemParameter1.OptionBoard);
            }
            catch
            {
                value = 0;
            }
            if (value == 2 && int.Parse(this.ParentForm.ParentForm.CurrentSystemParameter2.OPT2Port) != 0) // Dispenser
            {
                this.pictureBoxDispenserDisable.Visible = false;
                this.pictureBoxDispenserEnable.Visible = true;

                this.pictureBoxFeedbackDisable.Visible = true;
                this.pictureBoxFeedbackEnable.Visible = false;
            }
            else if (value == 1 && int.Parse(this.ParentForm.ParentForm.CurrentSystemParameter2.OPT1SamplingCount) != 0) // Feedback
            {
                this.pictureBoxDispenserDisable.Visible = true;
                this.pictureBoxDispenserEnable.Visible = false;

                this.pictureBoxFeedbackDisable.Visible = false;
                this.pictureBoxFeedbackEnable.Visible = true;
            }
            else
            {
                this.pictureBoxFeedbackDisable.Visible = true;
                this.pictureBoxFeedbackEnable.Visible = false;

                this.pictureBoxDispenserDisable.Visible = true;
                this.pictureBoxDispenserEnable.Visible = false;
            }

            //// 정지계량
            //if (this.ParentForm.ParentForm.CurrentSystemParameter1.StopWeighing == "0")
            //{
            //    this.pictureBoxStopWeighingDisable.Visible = true;
            //    this.pictureBoxStopWeighingEnable.Visible = false;
            //}
            //else
            //{
            //    this.pictureBoxStopWeighingDisable.Visible = false;
            //    this.pictureBoxStopWeighingEnable.Visible = true;
            //}

            // 0:미사용, 1:NG, 2:START, 3:STOP

            // 바코드
            if (this.ParentForm.ParentForm.SystemConfig1.IsBarcodeEnable == true)
            {
                this.pictureBoxBarcodeDisable.Visible = false;
                this.pictureBoxBarcodeEnable.Visible = true;

                this.pictureBoxBarcodeQueueCount.Visible = true;
                this.labelBarcodeCount.Visible = true;

                //this.pictureBoxCh2Disable.Visible = false;
                //this.pictureBoxCh2Enable.Visible = true;
            }
            else
            {
                this.pictureBoxBarcodeDisable.Visible = true;
                this.pictureBoxBarcodeEnable.Visible = false;

                this.pictureBoxBarcodeQueueCount.Visible = false;
                this.labelBarcodeCount.Visible = false;

                //this.pictureBoxCh2Disable.Visible = true;
                //this.pictureBoxCh2Enable.Visible = false;
            }

            // 이더넷
            if (this.ParentForm.ParentForm.SystemConfig2.IsEthernetEnable == true)
            {
                this.pictureBoxEthernetDisable.Visible = false;
                this.pictureBoxEthernetDisconnection.Visible = true;
                this.pictureBoxEthernetConnection.Visible = false;
            }
            else
            {
                this.pictureBoxEthernetDisable.Visible = true;
                this.pictureBoxEthernetDisconnection.Visible = false;
                this.pictureBoxEthernetConnection.Visible = false;
            }

            // Alarm
            if ((this.ParentForm.ParentForm.SystemConfig2.CountingOutput1Mode != 0 && this.ParentForm.ParentForm.SystemConfig2.CountingOutput1Number != 0)
                || (this.ParentForm.ParentForm.SystemConfig2.CountingOutput2Mode != 0 && this.ParentForm.ParentForm.SystemConfig2.CountingOutput2Number != 0))
            {
                this.pictureBoxCountingOutputDisable.Visible = false;
                this.pictureBoxCountingOutputEnable.Visible = true;
                this.pictureBoxCountingOutputCount.Visible = true;
            }
            else
            {
                this.pictureBoxCountingOutputDisable.Visible = true;
                this.pictureBoxCountingOutputEnable.Visible = false;
                this.pictureBoxCountingOutputCount.Visible = false;
            }

            //// 랜덤모드
            //if (this.ParentForm.ParentForm.SystemConfig2.IsUsingRandomMode == true)
            //{
            //    this.pictureBoxRandomModeDisable.Visible = false;
            //    this.pictureBoxRandomModeEnable.Visible = true;
            //}
            //else
            //{
            //    this.pictureBoxRandomModeDisable.Visible = true;
            //    this.pictureBoxRandomModeEnable.Visible = false;
            //}

            if (this.ParentForm.ParentForm.SystemConfig2.CountingOutput1Mode != 0 && this.ParentForm.ParentForm.SystemConfig2.CountingOutput1Number != 0)
                this.labelCountingOutput1.Visible = true;
            else
                this.labelCountingOutput1.Visible = false;

            if (this.ParentForm.ParentForm.SystemConfig2.CountingOutput2Mode != 0 && this.ParentForm.ParentForm.SystemConfig2.CountingOutput2Number != 0)
                this.labelCountingOutput2.Visible = true;
            else
                this.labelCountingOutput2.Visible = false;
        }
        public void UpdateDisplayAlarmView(WeightData data)
        {
            // 압력 센서 에러
            if (data.IsPressureSensingError == true)
                this.pictureBoxIconAir.Visible = true;
            else
                this.pictureBoxIconAir.Visible = false;
        }
        public void UpdateEquipmentStatusDisplay(DataStore.EquipmentStatus status)
        {
            if (status == DataStore.EquipmentStatus.Start)
            {
                this.labelWeight.Text = Helper.StringToDecimalPlaces("0", this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces);
            }
            else
            {
                this.pictureBoxIconGap.Visible = false;
                this.pictureBoxIconMetal.Visible = false;
                this.pictureBoxIconExNG1.Visible = false;
                this.pictureBoxIconExNG2.Visible = false;
            }
        }
        public void UpdateCurrentProductDisplay(DataStore.EquipmentStatus status, ProductItem pItem, WeightData wData)
        {
            this.SetIconNET(pItem);
        }
        public void UpdateStopWeightDisplay(DataStore.EquipmentStatus status, WeightData data)
        {
            string value = "";

            if (status == DataStore.EquipmentStatus.Stop)
            {
                // 중량
                if (data.Weight == this.ParentForm.ParentForm.OverloadWeight)
                    value = "O.L  ";
                else
                    value = Helper.DoubleToString(data.Weight, this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces);
                if (this.labelWeight.Text != value)
                    this.labelWeight.Text = value;
            }

            // 영점 아이콘표시
            if (data.Status == DataStore.WeightStatus.WeightZero)
                this.pictureBoxIconZero.Visible = true;
            else
                this.pictureBoxIconZero.Visible = false;
        }
        public void UpdateStartWeightDisplay(DataStore.EquipmentStatus status, WeightData data)
        {
            string value = "";

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

            // 영점 표시
            if (data.Status == DataStore.WeightStatus.WeightZero)
                this.pictureBoxIconZero.Visible = true;
            else
                this.pictureBoxIconZero.Visible = false;

            // 중량
            if (data.Weight == this.ParentForm.ParentForm.OverloadWeight)
                value = "O.L  ";
            else
                value = Helper.DoubleToString(data.Weight, this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces);
            if (this.labelWeight.Text != value)
                this.labelWeight.Text = value;

            // GAP 아이콘 표시
            if (data.JudgmentStatus == DataStore.JudgmentStatus.Double)
            {
                if (this.pictureBoxIconGap.Visible != true)
                    this.pictureBoxIconGap.Visible = true;
            }
            else
                this.pictureBoxIconGap.Visible = false;

            // Metal 아이콘 표시
            if (data.JudgmentStatus == DataStore.JudgmentStatus.Metal)
            {
                if (this.pictureBoxIconMetal.Visible != true)
                    this.pictureBoxIconMetal.Visible = true;
            }
            else
                this.pictureBoxIconMetal.Visible = false;

            // ExNG1 아이콘 표시
            if (data.JudgmentStatus == DataStore.JudgmentStatus.ExNg1)
            {
                if (this.pictureBoxIconExNG1.Visible != true)
                    this.pictureBoxIconExNG1.Visible = true;
            }
            else
                this.pictureBoxIconExNG1.Visible = false;

            // ExNG2 아이콘 표시
            if (data.JudgmentStatus == DataStore.JudgmentStatus.ExNg2)
            {
                if (this.pictureBoxIconExNG2.Visible != true)
                    this.pictureBoxIconExNG2.Visible = true;
            }
            else
                this.pictureBoxIconExNG2.Visible = false;
        }
        public void UpdateBarcodeDisplay(Barcode barcode)
        {
            int value = 0;

            value = barcode.GetQueueCount;
            if (this.labelBarcodeCount.Text != value.ToString())
                this.labelBarcodeCount.Text = value.ToString();
        }
        public void UpdateEthernetStatusDisplay(bool status)
        {
            if (status == true)
            {
                this.pictureBoxEthernetConnection.Visible = true;
                this.pictureBoxEthernetDisconnection.Visible = false;
            }
            else
            {
                this.pictureBoxEthernetConnection.Visible = false;
                this.pictureBoxEthernetDisconnection.Visible = true;
            }
        }
        public void UpdateAlarmLabelDisplay(int alarm1Count, int alarm2Count)
        {
            this.labelCountingOutput1.Text = alarm1Count.ToString();
            this.labelCountingOutput2.Text = alarm2Count.ToString();
        }

        public void DisplayRefresh(SystemStatus status)
        {
            // 아이콘, 단위 등 화면 갱신
            this.UpdateDisplay();
        }
        #endregion

        #region Event Handler
        private void labelAlarm1_Click(object sender, EventArgs e)
        {
            this.ParentForm.ParentForm.ClearAlarm(1);
        }

        private void labelAlarm2_Click(object sender, EventArgs e)
        {
            this.ParentForm.ParentForm.ClearAlarm(2);
        }

        private void labelBarcodeCount_Click(object sender, EventArgs e)
        {
            this.ParentForm.ParentForm.CurrentBarcode.Clear();
            this.UpdateBarcodeDisplay(this.ParentForm.ParentForm.CurrentBarcode);
        }
        #endregion
    }
}