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

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

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

            this.ParentForm = parent;

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

        #region Property
        public FormMainDisplay 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)
            {
                this.labelTitleTime1.Text = this.labelTitleTime2.Text = "Time";
                this.labelTitleWeight1.Text = this.labelTitleWeight2.Text = "Weight";
                this.labelTitleGrade1.Text = this.labelTitleGrade2.Text = "Grade";

                this.buttonOver1.DownImage = this.buttonOver2.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.engLightOverDown));
                this.buttonOver1.UpImage = this.buttonOver2.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.engLightOverUp));
                this.buttonPass1.DownImage = this.buttonPass2.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.engLightPassDown));
                this.buttonPass1.UpImage = this.buttonPass2.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.engLightPassUp));
                this.buttonUnder1.DownImage = this.buttonUnder2.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.engLightUnderDown));
                this.buttonUnder1.UpImage = this.buttonUnder2.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.engLightUnderUp));
            }
            else if (this.ParentForm.ParentForm.SystemConfig.Language == DataStore.LanguageID.Chinese)
            {
                this.labelTitleTime1.Text = this.labelTitleTime2.Text = "时间";
                this.labelTitleWeight1.Text = this.labelTitleWeight2.Text = "重量";
                this.labelTitleGrade1.Text = this.labelTitleGrade2.Text = "等级";

                this.buttonOver1.DownImage = this.buttonOver2.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.chnLightOverDown));
                this.buttonOver1.UpImage = this.buttonOver2.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.chnLightOverUp));
                this.buttonPass1.DownImage = this.buttonPass2.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.chnLightPassDown));
                this.buttonPass1.UpImage = this.buttonPass2.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.chnLightPassUp));
                this.buttonUnder1.DownImage = this.buttonUnder2.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.chnLightUnderDown));
                this.buttonUnder1.UpImage = this.buttonUnder2.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.chnLightUnderUp));
            }
            else if (this.ParentForm.ParentForm.SystemConfig.Language == DataStore.LanguageID.Czech)
            {
            }
            else if (this.ParentForm.ParentForm.SystemConfig.Language == DataStore.LanguageID.Russian)
            {
            }
            else
            {
                this.labelTitleTime1.Text = this.labelTitleTime2.Text = "시간";
                this.labelTitleWeight1.Text = this.labelTitleWeight2.Text = "중량";
                this.labelTitleGrade1.Text = this.labelTitleGrade2.Text = "등급";

                this.buttonOver1.DownImage = this.buttonOver2.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.korLightOverDown));
                this.buttonOver1.UpImage = this.buttonOver2.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.korLightOverUp));
                this.buttonPass1.DownImage = this.buttonPass2.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.korLightPassDown));
                this.buttonPass1.UpImage = this.buttonPass2.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.korLightPassUp));
                this.buttonUnder1.DownImage = this.buttonUnder2.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.korLightUnderDown));
                this.buttonUnder1.UpImage = this.buttonUnder2.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.korLightUnderUp));
            }
        }
        private void DefaultSetting()
        {

        }

        private void UpdateDisplayJudgmentIcon1(DataStore.JudgmentStatus status)
        {
            if (status == DataStore.JudgmentStatus.Under)
            {
                this.buttonUnder1.ButtonDown();
                this.buttonPass1.ButtonUp();
                this.buttonOver1.ButtonUp();
            }
            else if (status == DataStore.JudgmentStatus.Pass)
            {
                this.buttonUnder1.ButtonUp();
                this.buttonPass1.ButtonDown();
                this.buttonOver1.ButtonUp();
            }
            else if (status == DataStore.JudgmentStatus.Over)
            {
                this.buttonUnder1.ButtonUp();
                this.buttonPass1.ButtonUp();
                this.buttonOver1.ButtonDown();
            }
            else
            {
                this.buttonUnder1.ButtonUp();
                this.buttonPass1.ButtonUp();
                this.buttonOver1.ButtonUp();
            }
        }
        private void UpdateDisplayJudgmentIcon2(DataStore.JudgmentStatus status)
        {
            if (status == DataStore.JudgmentStatus.Under)
            {
                this.buttonUnder2.ButtonDown();
                this.buttonPass2.ButtonUp();
                this.buttonOver2.ButtonUp();
            }
            else if (status == DataStore.JudgmentStatus.Pass)
            {
                this.buttonUnder2.ButtonUp();
                this.buttonPass2.ButtonDown();
                this.buttonOver2.ButtonUp();
            }
            else if (status == DataStore.JudgmentStatus.Over)
            {
                this.buttonUnder2.ButtonUp();
                this.buttonPass2.ButtonUp();
                this.buttonOver2.ButtonDown();
            }
            else
            {
                this.buttonUnder2.ButtonUp();
                this.buttonPass2.ButtonUp();
                this.buttonOver2.ButtonUp();
            }
        }
        private void UpdateDisplayIconNET1(ProductItem item)
        {
            double dValue = 0.0;

            dValue = Helper.StringToWeight(item.TareRange, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);
            if (dValue != 0.0)
                this.labelIconNET1.Visible = true;
            else
                this.labelIconNET1.Visible = false;
        }
        private void UpdateDisplayIconNET2(ProductItem item)
        {
            double dValue = 0.0;

            dValue = Helper.StringToWeight(item.TareRange, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);
            if (dValue != 0.0)
                this.labelIconNET2.Visible = true;
            else
                this.labelIconNET2.Visible = false;
        }
        private void UpdateDisplayStopWeight1(DataStore.EquipmentStatus status, WeightData data)
        {
            string value = "";

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

            if (status == DataStore.EquipmentStatus.Stop)
            {
                // 중량
                value = Helper.DoubleToString(data.Weight, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);
                if (this.labelWeight1.Text != value)
                    this.labelWeight1.Text = value;
            }
        }
        private void UpdateDisplayStopWeight2(DataStore.EquipmentStatus status, WeightData data)
        {
            string value = "";

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

            if (status == DataStore.EquipmentStatus.Stop)
            {
                // 중량
                value = Helper.DoubleToString(data.Weight, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);
                if (this.labelWeight2.Text != value)
                    this.labelWeight2.Text = value;
            }
        }
        private void UpdateDisplayListBox1(DataStore.EquipmentStatus status, WeightData data)
        {
            string value = "", time = "", weight = "";

            time = string.Format(@"{0:MM-dd HH:mm:ss}", DateTime.Now);
            weight = Helper.DoubleToString(data.Weight, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);
            value = string.Format("{0,20}{1,25}{2,28}", time, weight, data.JudgmentStatus);

            if (this.listBox1.Items.Count >= 100)
                this.listBox1.RemoveItem(0);

            this.listBox1.AddItem(value);
            this.listBox1.ScrollDown();
        }
        private void UpdateDisplayListBox2(DataStore.EquipmentStatus status, WeightData data)
        {
            string value = "", time = "", weight = "";

            time = string.Format(@"{0:MM-dd HH:mm:ss}", DateTime.Now);
            weight = Helper.DoubleToString(data.Weight, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);
            value = string.Format("{0,20}{1,25}{2,28}", time, weight, data.JudgmentStatus);

            if (this.listBox2.Items.Count >= 100)
                this.listBox2.RemoveItem(0);

            this.listBox2.AddItem(value);
            this.listBox2.ScrollDown();
        }

        public void UpdateEquipmentStatusDisplay(DataStore.EquipmentStatus status)
        {
            if (status == DataStore.EquipmentStatus.Start)
            {
                this.labelWeight1.Text = Helper.StringToDecimalPlaces("0", this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);
                this.labelWeight2.Text = Helper.StringToDecimalPlaces("0", this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);
            }
            else
            {
                this.pictureBoxIconGap1.Visible = false;
                this.pictureBoxIconGap2.Visible = false;
                this.pictureBoxIconMetal1.Visible = false;
                this.pictureBoxIconMetal2.Visible = false;

                this.buttonUnder1.ButtonUp();
                this.buttonUnder2.ButtonUp();
                this.buttonPass1.ButtonUp();
                this.buttonPass2.ButtonUp();
                this.buttonOver1.ButtonUp();
                this.buttonOver2.ButtonUp();
            }
        }
        public void UpdateCurrentProductDisplay(DataStore.EquipmentStatus status, ProductItem pItem1, ProductItem pItem2, WeightData wData1, WeightData wData2)
        {
            // NET Icon 표시
            this.UpdateDisplayIconNET1(pItem1);
            this.UpdateDisplayIconNET2(pItem2);
        }
        public void UpdateStopWeightDisplay(DataStore.EquipmentStatus status, WeightData data1, WeightData data2)
        {
            this.UpdateDisplayStopWeight1(status, data1);
            this.UpdateDisplayStopWeight2(status, data2);
        }
        public void UpdateStartWeightDisplay1(DataStore.EquipmentStatus status, WeightData data)
        {
            string value = "";

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

            // 중량
            value = Helper.DoubleToString(data.Weight, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);
            if (this.labelWeight1.Text != value)
                this.labelWeight1.Text = value;

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

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

            this.UpdateDisplayJudgmentIcon1(data.JudgmentStatus);
            this.UpdateDisplayListBox1(status, data);
        }
        public void UpdateStartWeightDisplay2(DataStore.EquipmentStatus status, WeightData data)
        {
            string value = "";

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

            // 중량
            value = Helper.DoubleToString(data.Weight, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);
            if (this.labelWeight2.Text != value)
                this.labelWeight2.Text = value;

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

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

            this.UpdateDisplayJudgmentIcon2(data.JudgmentStatus);
            this.UpdateDisplayListBox2(status, data);
        }

        public void Clear()
        {
            this.listBox1.ClearAll();
            this.listBox2.ClearAll();
        }
        public void DisplayRefresh(SystemStatus status)
        {

        }
        #endregion

        #region Event Handler
        private void buttonListUp1_Click(object sender, EventArgs e)
        {
            if (this.listBox1.ViewStartItemIndex / 7 > 0)
                this.listBox1.ScrollUp(130);
            else
                this.listBox1.ScrollUp();

            this.listBox1.SelectItemIndex = -1;
        }
        private void buttonListDown1_Click(object sender, EventArgs e)
        {
            if (this.listBox1.ViewRemainCount / 7 > 0)
                this.listBox1.ScrollDown(130);
            else
                this.listBox1.ScrollDown();

            this.listBox1.SelectItemIndex = -1;
        }
        private void buttonListRapidDown1_Click(object sender, EventArgs e)
        {
            this.listBox1.ScrollDown(this.listBox1.ViewRemainCount / 7 * 160);

            for (int i = 0; i < this.listBox1.ViewRemainCount % 7; i++)
                this.listBox1.ScrollDown();

            this.listBox1.SelectItemIndex = -1;
        }

        private void buttonListUp2_Click(object sender, EventArgs e)
        {
            if (this.listBox2.ViewStartItemIndex / 7 > 0)
                this.listBox2.ScrollUp(150);
            else
                this.listBox2.ScrollUp();

            this.listBox2.SelectItemIndex = -1;
        }
        private void buttonListDown2_Click(object sender, EventArgs e)
        {
            if (this.listBox2.ViewRemainCount / 7 > 0)
                this.listBox2.ScrollDown(150);
            else
                this.listBox2.ScrollDown();

            this.listBox2.SelectItemIndex = -1;
        }
        private void buttonListRapidDown2_Click(object sender, EventArgs e)
        {
            this.listBox2.ScrollDown(this.listBox2.ViewRemainCount / 7 * 170);

            for (int i = 0; i < this.listBox2.ViewRemainCount % 7; i++)
                this.listBox2.ScrollDown();

            this.listBox2.SelectItemIndex = -1;
        }
        #endregion
    }
}
