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

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

        private int SelectedProductNo;
        private ProductItem SelectedProductItem;
        #endregion

        #region Constructor
        public ControlCenterBasicProduct(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.SelectedProductNo = 1;
            this.SelectedProductItem = new ProductItem();

            if (this.listBoxProductList.Items.Count == 0)
            {
                this.listBoxProductList.SelectedIndexChanged -= new EventHandler(this.listBoxProductList_SelectedIndexChanged);
                this.AddProductList();
                this.listBoxProductList.SelectedIndexChanged += new EventHandler(this.listBoxProductList_SelectedIndexChanged);
            }
        }

        private void AddProductList()
        {
            Collection<string> productNames = new Collection<string>();

            this.ParentForm.ParentForm.LoadProductName(ref productNames);

            this.listBoxProductList.Items.Clear();

            for (int i = 0; i < productNames.Count; i++)
                this.listBoxProductList.Items.Add(productNames[i]);
        }
        private void DisplayControls(ProductItem item)
        {
            int iValue = 0;
            string sValue = "";

            if (this.SelectedProductItem == null && item == null)
                return;

            sValue = item.Number.ToString();
            if (this.buttonItemNo.Text != sValue)
                this.buttonItemNo.Text = sValue;

            sValue = item.Name;
            if (this.textBoxProductName.Text != sValue)
                this.textBoxProductName.Text = sValue;

            sValue = item.LotNo;
            if (this.textBoxLotNo.Text != sValue)
                this.textBoxLotNo.Text = sValue;

            sValue = Helper.StringToDecimalPlaces(item.OverRange, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);
            if (this.labelOverRange.Text != sValue)
                this.labelOverRange.Text = sValue;

            sValue = Helper.StringToDecimalPlaces(item.PassRange, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);
            if (this.labelPassRange.Text != sValue)
                this.labelPassRange.Text = sValue;

            sValue = Helper.StringToDecimalPlaces(item.UnderRange, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);
            if (this.labelUnderRange.Text != sValue)
                this.labelUnderRange.Text = sValue;

            sValue = Helper.StringToDecimalPlaces(item.TareRange, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);
            if (this.labelTareRange.Text != sValue)
                this.labelTareRange.Text = sValue;

            iValue = int.Parse(item.OverRange) - int.Parse(item.PassRange);
            sValue = Helper.StringToDecimalPlaces(iValue.ToString(), this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);
            sValue = string.Format("+{0}", sValue);
            if (this.labelDeviationOver.Text != sValue)
                this.labelDeviationOver.Text = sValue;

            iValue = int.Parse(item.PassRange) - int.Parse(item.UnderRange);
            sValue = string.Format("-{0}", Helper.StringToDecimalPlaces(iValue.ToString(), this.ParentForm.ParentForm.SystemConfig.DecimalPlaces));
            if (this.labelDeviationUnder.Text != sValue)
                this.labelDeviationUnder.Text = sValue;
        }

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

            this.buttonItemNo.Text = this.ParentForm.ParentForm.SystemConfig.ProductNumber.ToString();
            this.SelectedProductNo = this.ParentForm.ParentForm.SystemConfig.ProductNumber;

            this.listBoxProductList.SelectedIndex = this.SelectedProductNo - 1;

            this.ParentForm.ParentForm.LoadProductFile(ref this.SelectedProductItem, this.SelectedProductNo - 1);
            this.DisplayControls(this.SelectedProductItem);

            this.smartKeyboard.Hide();
            this.buttonNameEdit.Enabled = true;
            this.buttonLotEdit.Enabled = true;
            this.textBoxProductName.Enabled = true;
            this.textBoxLotNo.Enabled = true;
            this.listBoxProductList.Enabled = true;
        }
        #endregion

        #region Event Handler
        private void buttonProductNo_Click(object sender, EventArgs e)
        {
            DialogFormNumKeyPad myKeyPad = new DialogFormNumKeyPad(this.buttonItemNo.Text, 4, 0, false, this.ParentForm.ParentForm.SystemConfig.Language);
            myKeyPad.Location = new Point(280, 90);

            if (myKeyPad.ShowDialog() == DialogResult.OK)
            {
                if (myKeyPad.doubleValue < 1 || myKeyPad.doubleValue > 1000)
                {
                    // 입력범위를 확인하세요
                    DialogFormMessage myMsg = new DialogFormMessage(1, this.ParentForm.ParentForm.SystemConfig.Language);
                    myMsg.ShowDialog();
                }
                else
                {
                    this.buttonItemNo.Text = myKeyPad.StringValue;
                    this.SelectedProductNo = myKeyPad.IntValue;

                    this.listBoxProductList.SelectedIndexChanged -= new EventHandler(this.listBoxProductList_SelectedIndexChanged);
                    this.listBoxProductList.SelectedIndex = this.SelectedProductNo - 1;
                    this.listBoxProductList.SelectedIndexChanged += new EventHandler(this.listBoxProductList_SelectedIndexChanged);

                    this.ParentForm.ParentForm.LoadProductFile(ref this.SelectedProductItem, this.SelectedProductNo - 1);
                    
                    this.DisplayControls(this.SelectedProductItem);
                }
            }
        }

        private void buttonNameEdit_Click(object sender, EventArgs e)
        {
            this.smartKeyboard.KeyboardType = SmartX.SmartKeyboard.KEYBOARDTYPES.NORMAL;
            this.smartKeyboard.Location = new Point(25, 181);
            this.smartKeyboard.Size = new Size(532,210);
            this.smartKeyboard.Hide();

            this.smartKeyboard.TargetInputObject = this.textBoxProductName;
            this.smartKeyboard.Show();

            this.textBoxProductName.Select(this.textBoxProductName.Text.Length, 0);

            this.buttonNameEdit.Enabled = false;
            this.buttonLotEdit.Enabled = false;
            this.textBoxLotNo.Enabled = false;
            this.listBoxProductList.Enabled = false;
        }

        private void buttonLotEdit_Click(object sender, EventArgs e)
        {
            this.smartKeyboard.KeyboardType = SmartX.SmartKeyboard.KEYBOARDTYPES.NORMAL;
            this.smartKeyboard.Location = new Point(25, 181);
            this.smartKeyboard.Size = new Size(532, 210);
            this.smartKeyboard.Hide();

            this.smartKeyboard.TargetInputObject = this.textBoxLotNo;
            this.smartKeyboard.Show();

            this.textBoxLotNo.Select(this.textBoxLotNo.Text.Length, 0);

            this.buttonNameEdit.Enabled = false;
            this.buttonLotEdit.Enabled = false;
            this.textBoxProductName.Enabled = false;
            this.listBoxProductList.Enabled = false;
        }

        private void textBoxProductName_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == '\r')
            {
                this.SelectedProductItem.Name = this.textBoxProductName.Text;

                this.listBoxProductList.SelectedIndexChanged -= new EventHandler(this.listBoxProductList_SelectedIndexChanged);
                this.listBoxProductList.Items.Insert(this.SelectedProductItem.Number - 1, this.textBoxProductName.Text);
                this.listBoxProductList.Items.RemoveAt(this.SelectedProductItem.Number);
                this.listBoxProductList.SelectedIndex = this.SelectedProductItem.Number - 1;
                this.listBoxProductList.SelectedIndexChanged += new EventHandler(this.listBoxProductList_SelectedIndexChanged);

                e.Handled = true;
                this.smartKeyboard.Hide();

                this.ParentForm.ParentForm.SaveProductFile(this.SelectedProductItem, this.SelectedProductNo - 1);

                this.buttonNameEdit.Enabled = true;
                this.buttonLotEdit.Enabled = true;
                this.textBoxLotNo.Enabled = true;
                this.listBoxProductList.Enabled = true;
            }
            else if (e.KeyChar == 27)
            {
                e.Handled = true;
                this.smartKeyboard.Hide();
                this.ParentForm.ParentForm.LoadProductFile(ref this.SelectedProductItem, this.SelectedProductNo - 1);
                this.DisplayControls(this.SelectedProductItem);

                this.buttonNameEdit.Enabled = true;
                this.buttonLotEdit.Enabled = true;
                this.textBoxLotNo.Enabled = true;
                this.listBoxProductList.Enabled = true;
            }
        }

        private void textBoxLotNo_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == '\r')
            {
                this.SelectedProductItem.LotNo = this.textBoxLotNo.Text;

                e.Handled = true;
                this.smartKeyboard.Hide();

                this.ParentForm.ParentForm.SaveProductFile(this.SelectedProductItem, this.SelectedProductNo - 1);

                this.buttonNameEdit.Enabled = true;
                this.buttonLotEdit.Enabled = true;
                this.textBoxProductName.Enabled = true;
                this.listBoxProductList.Enabled = true;
            }
            else if (e.KeyChar == 27)
            {
                e.Handled = true;
                this.smartKeyboard.Hide();
                this.ParentForm.ParentForm.LoadProductFile(ref this.SelectedProductItem, this.SelectedProductNo - 1);
                this.DisplayControls(this.SelectedProductItem);

                this.buttonNameEdit.Enabled = true;
                this.buttonLotEdit.Enabled = true;
                this.textBoxProductName.Enabled = true;
                this.listBoxProductList.Enabled = true;
            }
        }

        private void labelOverRange_Click(object sender, EventArgs e)
        {
            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.labelOverRange.Text, digit, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces, 
                false, this.ParentForm.ParentForm.SystemConfig.Language);

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

                    this.ParentForm.ParentForm.SaveProductFile(this.SelectedProductItem, this.SelectedProductNo - 1);

                    this.DisplayControls(this.SelectedProductItem);
                }
            }
        }

        private void labelPassRange_Click(object sender, EventArgs e)
        {
            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.labelPassRange.Text, digit, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces, 
                false, this.ParentForm.ParentForm.SystemConfig.Language);

            if (myKeyPad.ShowDialog() == DialogResult.OK)
            {
                if ((myKeyPad.doubleValue > Helper.StringToWeight(this.SelectedProductItem.OverRange, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces)) ||
                    (myKeyPad.doubleValue < Helper.StringToWeight(this.SelectedProductItem.UnderRange, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces)))
                {
                    // 입력범위를 확인하세요
                    DialogFormMessage myMsg = new DialogFormMessage(1, this.ParentForm.ParentForm.SystemConfig.Language);
                    myMsg.ShowDialog();
                }
                else
                {
                    this.labelPassRange.Text = myKeyPad.StringValue;
                    this.SelectedProductItem.PassRange = myKeyPad.StringValue.Replace(".", "");

                    this.ParentForm.ParentForm.SaveProductFile(this.SelectedProductItem, this.SelectedProductNo - 1);
                }
            }
        }

        private void labelUnderRange_Click(object sender, EventArgs e)
        {
            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.labelUnderRange.Text, digit, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces, 
                false, this.ParentForm.ParentForm.SystemConfig.Language);

            if (myKeyPad.ShowDialog() == DialogResult.OK)
            {
                if (myKeyPad.doubleValue > Helper.StringToWeight(this.SelectedProductItem.PassRange.ToString(), this.ParentForm.ParentForm.SystemConfig.DecimalPlaces))
                {
                    // 입력범위를 확인하세요
                    DialogFormMessage myMsg = new DialogFormMessage(1, this.ParentForm.ParentForm.SystemConfig.Language);
                    myMsg.ShowDialog();
                }
                else
                {
                    this.labelUnderRange.Text = myKeyPad.StringValue;
                    this.SelectedProductItem.UnderRange = myKeyPad.StringValue.Replace(".", "");

                    this.ParentForm.ParentForm.SaveProductFile(this.SelectedProductItem, this.SelectedProductNo - 1);

                    this.DisplayControls(this.SelectedProductItem);
                }
            }
        }

        private void labelDeviationOver_Click(object sender, EventArgs e)
        {
            int digit = 0;
            string sValue = "";
            double dValue = 0.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.labelDeviationOver.Text, digit, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces,
                false, this.ParentForm.ParentForm.SystemConfig.Language);

            if (myKeyPad.ShowDialog() == DialogResult.OK)
            {
                dValue = Helper.StringToWeight(this.SelectedProductItem.PassRange, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces)
                       + myKeyPad.doubleValue;
                sValue = Helper.DoubleToString(dValue, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);

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

                    this.SelectedProductItem.OverRange = sValue.Replace(".", "");
                    this.ParentForm.ParentForm.SaveProductFile(this.SelectedProductItem, this.SelectedProductNo - 1);

                    this.DisplayControls(this.SelectedProductItem);
                }
            }
        }

        private void labelDeviationUnder_Click(object sender, EventArgs e)
        {
            int digit = 0;
            string sValue = "";
            double dValue = 0.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.labelDeviationUnder.Text, digit, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces,
                false, this.ParentForm.ParentForm.SystemConfig.Language);

            if (myKeyPad.ShowDialog() == DialogResult.OK)
            {
                dValue = Helper.StringToWeight(this.SelectedProductItem.PassRange, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces)
                       - Math.Abs(myKeyPad.doubleValue);
                sValue = Helper.DoubleToString(dValue, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);

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

                    this.SelectedProductItem.UnderRange = sValue.Replace(".", "");
                    this.ParentForm.ParentForm.SaveProductFile(this.SelectedProductItem, this.SelectedProductNo - 1);

                    this.DisplayControls(this.SelectedProductItem);
                }
            }
        }

        private void labelTareRange_Click(object sender, EventArgs e)
        {
            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.labelTareRange.Text, digit, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces, 
                false, this.ParentForm.ParentForm.SystemConfig.Language);
            myKeyPad.Location = new Point(100, 90);

            if (myKeyPad.ShowDialog() == DialogResult.OK)
            {
                if (myKeyPad.doubleValue < 0 || myKeyPad.doubleValue > 99999)
                {
                    // 입력범위를 확인하세요
                    DialogFormMessage myMsg = new DialogFormMessage(1, this.ParentForm.ParentForm.SystemConfig.Language);
                    myMsg.ShowDialog();
                }
                else
                {
                    this.labelTareRange.Text = myKeyPad.StringValue;
                    this.SelectedProductItem.TareRange = myKeyPad.StringValue.Replace(".", "");

                    this.ParentForm.ParentForm.SaveProductFile(this.SelectedProductItem, this.SelectedProductNo - 1);
                }
            }
        }

        private void listBoxProductList_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.listBoxProductList.SelectedItem == null)
                return;

            this.SelectedProductNo = this.listBoxProductList.SelectedIndex + 1;
            this.buttonItemNo.Text = this.SelectedProductNo.ToString();

            this.ParentForm.ParentForm.LoadProductFile(ref this.SelectedProductItem, this.SelectedProductNo - 1);

            this.DisplayControls(this.SelectedProductItem);
        }

        private void smartKeyboard_OnXKeyClick(object sender, EventArgs e)
        {
            this.smartKeyboard.Hide();
            this.ParentForm.ParentForm.LoadProductFile(ref this.SelectedProductItem, this.SelectedProductNo - 1);
            this.DisplayControls(this.SelectedProductItem);
            this.buttonNameEdit.Enabled = true;
            this.buttonLotEdit.Enabled = true;
            this.textBoxProductName.Enabled = true;
            this.textBoxLotNo.Enabled = true;
            this.listBoxProductList.Enabled = true;
        }
        #endregion
    }
}
