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 NumericKeypadControl : UserControl
    {
        private EventHandler m_eEnteredEvent;
        private EventHandler m_eCanceledEvent;

        public NumericKeypadControl()
        {
            InitializeComponent();
        }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

        private void btnSign_Click(object sender, EventArgs e)
        {
            if (txtNumeric.Text.IndexOf('-') != -1)
                txtNumeric.Text = txtNumeric.Text.Remove(0, 1);
            else
                txtNumeric.Text = txtNumeric.Text.Insert(0, "-");
        }

        private void btnPoint_Click(object sender, EventArgs e)
        {
            if (txtNumeric.Text.IndexOf('.') == -1)
                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 btnOk_Click(object sender, EventArgs e)
        {
            Close();

            if (m_eEnteredEvent != null)
                m_eEnteredEvent(sender, e);
        }

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

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

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

            Form parentForm = parent.TopLevelControl as Form;
            parentForm.Controls.Add(this);

            int x = (parentForm.Width - this.Width) / 2;
            int y = (parentForm.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(Control parent)
        {
            Show(parent, "");            
        }
    }
}
