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 INT51DB.DialogForms;
using INT51DB.Forms;
using INT51DB_ImageDll;

namespace INT51DB.Controls
{
    public partial class ControlMainDisplayLineGraph : UserControl
    {
        #region Field
        private FormMainDisplay m_ParentForm;
        
        #endregion
        
        #region Constructor
        public ControlMainDisplayLineGraph(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)
            {
            }
            else if (this.ParentForm.ParentForm.SystemConfig.Language == DataStore.LanguageID.Chinese)
            {
            }
            else if (this.ParentForm.ParentForm.SystemConfig.Language == DataStore.LanguageID.Czech)
            {
            }
            else
            {
            }
        }
        private void DefaultSetting()
        {
            this.InitializeDraw();
        }

        private void InitializeDraw()
        {
            SmartDraw.CHARTREFRESH charRefreshType = SmartDraw.CHARTREFRESH.LEFTSCROLL;
            SmartDraw.CHARTPENSTYLE[] charPenStyle;
            charPenStyle = new SmartX.SmartDraw.CHARTPENSTYLE[1];
            charPenStyle[0].m_chColor = Color.White;
            charPenStyle[0].m_iPenWidth = 2;


            this.draw.SetChartCfg(0, 198, 420, 198, 5, charRefreshType, 1);
            this.draw.ChartChannelPenStyle = charPenStyle;
            this.draw.ChartDrawStep = 1;
        }
        private void SetDrawData(string stringWeight, ProductItem product)
        {
            int value = 0, gap = 0, startingPoint = 0, weight = 0;

            try
            {
                weight = int.Parse(stringWeight);
                gap = int.Parse(product.OverRange.Replace(".", "")) - int.Parse(product.UnderRange.Replace(".", ""));
                startingPoint = int.Parse(product.UnderRange.Replace(".", "")) - gap;

                value = weight - startingPoint;
                value = (value * 66) / gap;

                if (value > 197)
                    value = 197;
                if (value < 1)
                    value = 1;

                draw.PutData(value);
            }
            catch
            {
            }
        }
        private void SetProduct(ProductItem item)
        {
            string underValue = "", passValue = "", overValue = "";

            switch (this.ParentForm.ParentForm.CurrentSystemStatus.CurrentWeightInputMode)
            {
                case DataStore.WeightInputMode.Weight:
                    underValue = Helper.StringToDecimalPlaces(item.UnderRange, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);
                    passValue = Helper.StringToDecimalPlaces(item.PassRange, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);
                    overValue = Helper.StringToDecimalPlaces(item.OverRange, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);
                    break;
                case DataStore.WeightInputMode.Deviation:
                    underValue = Helper.StringToDecimalPlaces(item.UnderRangDeviation.ToString(), this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);
                    passValue = Helper.StringToDecimalPlaces(item.PassRange, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);
                    overValue = Helper.StringToDecimalPlaces(item.OverRangeDeviation.ToString(), this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);
                    overValue = string.Format("+{0}", overValue);
                    break;
                default:
                    break;
            }

            if (this.buttonUnderRange.Text != underValue)
                this.buttonUnderRange.Text = underValue;

            if (this.buttonPassRange.Text != passValue)
                this.buttonPassRange.Text = passValue;

            if (this.buttonOverRange.Text != overValue)
                this.buttonOverRange.Text = overValue;
        }
        private void SetCount(WeightData data)
        {
            string value = "";

            value = data.UnderCount.ToString();
            if (this.labelUnderCount.Text != value)
                this.labelUnderCount.Text = value;

            value = data.PassCount.ToString();
            if (this.labelPassCount.Text != value)
                this.labelPassCount.Text = value;

            value = data.OverCount.ToString();
            if (this.labelOverCount.Text != value)
                this.labelOverCount.Text = value;

            value = data.UnderRatio;
            if (this.labelUnderRatio.Text != value)
                this.labelUnderRatio.Text = value;

            value = data.PassRatio;
            if (this.labelPassRatio.Text != value)
                this.labelPassRatio.Text = value;

            value = data.OverRatio;
            if (this.labelOverRatio.Text != value)
                this.labelOverRatio.Text = value;
        }
        private void UpdateDisplayUser(SystemStatus status)
        {
            switch (status.CurrentUser.Group)
            {
                case DataStore.UserGroup.LogOff:
                    this.buttonUnderRange.Enabled = false;
                    this.buttonPassRange.Enabled = false;
                    this.buttonOverRange.Enabled = false;
                    break;
                case DataStore.UserGroup.Level1Operator:
                    this.buttonUnderRange.Enabled = true;
                    this.buttonPassRange.Enabled = true;
                    this.buttonOverRange.Enabled = true;
                    break;
                case DataStore.UserGroup.Level2Engineer:
                    this.buttonUnderRange.Enabled = true;
                    this.buttonPassRange.Enabled = true;
                    this.buttonOverRange.Enabled = true;
                    break;
                case DataStore.UserGroup.Level3Manager:
                    this.buttonUnderRange.Enabled = true;
                    this.buttonPassRange.Enabled = true;
                    this.buttonOverRange.Enabled = true;
                    break;
                case DataStore.UserGroup.Level4Developer:
                    break;
                default:
                    break;
            }
        }


        public void UpdateEquipmentStatusDisplay(DataStore.EquipmentStatus status)
        {

        }
        public void UpdateCurrentProductDisplay(DataStore.EquipmentStatus status, ProductItem pItem, WeightData wData)
        {
            this.SetProduct(pItem);

            this.SetCount(wData);

            this.draw.PutDataAllClear();
        }
        public void UpdateUpdateStartWeightDisplay(DataStore.EquipmentStatus status, WeightData data)
        {
            this.SetCount(data);
            this.SetDrawData(data.WeightString, this.ParentForm.ParentForm.CurrentProductItem);
        }

        public void Clear()
        {
            this.SetCount(this.ParentForm.ParentForm.CurrentWeightData);
            this.draw.PutDataAllClear();
        }
        public void DisplayRefresh(SystemStatus status)
        {
            //this.UpdateDisplayUser(status);

            this.SetProduct(this.ParentForm.ParentForm.CurrentProductItem);
            this.SetCount(this.ParentForm.ParentForm.CurrentWeightData);
        }
        #endregion

        #region Event Handler
        private void buttonUnderRange_Click(object sender, EventArgs e)
        {
            string value = "", sValue = "";
            double dValue = 0.0;
            int digit = 0;

            if (this.ParentForm.ParentForm.SystemConfig.DecimalPlaces == 0)
                digit = 4;
            else if (this.ParentForm.ParentForm.SystemConfig.DecimalPlaces == 1)
                digit = 5;
            else if (this.ParentForm.ParentForm.SystemConfig.DecimalPlaces == 2)
                digit = 6;
            else
                digit = 5;

            DialogFormNumKeyPad myKeyPad = new DialogFormNumKeyPad(this.buttonUnderRange.Text, digit, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces,
               false, this.ParentForm.ParentForm.SystemConfig.Language);

            if (myKeyPad.ShowDialog() != DialogResult.OK)
                return;

            switch (this.ParentForm.ParentForm.CurrentSystemStatus.CurrentWeightInputMode)
            {
                case DataStore.WeightInputMode.Weight:
                    dValue = myKeyPad.doubleValue;
                    sValue = myKeyPad.StringValue;
                    break;
                case DataStore.WeightInputMode.Deviation:
                    dValue = Helper.StringToWeight(this.ParentForm.ParentForm.CurrentProductItem.PassRange, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces)
                        - Math.Abs(myKeyPad.doubleValue);
                    sValue = Helper.DoubleToString(dValue, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);
                    break;
                default:
                    dValue = myKeyPad.doubleValue;
                    sValue = myKeyPad.StringValue;
                    break;
            }

            if (dValue > Helper.StringToWeight(this.ParentForm.ParentForm.CurrentProductItem.PassRange, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces) || dValue < 0)
            {
                // 입력범위를 확인하세요
                DialogFormMessage myMsg = new DialogFormMessage(1, this.ParentForm.ParentForm.SystemConfig.Language);
                myMsg.ShowDialog();
            }
            else
            {
                this.ParentForm.ParentForm.CurrentProductItem.UnderRange = sValue.Replace(".", "");
                this.buttonUnderRange.Text = myKeyPad.StringValue;

                this.ParentForm.RescaleControl();

                this.ParentForm.ParentForm.SaveProductFile(this.ParentForm.ParentForm.CurrentProductItem, this.ParentForm.ParentForm.SystemConfig.ProductNumber - 1);

                value = Helper.StringZeroFillDigits7(this.ParentForm.ParentForm.CurrentProductItem.UnderRange);
                this.ParentForm.ParentForm.TransferDataStream(CommunicationCommand.Write, CommunicationID.MainBoard, CommunicationAddress.UnderRange, value);
            }
        }

        private void buttonPassRange_Click(object sender, EventArgs e)
        {
            string value = "";
            int digit = 0, temp = 0;
            StringBuilder sb = new StringBuilder();

            if (this.ParentForm.ParentForm.SystemConfig.DecimalPlaces == 0)
                digit = 4;
            else if (this.ParentForm.ParentForm.SystemConfig.DecimalPlaces == 1)
                digit = 5;
            else if (this.ParentForm.ParentForm.SystemConfig.DecimalPlaces == 2)
                digit = 6;
            else
                digit = 5;

            DialogFormNumKeyPad myKeyPad = new DialogFormNumKeyPad(this.buttonPassRange.Text, digit, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces,
               false, this.ParentForm.ParentForm.SystemConfig.Language);

            if (myKeyPad.ShowDialog() != DialogResult.OK)
                return;

            switch (this.ParentForm.ParentForm.CurrentSystemStatus.CurrentWeightInputMode)
            {
                case DataStore.WeightInputMode.Weight:
                    #region WeightMode
                    if ((myKeyPad.doubleValue > Helper.StringToWeight(this.ParentForm.ParentForm.CurrentProductItem.OverRange, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces))
                                    || (myKeyPad.doubleValue < Helper.StringToWeight(this.ParentForm.ParentForm.CurrentProductItem.UnderRange, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces)))
                    {
                        // 입력범위를 확인하세요
                        DialogFormMessage myMsg = new DialogFormMessage(1, this.ParentForm.ParentForm.SystemConfig.Language);
                        myMsg.ShowDialog();
                    }
                    else
                    {
                        this.ParentForm.ParentForm.CurrentProductItem.PassRange = myKeyPad.StringValue.Replace(".", "");
                        this.buttonPassRange.Text = myKeyPad.StringValue;

                        this.ParentForm.RescaleControl();

                        this.ParentForm.ParentForm.SaveProductFile(this.ParentForm.ParentForm.CurrentProductItem, this.ParentForm.ParentForm.SystemConfig.ProductNumber - 1);

                        value = Helper.StringZeroFillDigits7(this.ParentForm.ParentForm.CurrentProductItem.PassRange);
                        this.ParentForm.ParentForm.TransferDataStream(CommunicationCommand.Write, CommunicationID.MainBoard, CommunicationAddress.PassRange, value);
                    }
                    #endregion
                    break;
                case DataStore.WeightInputMode.Deviation:
                    #region DeviationMode
                    this.ParentForm.ParentForm.CurrentProductItem.PassRange = myKeyPad.StringValue.Replace(".", "");

                    temp = this.ParentForm.ParentForm.CurrentProductItem.PassRangeInt + this.ParentForm.ParentForm.CurrentProductItem.UnderRangDeviation;
                    if (temp < 0)
                        this.ParentForm.ParentForm.CurrentProductItem.UnderRange = "0";
                    else
                        this.ParentForm.ParentForm.CurrentProductItem.UnderRange = temp.ToString();

                    temp = this.ParentForm.ParentForm.CurrentProductItem.PassRangeInt + this.ParentForm.ParentForm.CurrentProductItem.OverRangeDeviation;
                    if (temp > 10000)
                        this.ParentForm.ParentForm.CurrentProductItem.OverRange = this.ParentForm.ParentForm.CurrentProductItem.PassRange;
                    else
                        this.ParentForm.ParentForm.CurrentProductItem.OverRange = temp.ToString();

                    this.ParentForm.RescaleControl();

                    this.ParentForm.ParentForm.SaveProductFile(this.ParentForm.ParentForm.CurrentProductItem, this.ParentForm.ParentForm.SystemConfig.ProductNumber - 1);

                    this.buttonPassRange.Text = myKeyPad.StringValue;
                    this.buttonUnderRange.Text = Helper.StringToDecimalPlaces(this.ParentForm.ParentForm.CurrentProductItem.UnderRangDeviation.ToString(), this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);
                    this.buttonOverRange.Text = Helper.StringToDecimalPlaces(this.ParentForm.ParentForm.CurrentProductItem.OverRangeDeviation.ToString(), this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);

                    sb.Append(Helper.StringZeroFillDigits7(this.ParentForm.ParentForm.CurrentProductItem.UnderRange));
                    sb.Append(Helper.StringZeroFillDigits7(this.ParentForm.ParentForm.CurrentProductItem.OverRange));
                    sb.Append(Helper.StringZeroFillDigits7(this.ParentForm.ParentForm.CurrentProductItem.TareRange));
                    sb.Append(Helper.StringZeroFillDigits4(this.ParentForm.ParentForm.SystemConfig.ProductNumber.ToString()));
                    this.ParentForm.ParentForm.TransferDataStream(CommunicationCommand.Write, CommunicationID.MainBoard, CommunicationAddress.ParameterWeightSetting, sb.ToString());
                    #endregion
                    break;
                default:
                    break;
            }
        }

        private void buttonOverRange_Click(object sender, EventArgs e)
        {
            string value = "", sValue = "";
            double dValue = 0.0;
            int digit = 0;

            if (this.ParentForm.ParentForm.SystemConfig.DecimalPlaces == 0)
                digit = 4;
            else if (this.ParentForm.ParentForm.SystemConfig.DecimalPlaces == 1)
                digit = 5;
            else if (this.ParentForm.ParentForm.SystemConfig.DecimalPlaces == 2)
                digit = 6;
            else
                digit = 5;

            DialogFormNumKeyPad myKeyPad = new DialogFormNumKeyPad(this.buttonOverRange.Text, digit, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces,
                false, this.ParentForm.ParentForm.SystemConfig.Language);

            if (myKeyPad.ShowDialog() != DialogResult.OK)
                return;

            switch (this.ParentForm.ParentForm.CurrentSystemStatus.CurrentWeightInputMode)
            {
                case DataStore.WeightInputMode.Weight:
                    dValue = myKeyPad.doubleValue;
                    sValue = myKeyPad.StringValue;
                    break;
                case DataStore.WeightInputMode.Deviation:
                    dValue = Helper.StringToWeight(this.ParentForm.ParentForm.CurrentProductItem.PassRange, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces)
                        + myKeyPad.doubleValue;
                    sValue = Helper.DoubleToString(dValue, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);
                    break;
                default:
                    dValue = myKeyPad.doubleValue;
                    sValue = myKeyPad.StringValue;
                    break;
            }

            if (dValue < Helper.StringToWeight(this.ParentForm.ParentForm.CurrentProductItem.PassRange, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces))
            {
                // 입력범위를 확인하세요
                DialogFormMessage myMsg = new DialogFormMessage(1, this.ParentForm.ParentForm.SystemConfig.Language);
                myMsg.ShowDialog();
            }
            else
            {
                this.ParentForm.ParentForm.CurrentProductItem.OverRange = sValue.Replace(".", "");
                this.buttonOverRange.Text = myKeyPad.StringValue;

                this.ParentForm.RescaleControl();

                this.ParentForm.ParentForm.SaveProductFile(this.ParentForm.ParentForm.CurrentProductItem, this.ParentForm.ParentForm.SystemConfig.ProductNumber - 1);

                value = Helper.StringZeroFillDigits7(this.ParentForm.ParentForm.CurrentProductItem.OverRange);
                this.ParentForm.ParentForm.TransferDataStream(CommunicationCommand.Write, CommunicationID.MainBoard, CommunicationAddress.OverRange, value);
            }
        }

        #endregion
    }
}
