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;

namespace INT69DC_7C.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] == '-')
                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
            {
                double dValue = 0.0;

                try
                {
                    dValue = double.Parse(this.StringValue);
                }
                catch
                {
                    dValue = -1;
                }

                return dValue;
            }
        }

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

        #region Method
        private void InitializeDesign(DataStore.LanguageID language)
        {
            if (language == DataStore.LanguageID.Chinese)
            {
                this.label1.Text = "键盘";

                this.buttonCancel.Text = "取消";
                this.buttonEnter.Text = "确认";
            }
            else if (language == DataStore.LanguageID.Czech)
            {
                this.label1.Text = "Klávesnice";

                this.buttonCancel.Text = "Zrušit";
                this.buttonEnter.Text = "Enter";
            }
            else if (language == DataStore.LanguageID.German)
            {
                this.label1.Text = "Tastatur";

                this.buttonCancel.Text = "X";
                this.buttonEnter.Text = "Enter";
            }
            else if (language == DataStore.LanguageID.Japanese)
            {
                this.label1.Text = "キㅡパッド";

                this.buttonCancel.Text = "X";
                this.buttonEnter.Text = "入力";
            }
            else
            {

            }
        }
        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);

            this.Size = new Size(250, 443);
        }

        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)
        {
            Button bt = sender as Button;

            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
    }
}