using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace Corp3s.Controls
{
    public class NumericTextBoxControl : TextBox
    {
        private EventHandler m_eNumericEntered = null;
        private EventHandler m_eNumericCanceled = null;

        private static Input.NumericKeypadControl m_NumericKeypad = null;

        private String m_previous = "";

        public NumericTextBoxControl()
            : base()
        {
#if !DESIGN
            if (Environment.OSVersion.Platform != PlatformID.WinCE)
                return;

            if (m_NumericKeypad == null)
                m_NumericKeypad = new Corp3s.Controls.Input.NumericKeypadControl();
#endif
        }        

        protected override void OnTextChanged(EventArgs e)
        {
            try
            {                
                if (this.Text == String.Empty)
                    return;

                float.Parse(this.Text);
                m_previous = this.Text;
            }
            catch
            {
                this.Text = m_previous;
            }

            base.OnTextChanged(e);
        }        

        protected override void OnGotFocus(EventArgs e)
        {
            base.OnGotFocus(e);
            if (Environment.OSVersion.Platform != PlatformID.WinCE)
                return;

            ShowKeypad(false);
        }

        private void HideKeypad()
        {
            if (m_NumericKeypad == null)
                return;

            m_NumericKeypad.OnEntered -= new EventHandler(OnNumericEnteredHandler);
            m_NumericKeypad.OnCanceled -= new EventHandler(OnNumericCanceledHandler);

            Form form = TopLevelControl as Form;
            form.Controls.Remove(m_NumericKeypad);     
        }

        public void ShowKeypad(bool bForce)
        {
            if (m_NumericKeypad == null)
            {
                if (bForce == false)                                   
                    return;

                m_NumericKeypad = new Corp3s.Controls.Input.NumericKeypadControl();
            }

            m_NumericKeypad.OnEntered += new EventHandler(OnNumericEnteredHandler);
            m_NumericKeypad.OnCanceled += new EventHandler(OnNumericCanceledHandler);

            m_NumericKeypad.Show(this, this.Text);
        }

        void OnNumericCanceledHandler(object sender, EventArgs e)
        {
            HideKeypad();
            if (Environment.OSVersion.Platform != PlatformID.WinCE)
                return;

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

        void OnNumericEnteredHandler(object sender, EventArgs e)
        {            
            if (m_NumericKeypad == null)
                return;

            HideKeypad();

            this.Text = m_NumericKeypad.TextEntered;
            if (m_eNumericEntered != null)
                m_eNumericEntered(sender, e);
        }

        public event EventHandler OnNumericEntered
        {
            add { m_eNumericEntered += value; }
            remove { m_eNumericEntered -= value; }
        }

        public event EventHandler OnNumericCanceled
        {
            add { m_eNumericCanceled += value; }
            remove { m_eNumericCanceled -= value; }
        }
    }
}
