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.Forms;
using ITC81DB_ImageDll;

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

        #region Constructor
        public ControlMainWeightSmallScreen(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 if (this.ParentForm.ParentForm.SystemConfig1.Language == DataStore.LanguageID.Spanish)
            {
            }
            else
            {

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

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

        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;
        }
        public void UpdateSpotOrBypassImageVisibleStatus(bool check)
        {
            if (check == true)
            {
                this.pictureBoxBypass.Visible = true;
            }
            else
            {
                this.pictureBoxBypass.Visible = false;
            }
        }
        public void ChangeToSpotCheckImage()
        {
            Class1 images = new Class1();
            this.pictureBoxBypass.Image = new Bitmap(images.GetImage(Class1.ButtonImages.Spot_check_45X25));
        }
        public void ChangeToBypassImage()
        {
            Class1 images = new Class1();
            this.pictureBoxBypass.Image = new Bitmap(images.GetImage(Class1.ButtonImages.Bypass_45X25));
        }
        public 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;
            }

            // RFID or 바코드
            if (this.ParentForm.ParentForm.SystemConfig2.IsRFIDSpeedwayR420 == true)
            {
                this.pictureBoxRFIDEnable.Visible = true;
                this.pictureBoxRFIDDisable.Visible = false;

                this.pictureBoxBarcodeEnable.Visible = false;
                this.pictureBoxBarcodeDisable.Visible = true;

                this.pictureBoxBarcodeQueueCount.Visible = false;
                this.pictureBoxRFIDQueueCount.Visible = true;
                this.labelBarcodeCount.Visible = true;
            }
            else if (this.ParentForm.ParentForm.SystemConfig2.Barcode != 0)
            {
                this.pictureBoxBarcodeEnable.Visible = true;
                this.pictureBoxBarcodeDisable.Visible = false;

                this.pictureBoxRFIDEnable.Visible = false;
                this.pictureBoxRFIDDisable.Visible = true;

                this.pictureBoxBarcodeQueueCount.Visible = true;
                this.pictureBoxRFIDQueueCount.Visible = false;
                this.labelBarcodeCount.Visible = true;
            }
            else
            {
                this.pictureBoxRFIDEnable.Visible = false;
                this.pictureBoxRFIDDisable.Visible = true;

                this.pictureBoxBarcodeEnable.Visible = false;
                this.pictureBoxBarcodeDisable.Visible = true;

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

            #region 외부 입력
            if (this.ParentForm.ParentForm.CheckSystemParameter1((int)DataStore.ExternalInputMode.IN4_DischargeSorterA) == true
               || this.ParentForm.ParentForm.CheckSystemParameter1((int)DataStore.ExternalInputMode.IN16_DischargeSorterB) == true)
            {
                this.pictureBoxDischargeDisable.Visible = false;
                this.pictureBoxDischargeEnable.Visible = true;
            }
            else
            {
                this.pictureBoxDischargeDisable.Visible = true;
                this.pictureBoxDischargeEnable.Visible = false;
            }

            if (this.ParentForm.ParentForm.CheckSystemParameter1((int)DataStore.ExternalInputMode.IN5_Air) == true)
            {
                this.pictureBoxAirCheckDisable.Visible = false;
                this.pictureBoxAirCheckEnable.Visible = true;
            }
            else
            {
                this.pictureBoxAirCheckDisable.Visible = true;
                this.pictureBoxAirCheckEnable.Visible = false;
            }

            if (this.ParentForm.ParentForm.CheckSystemParameter1((int)DataStore.ExternalInputMode.IN6_Door) == true)
            {
                this.pictureBoxDoorCheckDisable.Visible = false;
                this.pictureBoxDoorCheckEnable.Visible = true;
            }
            else
            {
                this.pictureBoxDoorCheckDisable.Visible = true;
                this.pictureBoxDoorCheckEnable.Visible = false;
            }

            if (this.ParentForm.ParentForm.CheckSystemParameter1((int)DataStore.ExternalInputMode.IN7_Stopper) == true)
            {
                this.pictureBoxEntryStopperDisable.Visible = false;
                this.pictureBoxEntryStopperEnable.Visible = true;
            }
            else
            {
                this.pictureBoxEntryStopperDisable.Visible = true;
                this.pictureBoxEntryStopperEnable.Visible = false;
            }

            if (this.ParentForm.ParentForm.CheckSystemParameter1((int)DataStore.ExternalInputMode.IN8_Windproof) == true)
            {
                this.pictureBoxWindProofDisable.Visible = false;
                this.pictureBoxWindProofEnable.Visible = true;
            }
            else
            {
                this.pictureBoxWindProofDisable.Visible = true;
                this.pictureBoxWindProofEnable.Visible = false;
            }

            if (this.ParentForm.ParentForm.CheckSystemParameter1((int)DataStore.ExternalInputMode.IN9_ExternalOperation) == true)
            {
                this.pictureBoxIconExternalOperationDisable.Visible = false;
                this.pictureBoxIconExternalOperationEnable.Visible = true;
            }
            else
            {
                this.pictureBoxIconExternalOperationDisable.Visible = true;
                this.pictureBoxIconExternalOperationEnable.Visible = false;
            }
            #endregion

            // 이더넷
            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;

            // 기준치 갱신
            if (this.ParentForm.ParentForm.SystemConfig1.IsAverageTracking == true)
            {
                this.pictureBoxAverageTrackingDisable.Visible = false;
                this.pictureBoxAverageTrackingEnable.Visible = true;
            }
            else
            {
                this.pictureBoxAverageTrackingDisable.Visible = true;
                this.pictureBoxAverageTrackingEnable.Visible = false;
            }
        }
        public void UpdateDisplayAlarmView(WeightData data)
        {
            // 압력 센서 에러
            if (data.IsPressureSensingError == true)
                this.pictureBoxIconAir.Visible = true;
            else
                this.pictureBoxIconAir.Visible = false;
        }
        public void UpdateDisplayAlarmView(AlarmList alarm)
        {
            // 압력 센서 에러
            if (alarm.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;
                this.pictureBoxIconLengthError.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;

            // LengthError 아이콘 표시
            if (data.JudgmentStatus == DataStore.JudgmentStatus.LengthError)
            {
                if (this.pictureBoxIconLengthError.Visible != true)
                    this.pictureBoxIconLengthError.Visible = true;
            }
            else
                this.pictureBoxIconLengthError.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 UpdateRFIDDisplay(RFID_Impinj_Speedway_R420 rfid)
        {
            int value = 0;

            value = rfid.Count();
            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)
        {
            if (this.ParentForm.ParentForm.SystemConfig2.Barcode != 0)
            {
                this.ParentForm.ParentForm.CurrentBarcode.Clear();
                this.UpdateBarcodeDisplay(this.ParentForm.ParentForm.CurrentBarcode);
            }
            else if (this.ParentForm.ParentForm.SystemConfig2.IsRFIDSpeedwayR420 == true)
            {
                this.ParentForm.ParentForm.CurrentRFID.Clear();
                this.UpdateRFIDDisplay(this.ParentForm.ParentForm.CurrentRFID);
            }
        }
        #endregion
    }
}
