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

namespace INT51DB.Controls
{
    public partial class ControlCenterSystemCalibration : UserControl
    {
        #region Field
        private FormMenu m_ParentForm;

        private Color NormalColor;
        private Color FinishColor;

        // 중량 조정이 완료 되었는지 확인하는 Flag
        private bool IsCalibrationFinish;
        #endregion

        #region Constructor
        public ControlCenterSystemCalibration(FormMenu parent)
        {
            InitializeComponent();

            this.ParentForm = parent;

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

        #region Property
        public FormMenu 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.NormalColor = Color.White;
            this.FinishColor = Color.Lime;

            this.IsCalibrationFinish = false;
        }

        private void CalibrationButtonEnable(bool btStart, bool btBalance, bool btCcancel)
        {
            if (this.buttonStart.Enabled != btStart)
                this.buttonStart.Enabled = btStart;
            if (this.buttonBalance.Enabled != btBalance)
                this.buttonBalance.Enabled = btBalance;
            if (this.buttonCancel.Enabled != btCcancel)
                this.buttonCancel.Enabled = btCcancel;
        }
        private void CalibrationPictureBoxVisible(bool step1, bool step2, bool step3)
        {
            if (step1 == true)
            {
                this.groupBoxStep1.Visible = true;
                this.groupBoxStep2.Visible = false;
                this.groupBoxStep3.Visible = false;
            }
            else if (step2 == true)
            {
                this.groupBoxStep1.Visible = false;
                this.groupBoxStep2.Visible = true;
                this.groupBoxStep3.Visible = false;
            }
            else if(step3 == true)
            {
                this.groupBoxStep1.Visible = false;
                this.groupBoxStep2.Visible = false;
                this.groupBoxStep3.Visible = true;
            }
            else
            {
                this.groupBoxStep1.Visible = true;
                this.groupBoxStep2.Visible = false;
                this.groupBoxStep3.Visible = false;
            }
        }

        private void UpdateControlsDisplay()
        {
            this.listBoxHelp.Items.Clear();
            this.listBoxHelp.AddItem("중량조정을 하시려면");
            this.listBoxHelp.AddItem("시작 버튼을 누르세요");

            this.CalibrationButtonEnable(true, false, false);
            this.CalibrationPictureBoxVisible(true, false, false);

            if (this.ParentForm.ParentForm.SystemConfig.DecimalPlaces == 0)
            {
                if (this.radioButtonDecimalPlaces0.Checked != true)
                    this.radioButtonDecimalPlaces0.Checked = true;
            }
            else if (this.ParentForm.ParentForm.SystemConfig.DecimalPlaces == 1)
            {
                if (this.radioButtonDecimalPlaces1.Checked != true)
                    this.radioButtonDecimalPlaces1.Checked = true;
            }
            else if (this.ParentForm.ParentForm.SystemConfig.DecimalPlaces == 2)
            {
                if (this.radioButtonDecimalPlaces2.Checked != true)
                    this.radioButtonDecimalPlaces2.Checked = true;
            }
            else
            {
                if (this.radioButtonDecimalPlaces0.Checked != true)
                    this.radioButtonDecimalPlaces0.Checked = true;
            }

            if (this.ParentForm.ParentForm.SystemConfig.Unit == "g")
            {
                if (this.radioButtonUnitG.Checked != true)
                    this.radioButtonUnitG.Checked = true;
            }
            else if (this.ParentForm.ParentForm.SystemConfig.Unit == "kg")
            {
                if (this.radioButtonUnitKg.Checked != true)
                    this.radioButtonUnitKg.Checked = true;
            }
            else
            {
                if (this.radioButtonUnitG.Checked != true)
                    this.radioButtonUnitG.Checked = true;
            }

            if (this.labelMaxWeightUnit.Text != this.ParentForm.ParentForm.SystemConfig.Unit)
                this.labelMaxWeightUnit.Text = this.ParentForm.ParentForm.SystemConfig.Unit;
            if (this.labelBalanceWeightUnit.Text != this.ParentForm.ParentForm.SystemConfig.Unit)
                this.labelBalanceWeightUnit.Text = this.ParentForm.ParentForm.SystemConfig.Unit;
            if (this.labelWeightUnit.Text != this.ParentForm.ParentForm.SystemConfig.Unit)
                this.labelWeightUnit.Text = this.ParentForm.ParentForm.SystemConfig.Unit;
        }
        public void UpdateCalibrationItemDisplay(DataStore.EquipmentStatus status, CalibrationItem item)
        {
            string value = "";

            value = Helper.CommunicationWeightValueToString(item.BalanceWeight, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);
            if (this.labelBalanceWeight.Text != value)
                this.labelBalanceWeight.Text = value;

            value = Helper.CommunicationWeightValueToString(item.MaxWeight, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);
            if (this.labelMaxWeight.Text != value)
                this.labelMaxWeight.Text = value;

            value = item.Digit.Trim();
            if (value == "1")
            {
                if (this.radioButtonDigit1.Checked != true)
                    this.radioButtonDigit1.Checked = true;
            }
            else if (value == "2")
            {
                if (this.radioButtonDigit2.Checked != true)
                    this.radioButtonDigit2.Checked = true;
            }
            else if (value == "5")
            {
                if (this.radioButtonDigit5.Checked != true)
                    this.radioButtonDigit5.Checked = true;
            }
            else
            {
                if (this.radioButtonDigit1.Checked != true)
                    this.radioButtonDigit1.Checked = true;
            }

            value = Helper.StringToDecimalPlaces(item.Constant.Trim(), 6);
            if (this.labelConstant.Text != value)
                this.labelConstant.Text = value;
        }
        public void UpdateWeightDisplay(DataStore.EquipmentStatus status, WeightData data)
        {
            switch (data.Status)
            {
                case DataStore.WeightStatus.CalNomal:
                    this.labelWeight.Text = Helper.DoubleToString(data.Weight, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);
                    this.labelWeight.ForeColor = this.NormalColor;
                    this.labelADC.Text = data.ADCValue;
                    this.CalibrationButtonEnable(true, false, false);
                    this.CalibrationPictureBoxVisible(true, false, false);
                    break;
                case DataStore.WeightStatus.CalStandby:
                    // 분동중량 표시
                    this.labelWeight.Text = string.Format("-{0}", this.labelBalanceWeight.Text);
                    this.labelWeight.ForeColor = this.NormalColor;
                    this.labelADC.Text = data.ADCValue;

                    this.listBoxHelp.Items.Clear();
                    this.listBoxHelp.AddItem("분동을 올려주세요");

                    this.CalibrationButtonEnable(false, true, true);
                    this.CalibrationPictureBoxVisible(false, true, false);
                    break;
                case DataStore.WeightStatus.CalBalans:
                    // 분동중량표시
                    this.labelWeight.Text = string.Format("-{0}", this.labelBalanceWeight.Text);
                    this.labelWeight.ForeColor = this.NormalColor;
                    this.labelADC.Text = data.ADCValue;
                    this.CalibrationButtonEnable(false, false, true);
                    this.CalibrationPictureBoxVisible(false, false, true);
                    break;
                case DataStore.WeightStatus.CalFinish:
                    this.listBoxHelp.Items.Clear();
                    this.listBoxHelp.AddItem("중량조정이 완료 되었습니다");

                    this.CalibrationButtonEnable(true, false, false);
                    this.CalibrationPictureBoxVisible(true, false, false);
                    this.labelWeight.Text = Helper.DoubleToString(data.Weight, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);
                    this.labelWeight.ForeColor = this.FinishColor;
                    this.labelADC.Text = data.ADCValue;
                    if (this.IsCalibrationFinish == false)
                    {
                        this.IsCalibrationFinish = true;
                        this.ParentForm.ParentForm.TransferDataStream(CommunicationCommand.Read, CommunicationID.MainBoard, CommunicationAddress.ReadConstant, "");
                    }
                    break;
                case DataStore.WeightStatus.CalError:
                    this.listBoxHelp.Items.Clear();
                    this.listBoxHelp.AddItem("중량조정 에러");
                    this.listBoxHelp.AddItem("다시 시도해 주세요.");

                    this.CalibrationButtonEnable(true, false, false);
                    this.CalibrationPictureBoxVisible(true, false, false);
                    this.labelWeight.Text = Helper.DoubleToString(data.Weight, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);
                    this.labelWeight.ForeColor = this.NormalColor;
                    this.labelADC.Text = data.ADCValue;
                    break;
                default:
                    break;
            }
        }

        public void DisplayRefresh(DataStore.UserGroup group)
        {
            this.ParentForm.ParentForm.CurrentSystemStatus.CurrentDisplay = DataStore.DisplayStore.SystemCalibration;
            this.ParentForm.ParentForm.SetDisplayMode(DataStore.DisplayMode.Calibration);
            this.ParentForm.DisplayTitleRoot(this.ParentForm.ParentForm.CurrentSystemStatus);

            this.ParentForm.ParentForm.TransferDataStream(CommunicationCommand.Read, CommunicationID.MainBoard, CommunicationAddress.ParameterRead3901, "");

            this.IsCalibrationFinish = false;

            this.UpdateControlsDisplay();

            this.Refresh();
        }
        #endregion

        #region Event Handler
        private void labelMaxWeight_Click(object sender, EventArgs e)
        {
            string value = "";
            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.labelMaxWeight.Text, digit, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces, 
                false, this.ParentForm.ParentForm.SystemConfig.Language);

            if (myKeyPad.ShowDialog() == DialogResult.OK)
            {
                if (myKeyPad.doubleValue < 0 || myKeyPad.doubleValue > 9999)
                {
                    // 입력범위를 확인하세요
                    DialogFormMessage myMsg = new DialogFormMessage(1, this.ParentForm.ParentForm.SystemConfig.Language);
                    myMsg.Location = new Point(167, 207);
                    myMsg.ShowDialog();
                }
                else
                {
                    this.labelMaxWeight.Text = myKeyPad.StringValue;

                    value = Helper.StringZeroFillDigits7(this.labelMaxWeight.Text.Replace(".", ""));
                    this.ParentForm.ParentForm.TransferDataStream(CommunicationCommand.Write, CommunicationID.SubBoardAll, CommunicationAddress.MaxWeight, value);
                    this.ParentForm.ParentForm.CurrentCalibrationItem.MaxWeight = value;
                }
            }
        }
        private void labelBalanceWeight_Click(object sender, EventArgs e)
        {
            string value = "";
            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.labelBalanceWeight.Text, digit, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces,
                false, this.ParentForm.ParentForm.SystemConfig.Language);

            if (myKeyPad.ShowDialog() == DialogResult.OK)
            {
                if (myKeyPad.doubleValue < 0 || myKeyPad.doubleValue > 9999)
                {
                    // 입력범위를 확인하세요
                    DialogFormMessage myMsg = new DialogFormMessage(1, this.ParentForm.ParentForm.SystemConfig.Language);
                    myMsg.Location = new Point(167, 207);
                    myMsg.ShowDialog();
                }
                else
                {
                    this.labelBalanceWeight.Text = myKeyPad.StringValue;

                    value = Helper.StringZeroFillDigits7(this.labelBalanceWeight.Text.Replace(".", ""));
                    this.ParentForm.ParentForm.TransferDataStream(CommunicationCommand.Write, CommunicationID.SubBoardAll, CommunicationAddress.BalanceWeight, value);
                    this.ParentForm.ParentForm.CurrentCalibrationItem.BalanceWeight = value;
                }
            }
        }

        private void radioButtonUnit_Click(object sender, EventArgs e)
        {
            if (this.radioButtonUnitG.Checked == true)
                this.ParentForm.ParentForm.SystemConfig.Unit = "g";
            else if(this.radioButtonUnitKg.Checked == true)
                this.ParentForm.ParentForm.SystemConfig.Unit = "kg";
            else
                this.ParentForm.ParentForm.SystemConfig.Unit = "g";

            this.ParentForm.ParentForm.SaveSystemConfigurationFile(this.ParentForm.ParentForm.SystemConfig);

            this.labelMaxWeightUnit.Text = this.labelBalanceWeightUnit.Text = this.labelWeightUnit.Text = this.ParentForm.ParentForm.SystemConfig.Unit;
        }
        private void radioButtonDigit_Click(object sender, EventArgs e)
        {
            string value = "";

            if (this.radioButtonDigit1.Checked == true)
                value = "1";
            else if (this.radioButtonDigit2.Checked == true)
                value = "2";
            else if (this.radioButtonDigit5.Checked == true)
                value = "5";
            else
                value = "1";

            value = Helper.StringZeroFillDigits4(value);

            this.ParentForm.ParentForm.TransferDataStream(CommunicationCommand.Write, CommunicationID.SubBoardAll, CommunicationAddress.Digit, value);
        }
        private void radioButtonDecimalPlaces_Click(object sender, EventArgs e)
        {
            if (this.radioButtonDecimalPlaces0.Checked == true)
                this.ParentForm.ParentForm.SystemConfig.DecimalPlaces = 0;
            else if(this.radioButtonDecimalPlaces1.Checked == true)
                this.ParentForm.ParentForm.SystemConfig.DecimalPlaces = 1;
            else if(this.radioButtonDecimalPlaces2.Checked == true)
                this.ParentForm.ParentForm.SystemConfig.DecimalPlaces = 2;

            this.ParentForm.ParentForm.SaveSystemConfigurationFile(this.ParentForm.ParentForm.SystemConfig);

            this.UpdateCalibrationItemDisplay(this.ParentForm.ParentForm.CurrentSystemStatus.Equipment, this.ParentForm.ParentForm.CurrentCalibrationItem);
        }

        private void buttonZero_Click(object sender, EventArgs e)
        {
            this.ParentForm.ParentForm.TransferData(CommunicationCommand.Zero, CommunicationID.SubBoard1);
        }
        private void buttonStart_Click(object sender, EventArgs e)
        {
            this.listBoxHelp.Items.Clear();
            this.listBoxHelp.AddItem("중량조정을 시작합니다");
            this.listBoxHelp.AddItem("잠시만 기다리세요");

            this.ParentForm.ParentForm.TransferData(CommunicationCommand.CalibrationStart, CommunicationID.SubBoard1);

            this.CalibrationButtonEnable(false, false, false);
        }
        private void buttonBalance_Click(object sender, EventArgs e)
        {
            this.listBoxHelp.Items.Clear();
            this.listBoxHelp.AddItem("분동 계산중 입니다");
            this.listBoxHelp.AddItem("잠시만 기다리세요");

            this.ParentForm.ParentForm.TransferData(CommunicationCommand.CalibrationBalance, CommunicationID.SubBoard1);
            this.CalibrationButtonEnable(false, false, true);
        }
        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.listBoxHelp.Items.Clear();
            this.listBoxHelp.AddItem("중량조정을 취소합니다");

            this.ParentForm.ParentForm.TransferData(CommunicationCommand.CalibrationCancel, CommunicationID.SubBoard1);
            this.CalibrationButtonEnable(true, false, false);
        }
        #endregion
    }
}
