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

using SmartX;
using ITC81DB_2H_ImageDll;

namespace ITC81DB_0H.DialogForms
{
    public partial class DialogFormNumKeyPad : Form
    {
        #region Field
        private bool IsAddition;
        private bool IsEditing;
        private int Digit;
        private int DecimalPlaces;
        StringBuilder sbInputValue;

        private string m_StringValue;
        #endregion

        #region Constructor
        public DialogFormNumKeyPad(string value, int dig, int decimalPlaces, bool isSignEnable, DataStore.LanguageID language)
        {
            InitializeComponent();

            this.sbInputValue = new StringBuilder();

            this.Digit = dig;
            this.DecimalPlaces = decimalPlaces;
            this.labelScreen.Text = value;

            if (value[0] == '+')
                value.Remove(0, 1);

            if (value[0] == '-')
                this.IsAddition = false;
            else
                this.IsAddition = true;

            this.buttonSign.Enabled = isSignEnable;

            this.InitializeDesign(language);
        }
        #endregion

        #region Property
        public string StringValue
        {
            get { return this.m_StringValue; }
            private set { this.m_StringValue = value; }
        }

        public double doubleValue
        {
            get
            {
                try
                {
                    return double.Parse(this.StringValue);
                }
                catch
                {
                    return 0.0;
                }
            }
        }

        public int IntValue
        {
            get
            {
                try
                {
                    return int.Parse(this.StringValue);
                }
                catch
                {
                    return 0;
                    throw;
                }
            }
        }
        #endregion

        #region Method
        private void InitializeDesign(DataStore.LanguageID language)
        {
            Class1 images = new Class1();

            if (language == DataStore.LanguageID.English)
            {
                this.buttonCancel.Text = "ESC";
                this.buttonEnter.Text = "Enter";
            }
            else if (language == DataStore.LanguageID.Chinese)
            {
                this.labelTitle.Text = "键盘";

                this.buttonCancel.Text = "取消";
                this.buttonEnter.Text = "确认";
            }
            else if (language == DataStore.LanguageID.Czech)
            {
            }
            else if (language == DataStore.LanguageID.Russian)
            {
                this.labelTitle.Text = "Клавиатура";

                this.buttonCancel.Text = "X";
                this.buttonEnter.Text = "Ввод";
            }
            else if (language == DataStore.LanguageID.German)
            {
                this.buttonCancel.Text = "X";
                this.buttonEnter.Text = "Enter";
            }
            else
            {
                this.buttonCancel.Text = "ESC";
                this.buttonEnter.Text = "Enter";
            }
        }
        private void InitializeContnrol()
        {
            int x = 0, y = 0;

            x = Screen.PrimaryScreen.Bounds.Width / 2 - this.Size.Width / 2;
            y = Screen.PrimaryScreen.Bounds.Height / 2 - this.Size.Height / 2;

            this.Location = new Point(x, y);
        }

        private void InputNumber(string key)
        {
            if (this.IsEditing == false)
                this.IsEditing = true;

            this.sbInputValue.Append(key);
            this.DisplayScreen(this.sbInputValue.ToString());
        }

        private void DisplayScreen(string value)
        {
            int iValue = 0;
            string sValue = "";

            if (this.IsEditing == false || value == null)
                return;

            iValue = int.Parse(value);

            if (this.DecimalPlaces == 0)
            {
                if (iValue.ToString().Length <= this.Digit)
                    sValue = iValue.ToString();
                else
                {
                    this.sbInputValue.Length = 0;
                    sValue = "0";
                }
            }
            else
            {
                if (iValue.ToString().Length <= this.Digit)
                    sValue = Helper.StringToDecimalPlaces(iValue.ToString(), this.DecimalPlaces);
                else
                {
                    this.sbInputValue.Length = 0;
                    sValue = Helper.StringToDecimalPlaces(this.sbInputValue.ToString(), this.DecimalPlaces);
                }
            }

            if (this.IsAddition == true)
                this.labelScreen.Text = string.Format("{0}", sValue);
            else
                this.labelScreen.Text = string.Format("-{0}", sValue);
        }
        #endregion

        #region Override Member
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            this.InitializeContnrol();
        }
        #endregion

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

            if (bt == null)
                return;

            if (bt == this.buttonNumber0)
                this.InputNumber("0");
            else if (bt == this.buttonNumber1)
                this.InputNumber("1");
            else if (bt == this.buttonNumber2)
                this.InputNumber("2");
            else if (bt == this.buttonNumber3)
                this.InputNumber("3");
            else if (bt == this.buttonNumber4)
                this.InputNumber("4");
            else if (bt == this.buttonNumber5)
                this.InputNumber("5");
            else if (bt == this.buttonNumber6)
                this.InputNumber("6");
            else if (bt == this.buttonNumber7)
                this.InputNumber("7");
            else if (bt == this.buttonNumber8)
                this.InputNumber("8");
            else if (bt == this.buttonNumber9)
                this.InputNumber("9");
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void buttonEnter_Click(object sender, EventArgs e)
        {
            this.StringValue = this.labelScreen.Text;

            if (this.doubleValue == 0)
            {
                if (this.DecimalPlaces == 1)
                    this.StringValue = "0.0";
                else if (this.DecimalPlaces == 2)
                    this.StringValue = "0.00";
                else
                    this.StringValue = "0";
            }

            this.DialogResult = DialogResult.OK;
        }

        private void buttonSign_Click(object sender, EventArgs e)
        {
            if (this.IsEditing == false)
            {
                if (this.IsAddition == false)
                {
                    this.IsAddition = true;
                    this.labelScreen.Text = this.labelScreen.Text.Remove(0, 1);
                }
                else
                {
                    this.IsAddition = false;
                    this.labelScreen.Text = this.labelScreen.Text.Insert(0, "-");
                }
            }
            else
            {
                if (this.IsAddition == false)
                    this.IsAddition = true;
                else
                    this.IsAddition = false;

                this.DisplayScreen(this.sbInputValue.ToString());
            }
        }
        #endregion
    }
}