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_2C.Forms;

namespace INT63DC_2C.Controls
{
    public partial class ControlMainDisplayDotGraph15 : 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 ControlMainDisplayDotGraph15(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; }
        }
        private int EquipmentColumns { get { return this.ParentForm.ParentForm.SystemConfig.EquipmentColumns; } }
        #endregion

        #region Method
        private void CreateCollection()
        {
            this.CollectionPictureBoxZero = new Collection<PictureBox>();
            this.CollectionDraw = new Collection<SmartDraw>();
            this.CollectionPictureBoxBypass = new Collection<PictureBox>();

            for (int i = 1; i <= this.EquipmentColumns; i++)
            {
                this.CollectionPictureBoxZero.Add(FindByNameUtil.PictureBox("pictureBox" + i, this));
                this.CollectionDraw.Add(FindByNameUtil.SmartDraw("draw" + i, this));
                this.CollectionPictureBoxBypass.Add(FindByNameUtil.PictureBox("pictureBoxBypass" + i, this));
            }
        }
        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;

            // 2 : 432개
            // 16 : 54개
            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 = (int.Parse(weightData.WeightString) - 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)
        {
            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)
        {
            for (int i = 0; i < this.CollectionDraw.Count; i++)
                this.UpdateDraw(this.CollectionDraw[i], weightDatas[i]);
        }
        #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();
            else if (button == this.button9)
                this.draw9.BringToFront();
            else if (button == this.button10)
                this.draw10.BringToFront();
            else if (button == this.button11)
                this.draw11.BringToFront();
            else if (button == this.button12)
                this.draw12.BringToFront();
            else if (button == this.button13)
                this.draw13.BringToFront();
            else if (button == this.button14)
                this.draw14.BringToFront();
            else if (button == this.button15)
                this.draw15.BringToFront();
        }
        #endregion
    }
}
