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 INT63DC_2C.DialogForms
{
    public partial class DialogFormPasswordKeyPad : Form
    {
        #region Field
        private string m_InputKey;
        private string m_PassKey;

        private int m_Digit;
        private int m_DecPoint;

        private string m_Level1Password;
        private string m_Level2Password;
        private string m_Level3Password;
        private string m_Level4Password;
        private string m_DefinedPassword;

        private DataStore.UserGroup m_Group;        
        #endregion

        #region Constructor
        public DialogFormPasswordKeyPad(DataStore.E_LanguageID language, UserPasswordType user, int digit)
        {
            InitializeComponent();

            this.Digit = digit;
            this.Level1Password = user.Level1Password;
            this.Level2Password = user.Level2Password;
            this.Level3Password = user.Level3Password;
            this.Level4Password = user.Level4Password;
            this.DefinedPassword = user.DefinedPassword;

            this.InitializeDesign(language);
        }
        #endregion

        #region Property
        public string InputKey
        {
            get { return this.m_InputKey; }
            set { this.m_InputKey = value; }
        }
        public string PassKey
        {
            get { return this.m_PassKey; }
            set { this.m_PassKey = value; }
        }

        public int Digit
        {
            get { return this.m_Digit; }
            set { this.m_Digit = value; }
        }
        public int DecPoint
        {
            get { return this.m_DecPoint; }
            set { this.m_DecPoint = value; }
        }

        public string Level1Password
        {
            get { return this.m_Level1Password; }
            set { this.m_Level1Password = value; }
        }
        public string Level2Password
        {
            get { return this.m_Level2Password; }
            set { this.m_Level2Password = value; }
        }
        public string Level3Password
        {
            get { return this.m_Level3Password; }
            set { this.m_Level3Password = value; }
        }
        public string Level4Password
        {
            get { return this.m_Level4Password; }
            set { this.m_Level4Password = value; }
        }
        public string DefinedPassword
        {
            get { return this.m_DefinedPassword; }
            private set { this.m_DefinedPassword = value; }
        }

        public DataStore.UserGroup Group
        {
            get { return this.m_Group; }
            set { this.m_Group = value; }
        }
        #endregion

        #region Method
        private void InputNum(string key)
        {
            if (this.InputKey.Length >= this.Digit)
            {
                this.InputKey = key;
                this.labelScreen.Text = "*";
            }
            else
            {
                this.InputKey += key;
                this.labelScreen.Text += "*";
            }
        }

        private void InitializeDesign(DataStore.E_LanguageID language)
        {
            switch (language)
            {
                case DataStore.E_LanguageID.Korean:
                case DataStore.E_LanguageID.English:
                    this.labelSubject.Text = "Password";

                    this.buttonCancel.Text = "ESC";
                    this.buttonEnter.Text = "Enter";
                    break;
                case DataStore.E_LanguageID.Chinese:
                    this.labelSubject.Text = "密码";

                    this.buttonCancel.Text = "取消";
                    this.buttonEnter.Text = "确认";
                    break;
                case DataStore.E_LanguageID.Czech:
                    this.labelSubject.Text = "Klávesnice";

                    this.buttonCancel.Text = "Zrušit";
                    this.buttonEnter.Text = "Enter";
                    break;
                case DataStore.E_LanguageID.German:
                    this.labelSubject.Text = "Passwort";

                    this.buttonCancel.Text = "X";
                    this.buttonEnter.Text = "Enter";
                    break;
                case DataStore.E_LanguageID.Japanese:
                    this.labelSubject.Text = "パスワㅡド";

                    this.buttonCancel.Text = "X";
                    this.buttonEnter.Text = "入力";
                    break;
                default:
                    break;
            }
        }
        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(251, 390);

            this.InputKey = "";
            this.labelScreen.Text = "";
        }
        #endregion

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

            this.InitializeContnrol();
        }
        #endregion

        #region Event Handler
        private void buttonEnter_Click(object sender, EventArgs e)
        {
            if (this.Digit == 4)
            {
                if (this.labelScreen.Text.Length == 4)
                {
                    if (this.InputKey == this.Level4Password)
                    {
                        this.DialogResult = DialogResult.OK;
                        this.Group = DataStore.UserGroup.Developer;
                    }
                    else if (this.InputKey == this.Level3Password)
                    {
                        this.DialogResult = DialogResult.OK;
                        this.Group = DataStore.UserGroup.Level3;
                    }
                    else if (this.InputKey == this.Level2Password)
                    {
                        this.DialogResult = DialogResult.OK;
                        this.Group = DataStore.UserGroup.Level2;
                    }
                    else if (this.InputKey == this.Level1Password)
                    {
                        this.DialogResult = DialogResult.OK;
                        this.Group = DataStore.UserGroup.Level1;
                    }
                }
            }
            else
            {
                if (this.labelScreen.Text.Length == 8)
                {
                    if (this.InputKey == this.DefinedPassword)
                        this.DialogResult = DialogResult.OK;
                }
            }

            this.InputKey = "";
            this.labelScreen.Text = "";
        }

        private void buttonNumber_Click(object sender, EventArgs e)
        {
            Button bt = sender as Button;

            if (bt == null)
                return;

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

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Group = DataStore.UserGroup.LogOut;
            this.Close();
        }
        #endregion
    }
}