//------------------------------------------------------------------------------
// <copyright file=”*.cs” company=”EhLib Team”>
//     Copyright (c) 2017 Dmitry V. Bolshakov   
// </copyright>
//------------------------------------------------------------------------------

using System.Drawing;
using System.Windows.Forms;
using System.ComponentModel;

namespace EhLib
{

  [DesignerCategory("Code")]
  [DataAxisGridDataCellDesignTimeVisibleAttribute(false)]
  public abstract class DataAxisGridButtonBaseCell : DataAxisGridDataCellMan
  {

    #region privates
    internal bool DownState;
    #endregion privates

    #region Properties
    #endregion Properties

    #region Methods
    public virtual void SetDownState(int dataColIndex, int dataRowIndex, bool downState)
    {
      if (this.DownState != downState)
      {
        int gridColIndex;
        int gridRowIndex;

        this.DownState = downState;

        AreaCellPosToGridCellPos(dataColIndex, dataRowIndex, out gridColIndex, out gridRowIndex);
        Grid.InvalidateCell(gridColIndex, gridRowIndex);
      }
    }

    public virtual void Toggle(int dataColIndex, int dataRowIndex)
    {
    }

    protected internal override void OnKeyDown(KeyEventArgs e)
    {
      switch (e.KeyData & Keys.KeyCode)
      {
        case Keys.Space:
          {
            SetDownState(Grid.CurrentDataColIndex, Grid.CurrentDataRowIndex, true);
            e.Handled = true;
            return;
          }
      }
    }

    protected internal override void OnKeyUp(KeyEventArgs e)
    {
      switch (e.KeyData & Keys.KeyCode)
      {
        case Keys.Space:
          {
            SetDownState(Grid.CurrentDataColIndex, Grid.CurrentDataRowIndex, false);
            e.Handled = true;

            PropertyAxisBar propAxisBar;
            object listItemBar;
            AxisObjectsByDataColRowIndex(Grid.CurrentDataColIndex, Grid.CurrentDataRowIndex, out propAxisBar, out listItemBar);

            var evArg = new DataAxisGridDataCellEventArgs(Grid.Col, Grid.Row, 
              Grid.CurrentDataColIndex, Grid.CurrentDataRowIndex, Rectangle.Empty, propAxisBar, listItemBar, this);
            Grid.OnDataCellClick(evArg);
            if (!evArg.Processed)
              Toggle(Grid.CurrentDataColIndex, Grid.CurrentDataRowIndex);
            return;
          }
      }
    }

    protected internal override void OnMouseDown(BaseGridCellMouseEventArgs e)
    {
      base.OnMouseDown(e);
      DownState = true;
    }

    protected internal override void OnMouseUp(BaseGridCellMouseEventArgs e)
    {
      int dataColIndex;
      int dataRowIndex;

      GridCellPosToAreaCellPos(e.ColIndex, e.RowIndex, out dataColIndex, out dataRowIndex);
      if ((e.InCellX >= 0) &&
          (e.InCellY >= 0) &&
          (e.InCellX < e.CellRect.Width) &&
          (e.InCellY < e.CellRect.Height) && 
          !ReadOnly)
      {
        Toggle(dataColIndex, dataRowIndex);
      }
      base.OnMouseUp(e);
      SetDownState(dataColIndex, dataRowIndex, false);
    }

    protected internal override void OnMouseEnter(BaseGridCellEnterEventArgs e)
    {
      Grid.InvalidateCell(e.ColIndex, e.RowIndex);
      base.OnMouseEnter(e);
    }

    protected internal override void OnMouseLeave(BaseGridCellLeaveEventArgs e)
    {
      Grid.InvalidateCell(e.ColIndex, e.RowIndex);
      base.OnMouseLeave(e);
    }

    #endregion Methods

  }

}
