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

using SmartX;
using INT63DC_6CH.Forms;

namespace INT63DC_6CH.Controls
{
    public partial class ControlMainDisplayDotGraph8 : UserControl
    {
        #region Field
        private FormMainDisplay m_ParentForm;

        private string CurrentUnderWeight;
        private string CurrentOverWeight;
        private int CurrentDecimalPlaces;

        private Collection<PictureBox> CollectionPictureBoxZero;
        private Collection<SmartDraw> CollectionDraw;
        private Collection<PictureBox> CollectionPictureBoxBypass;
        #endregion

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

            this.ParentForm = parent;

            this.CreateCollection();
            this.InitializeControl();
        }
        #endregion

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

        #region Method
        private void CreateCollection()
        {
            // PictureBoxZero
            this.CollectionPictureBoxZero = new Collection<PictureBox>();
            this.CollectionPictureBoxZero.Clear();
            this.CollectionPictureBoxZero.Add(this.pictureBox1);
            this.CollectionPictureBoxZero.Add(this.pictureBox2);
            this.CollectionPictureBoxZero.Add(this.pictureBox3);
            this.CollectionPictureBoxZero.Add(this.pictureBox4);
            this.CollectionPictureBoxZero.Add(this.pictureBox5);
            this.CollectionPictureBoxZero.Add(this.pictureBox6);
            this.CollectionPictureBoxZero.Add(this.pictureBox7);
            this.CollectionPictureBoxZero.Add(this.pictureBox8);

            // SmartDraw
            this.CollectionDraw = new Collection<SmartDraw>();
            this.CollectionDraw.Clear();
            this.CollectionDraw.Add(this.draw1);
            this.CollectionDraw.Add(this.draw2);
            this.CollectionDraw.Add(this.draw3);
            this.CollectionDraw.Add(this.draw4);
            this.CollectionDraw.Add(this.draw5);
            this.CollectionDraw.Add(this.draw6);
            this.CollectionDraw.Add(this.draw7);
            this.CollectionDraw.Add(this.draw8);

            // PictureBoxBypass
            this.CollectionPictureBoxBypass = new Collection<PictureBox>();
            this.CollectionPictureBoxBypass.Clear();
            this.CollectionPictureBoxBypass.Add(this.pictureBoxBypass1);
            this.CollectionPictureBoxBypass.Add(this.pictureBoxBypass2);
            this.CollectionPictureBoxBypass.Add(this.pictureBoxBypass3);
            this.CollectionPictureBoxBypass.Add(this.pictureBoxBypass4);
            this.CollectionPictureBoxBypass.Add(this.pictureBoxBypass5);
            this.CollectionPictureBoxBypass.Add(this.pictureBoxBypass6);
            this.CollectionPictureBoxBypass.Add(this.pictureBoxBypass7);
            this.CollectionPictureBoxBypass.Add(this.pictureBoxBypass8);
        }
        private void InitializeControl()
        {
            foreach (PictureBox picture in this.CollectionPictureBoxZero)
                picture.Visible = false;

            foreach (PictureBox picture in this.CollectionPictureBoxBypass)
                picture.Visible = false;

            this.InitialzeDraws();
            this.draw1.BringToFront();
            this.button1.ButtonDown();
        }
        private void InitialzeDraws()
        {
            this.CurrentOverWeight = this.ParentForm.ParentForm.CurrentProductItem.OverRange;
            this.CurrentUnderWeight = this.ParentForm.ParentForm.CurrentProductItem.UnderRange;
            this.CurrentDecimalPlaces = this.ParentForm.ParentForm.SystemConfig.DecimalPlaces;

            foreach (SmartDraw draw in this.CollectionDraw)
                this.InitializeDraw(draw, this.ParentForm.ParentForm.CurrentProductItem, this.ParentForm.ParentForm.SystemConfig);
        }
        private void InitializeDraw(SmartDraw draw, ProductItem pItem, SystemConfigurationItem sItem)
        {
            draw.BackDraw.SetPenStyle(Color.Blue, 3);

            draw.BackDraw.Line(100, 20, 100, 380);
            draw.BackDraw.Line(100, 380, 968, 380);

            draw.BackDraw.SetPenStyle(Color.Gray, 1);

            draw.BackDraw.SetFontCfg(25, Color.Red);
            draw.BackDraw.TextOut(880, 2, draw.Text);
            draw.BackDraw.SetFontCfg(30, Color.White);

            // Chart의 가로 그리드 그리기
            draw.BackDraw.Line(103, 380 - (1 * 127) + 7, 968, 380 - (1 * 127) + 7);
            draw.BackDraw.TextOut(15, 365 - (1 * 127) + 7, Helper.StringToDecimalPlaces(pItem.UnderRange, sItem.DecimalPlaces));

            draw.BackDraw.Line(103, 380 - (2 * 127) + 14, 968, 380 - (2 * 127) + 14);
            draw.BackDraw.TextOut(15, 365 - (2 * 127) + 14, Helper.StringToDecimalPlaces(pItem.OverRange, sItem.DecimalPlaces));

            SmartX.SmartDraw.CHARTPENSTYLE[] chartchPenStyle = new SmartX.SmartDraw.CHARTPENSTYLE[1];
            chartchPenStyle[0].m_chColor = Color.Yellow;
            chartchPenStyle[0].m_iPenWidth = 2;

            draw.SetChartCfg(103, 380, 865, 360, 16, SmartX.SmartDraw.CHARTREFRESH.LEFTSCROLL, 1);
            draw.ChartChannelPenStyle = chartchPenStyle;
        }

        public void RescaleControl()
        {
            foreach (SmartDraw draw in this.CollectionDraw)
            {
                draw.BackDraw.BackErase(Color.Black);
                draw.Erase();
                draw.PutDataAllClear();
            }
            this.InitialzeDraws();
        }
        public void Clear()
        {
            foreach (SmartX.SmartDraw draw in this.CollectionDraw)
                draw.PutDataAllClear();
        }

        private void UpdateDraw(SmartDraw draw, WeightData weightData)
        {
            int iValue = 0, gap = 0, startingPoint = 0;
            double scale = 0.0, value = 0.0;

            if (weightData.JudgmentStatus == DataStore.JudgmentStatus.None || weightData.JudgmentStatus == DataStore.JudgmentStatus.Empty)
                return;

            // 스케일 계산
            gap = int.Parse(this.ParentForm.ParentForm.CurrentProductItem.OverRange)
                - int.Parse(this.ParentForm.ParentForm.CurrentProductItem.UnderRange);
            scale = 120.0 / gap;
            startingPoint = int.Parse(this.ParentForm.ParentForm.CurrentProductItem.UnderRange) - gap;

            try
            {
                value = (weightData.WeightInt - startingPoint) * scale;
            }
            catch (Exception)
            {
                value = 0;
            }

            // 그래프를 스케일을 넘어가면 Max값 적용
            if (value > 355)
                value = 355;
            if (value < 2)
                value = 2;

            iValue = int.Parse(string.Format("{0:f0}", value));

            draw.PutData(iValue);
        }

        public void UpdateEquipmentStatusDisplay(DataStore.EquipmentStatus status)
        {

        }
        public void UpdateCurrentProductDisplay(DataStore.EquipmentStatus status, Collection<WeightData> weightDatas)
        {

        }
        public void UpdateBypassDisplay(Collection<WeightData> weightData)
        {
            for (int i = 0; i < this.CollectionPictureBoxBypass.Count; i++)
            {
                if (weightData[i].IsBypassMode == true)
                    this.CollectionPictureBoxBypass[i].Visible = true;
                else
                    this.CollectionPictureBoxBypass[i].Visible = false;
            }
        }
        public void UpdateStopWeightDisplay(DataStore.EquipmentStatus status, Collection<WeightData> weightDatas)
        {
            string value = "";

            if (this.ParentForm.ParentForm.SystemConfig.EquipmentColumns > weightDatas.Count)
                return;

            if (status == DataStore.EquipmentStatus.Stop)
            {
                for (int i = 0; i < this.ParentForm.ParentForm.SystemConfig.EquipmentColumns; i++)
                {
                    // 영점표시
                    if (weightDatas[i].Status == DataStore.WeightStatus.WeightZero)
                        this.CollectionPictureBoxZero[i].Visible = true;
                    else
                        this.CollectionPictureBoxZero[i].Visible = false;
                }
            }
            else
            {
                for (int i = 0; i < this.ParentForm.ParentForm.SystemConfig.EquipmentColumns; i++)
                {
                    // 영점표시
                    if (weightDatas[i].Status == DataStore.WeightStatus.WeightZero)
                        this.CollectionPictureBoxZero[i].Visible = true;
                    else
                        this.CollectionPictureBoxZero[i].Visible = false;
                }
            }
        }
        public void UpdateStartWeightDisplay(DataStore.EquipmentStatus status, Collection<WeightData> weightDatas)
        {
            this.UpdateStartWeightDisplay1(status, weightDatas[0]);
            this.UpdateStartWeightDisplay2(status, weightDatas[1]);
            this.UpdateStartWeightDisplay3(status, weightDatas[2]);
            this.UpdateStartWeightDisplay4(status, weightDatas[3]);
            this.UpdateStartWeightDisplay5(status, weightDatas[4]);
            this.UpdateStartWeightDisplay6(status, weightDatas[5]);
            this.UpdateStartWeightDisplay7(status, weightDatas[6]);
            this.UpdateStartWeightDisplay8(status, weightDatas[7]);
        }
        public void UpdateStartWeightDisplay1(DataStore.EquipmentStatus status, WeightData weightData)
        {
            this.UpdateDraw(this.CollectionDraw[0], weightData);
        }
        public void UpdateStartWeightDisplay2(DataStore.EquipmentStatus status, WeightData weightData)
        {
            this.UpdateDraw(this.CollectionDraw[1], weightData);
        }
        public void UpdateStartWeightDisplay3(DataStore.EquipmentStatus status, WeightData weightData)
        {
            this.UpdateDraw(this.CollectionDraw[2], weightData);
        }
        public void UpdateStartWeightDisplay4(DataStore.EquipmentStatus status, WeightData weightData)
        {
            this.UpdateDraw(this.CollectionDraw[3], weightData);
        }
        public void UpdateStartWeightDisplay5(DataStore.EquipmentStatus status, WeightData weightData)
        {
            this.UpdateDraw(this.CollectionDraw[4], weightData);
        }
        public void UpdateStartWeightDisplay6(DataStore.EquipmentStatus status, WeightData weightData)
        {
            this.UpdateDraw(this.CollectionDraw[5], weightData);
        }
        public void UpdateStartWeightDisplay7(DataStore.EquipmentStatus status, WeightData weightData)
        {
            this.UpdateDraw(this.CollectionDraw[6], weightData);
        }
        public void UpdateStartWeightDisplay8(DataStore.EquipmentStatus status, WeightData weightData)
        {
            this.UpdateDraw(this.CollectionDraw[7], weightData);
        }
        #endregion

        #region Event Handler
        private void buttonSelectDraw_Click(object sender, EventArgs e)
        {
            SmartButton button = sender as SmartButton;

            if (button == null)
                return;

            if (button == this.button1)
                this.draw1.BringToFront();
            else if (button == this.button2)
                this.draw2.BringToFront();
            else if (button == this.button3)
                this.draw3.BringToFront();
            else if (button == this.button4)
                this.draw4.BringToFront();
            else if (button == this.button5)
                this.draw5.BringToFront();
            else if (button == this.button6)
                this.draw6.BringToFront();
            else if (button == this.button7)
                this.draw7.BringToFront();
            else if (button == this.button8)
                this.draw8.BringToFront();
        }
        #endregion

    }
}
