using System;
using System.ComponentModel;
using System.Data.SqlTypes;
using System.Drawing;
using System.Windows.Forms;
using DevComponents.DotNetBar.SuperGrid.Style;
using DevComponents.DotNetBar.Controls;
namespace DevComponents.DotNetBar.SuperGrid
{
    ///
    /// GridSwitchButtonEditControl
    ///
    [ToolboxItem(false)]
    public class GridSwitchButtonEditControl : SwitchButton, IGridCellEditControl
    {
        #region Private variables
        private GridCell _Cell;
        private EditorPanel _EditorPanel;
        private Bitmap _EditorCellBitmap;
        private bool _ValueChanged;
        private bool _SuspendUpdate;
        private object _OnValue;
        private object _OffValue;
        private StretchBehavior _StretchBehavior = StretchBehavior.Both;
        #endregion
        ///
        /// GridSwitchButtonEditControl
        ///
        public GridSwitchButtonEditControl()
        {
            FocusCuesEnabled = false;
            Style = eDotNetBarStyle.StyleManagerControlled;
        }
        #region Public properties
        #region OnValue
        ///
        /// OnValue
        ///
        public object OnValue
        {
            get { return (_OnValue); }
            set { _OnValue = value; }
        }
        #endregion
        #region OffValue
        ///
        /// OffValue
        ///
        public object OffValue
        {
            get { return (_OffValue); }
            set { _OffValue = value; }
        }
        #endregion
        #endregion
        #region OnValueChanged
        /// 
        /// OnValueChanged
        /// 
        /// 
        protected override void OnValueChanged(EventArgs e)
        {
            if (_SuspendUpdate == false)
            {
                _Cell.UpdateCellValue(this);
                _Cell.EditorValueChanged(this);
            }
            base.OnValueChanged(e);
        }
        #endregion
        #region OnInvalidated
        /// 
        /// OnInvalidated
        /// 
        /// 
        protected override void OnInvalidated(InvalidateEventArgs e)
        {
            base.OnInvalidated(e);
            if (_Cell != null && _SuspendUpdate == false)
                _Cell.InvalidateRender();
        }
        #endregion
        #region GetValue
        ///
        /// GetValue
        ///
        ///
        ///
        ///
        public virtual bool GetValue(object value)
        {
            GridPanel panel = _Cell.GridPanel;
            if (value == null ||
                (panel.NullValue == NullValue.DBNull && value == DBNull.Value))
            {
                return (false);
            }
            if (_Cell.IsValueExpression == true)
                value = _Cell.GetExpValue((string)value);
            Type type = value.GetType();
            if (_OnValue != null)
            {
                if (value.Equals(_OnValue))
                    return (true);
            }
            if (_OffValue != null)
            {
                if (value.Equals(_OffValue))
                    return (false);
            }
            if (type == typeof(bool))
                return ((bool)value);
            if (type == typeof(string))
            {
                string s = ((string)value).ToLower();
                if (s.Equals("true") || s.Equals("yes") || s.Equals("1"))
                    return (true);
                if (s.Equals("false") || s.Equals("no") || s.Equals("0"))
                    return (false);
            }
            else if (type == typeof(SqlBoolean))
            {
                SqlBoolean sb = (SqlBoolean)value;
                if (sb.IsNull == true)
                    return (false);
                return (sb.IsTrue);
            }
            else if (type == typeof(char))
            {
                char c = (char)value;
                switch (Char.ToLower(c))
                {
                    case 't':
                    case 'y':
                    case '1':
                        return (true);
                    case 'f':
                    case 'n':
                    case '0':
                        return (false);
                }
            }
            else
            {
                int n = Convert.ToInt32(value);
                return (n == 0 ? false : true);
            }
            throw new Exception("Invalid checkBox cell value (" + value + ").");
        }
        #endregion
        #region IGridCellEditControl Members
        #region Public properties
        #region CanInterrupt
        /// 
        /// CanInterrupt
        /// 
        public bool CanInterrupt
        {
            get { return (true); }
        }
        #endregion
        #region CellEditMode
        /// 
        /// CellEditMode
        /// 
        public CellEditMode CellEditMode
        {
            get { return (CellEditMode.InPlace); }
        }
        #endregion
        #region EditorCell
        /// 
        /// EditorCell
        /// 
        public GridCell EditorCell
        {
            get { return (_Cell); }
            set { _Cell = value; }
        }
        #endregion
        #region EditorCellBitmap
        ///
        /// EditorCellBitmap
        ///
        public Bitmap EditorCellBitmap
        {
            get { return (_EditorCellBitmap); }
            set
            {
                if (_EditorCellBitmap != null)
                    _EditorCellBitmap.Dispose();
                _EditorCellBitmap = value;
            }
        }
        #endregion
        #region EditorFormattedValue
        ///
        /// EditorFormattedValue
        ///
        public virtual string EditorFormattedValue
        {
            get
            {
                if (_Cell != null)
                {
                    if (_Cell.Value == null || _Cell.IsValueNull == true)
                        return (_Cell.NullString);
                    return (_Cell.Value.ToString());
                }
                return (Text);
            }
        }
        #endregion
        #region EditorPanel
        /// 
        /// EditorPanel
        /// 
        public EditorPanel EditorPanel
        {
            get { return (_EditorPanel); }
            set { _EditorPanel = value; }
        }
        #endregion
        #region EditorValue
        /// 
        /// EditorValue
        /// 
        public virtual object EditorValue
        {
            get { return (Value); }
            set { Value = GetValue(value); }
        }
        #endregion
        #region EditorValueChanged
        /// 
        /// EditorValueChanged
        /// 
        public virtual bool EditorValueChanged
        {
            get { return (_ValueChanged); }
            set
            {
                if (_ValueChanged != value)
                {
                    _ValueChanged = value;
                    if (value == true)
                        _Cell.SetEditorDirty(this);
                }
            }
        }
        #endregion
        #region EditorValueType
        ///
        /// EditorValueType
        ///
        public virtual Type EditorValueType
        {
            get { return (typeof(bool)); }
        }
        #endregion
        #region StretchBehavior
        /// 
        /// StretchBehavior
        /// 
        public virtual StretchBehavior StretchBehavior
        {
            get { return (_StretchBehavior); }
            set { _StretchBehavior = value; }
        }
        #endregion
        #region SuspendUpdate
        /// 
        /// SuspendUpdate
        /// 
        public bool SuspendUpdate
        {
            get { return (_SuspendUpdate); }
            set { _SuspendUpdate = value; }
        }
        #endregion
        #region ValueChangeBehavior
        /// 
        /// ValueChangeBehavior
        /// 
        public virtual ValueChangeBehavior ValueChangeBehavior
        {
            get { return (ValueChangeBehavior.InvalidateRender); }
        }
        #endregion
        #endregion
        #region InitializeContext
        ///
        /// InitializeContext
        ///
        ///
        ///
        public virtual void InitializeContext(GridCell cell, CellVisualStyle style)
        {
            _Cell = cell;
            if (style != null)
            {
                Enabled = (_Cell.IsReadOnly == false);
                Font = style.Font;
                ForeColor = style.TextColor;
            }
            Value = GetValue(_Cell.Value);
            _ValueChanged = false;
        }
        #endregion
        #region GetProposedSize
        ///
        /// GetProposedSize
        ///
        ///
        ///
        ///
        ///
        ///
        public virtual Size GetProposedSize(Graphics g,
            GridCell cell, CellVisualStyle style, Size constraintSize)
        {
            Size size = GetPreferredSize(constraintSize);
            size.Width += Dpi.Width4;
            return (size);
        }
        #endregion
        #region Edit support
        #region BeginEdit
        /// 
        /// BeginEdit
        /// 
        /// 
        /// 
        public virtual bool BeginEdit(bool selectAll)
        {
            return (false);
        }
        #endregion
        #region EndEdit
        /// 
        /// EndEdit
        /// 
        /// 
        public virtual bool EndEdit()
        {
            return (false);
        }
        #endregion
        #region CancelEdit
        /// 
        /// CancelEdit
        /// 
        /// 
        public virtual bool CancelEdit()
        {
            return (false);
        }
        #endregion
        #endregion
        #region CellRender
        /// 
        /// CellRender
        /// 
        /// 
        public virtual void CellRender(Graphics g)
        {
            _Cell.CellRender(this, g);
        }
        #endregion
        #region Keyboard support
        #region CellKeyDown
        ///
        /// CellKeyDown
        ///
        public virtual void CellKeyDown(KeyEventArgs e)
        {
            OnKeyDown(e);
        }
        #endregion
        #region WantsInputKey
        /// 
        /// WantsInputKey
        /// 
        /// 
        /// 
        /// 
        public virtual bool WantsInputKey(Keys key, bool gridWantsKey)
        {
            switch (key & Keys.KeyCode)
            {
                case Keys.Space:
                    return (true);
                default:
                    return (gridWantsKey == false);
            }
        }
        #endregion
        #endregion
        #region Mouse support
        #region OnCellMouseMove
        ///
        /// OnCellMouseMove
        ///
        ///
        public virtual void OnCellMouseMove(MouseEventArgs e)
        {
            OnMouseMove(e);
        }
        #endregion
        #region OnCellMouseEnter
        ///
        /// OnCellMouseEnter
        ///
        ///
        public virtual void OnCellMouseEnter(EventArgs e)
        {
            OnMouseEnter(e);
        }
        #endregion
        #region OnCellMouseLeave
        ///
        /// OnCellMouseLeave
        ///
        ///
        public virtual void OnCellMouseLeave(EventArgs e)
        {
            OnMouseLeave(e);
            CancelAnimation();
        }
        #endregion
        #region OnCellMouseUp
        ///
        /// OnCellMouseUp
        ///
        ///
        public virtual void OnCellMouseUp(MouseEventArgs e)
        {
            OnMouseUp(e);
        }
        #endregion
        #region OnCellMouseDown
        ///
        /// OnCellMouseDown
        ///
        ///
        public virtual void OnCellMouseDown(MouseEventArgs e)
        {
            OnMouseDown(e);
        }
        #endregion
        #endregion
        #endregion
    }
}