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

namespace Corp3s.Controls.Input
{
    public partial class KeypadControl : UserControl
    {
        private EventHandler m_eEntered = null;
        private EventHandler m_eCanceled = null;

        private bool m_bCaps = false;

        public KeypadControl()
        {
            InitializeComponent();
            SetCaps(false);
        }        

        public event EventHandler OnEntered
        {
            add { m_eEntered += value; }
            remove { m_eEntered -= value; }
        }

        public event EventHandler OnCanceled
        {
            add { m_eCanceled += value; }
            remove { m_eCanceled -= value; }
        }

        public String EnteredText
        {
            get { return txtNumeric.Text; }
        }

        private void SetCaps(bool bCaps)
        {
#if !DESIGN         
            m_bCaps = bCaps;
            if (bCaps)
            {
                foreach (ImageButtom btn in pnlLetters.Controls)
                {
                    String caption = btn.Tag.ToString().ToUpper();
                    btn.Tag = caption;
                    btn.Text = caption;
                }
            }
            else
            {
                foreach (ImageButtom btn in pnlLetters.Controls)
                {
                    String caption = btn.Tag.ToString().ToLower();
                    btn.Tag = caption;
                    btn.Text = caption;
                }
            }
#endif
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            txtNumeric.Text = "";
        }

        private void btnBackspace_Click(object sender, EventArgs e)
        {
            if (txtNumeric.Text.Length > 0)
                txtNumeric.Text = txtNumeric.Text.Remove(txtNumeric.Text.Length - 1, 1);
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Close();
            if (m_eCanceled != null)
                m_eCanceled(sender, e);
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            Close();
            if (m_eEntered != null)
                m_eEntered(sender, e);
        }

        private void btnSpaceClicked(object sender, EventArgs e)
        {
            txtNumeric.Text += " ";
        }

        private void Close()
        {
            Form form = TopLevelControl as Form;
            if (form != null)
                form.Controls.Remove(this);                            
        }

        private void OnCharacterClicked(object sender, EventArgs e)
        {
            ImageButtom btn = sender as ImageButtom;
            if (btn != null)
                txtNumeric.Text += btn.Text;
        }

        private void btnCaps_Click(object sender, EventArgs e)
        {
            SetCaps(!m_bCaps);
        }

        public void Show(Form parent, String text)
        {
            txtNumeric.Text = text;
            if (parent == null)
                return;

            parent.Controls.Add(this);

            int x = (parent.Width - this.Width) / 2;
            int y = (parent.Height - this.Height) - 2;
            if (Environment.OSVersion.Platform == PlatformID.Win32NT)
            {
                x = 0;
                y = 0;
            }
            this.Location = new System.Drawing.Point(x, y);
            this.Visible = true;
            this.BringToFront();
        }

        public void Show(Form parent)
        {
            Show(parent, "");
        }
    }
}
