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

using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Design;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
using System.Windows.Forms.VisualStyles;

namespace EhLib
{
  [ToolboxItem(false)]
  public class DataGridBaseCellMan : BaseGridCellManager
  {
    #region privates
    #endregion privates

    public DataGridBaseCellMan()
    {

    }

    #region properties
    [Browsable(false)]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
    public new DataGridEh Grid
    {
      get { return (DataGridEh)base.Grid; }
      set { base.Grid = value; }
    }
    #endregion

    #region methods
    protected internal override void OnPaintBackground(BaseGridCellPaintEventArgs e)
    {
      using (
        SolidBrush fillBrush = new SolidBrush(Grid.BackColor))
      {
        if ((BasePaintCellStates.Selected & e.State) != 0)
        {
          if (Grid.IsActiveControl())
            fillBrush.Color = SystemColors.Highlight;
          else
            fillBrush.Color = SystemColors.ButtonShadow;
        }

        Grid.PaintingFillRectangle(e.Graphics, fillBrush, e.CellClientRect);
      }
    }

    public override string GetDisplayText(int areaColIndex, int areaRowIndex)
    {
      return "GetDisplayText is not implemented";
    }

    protected internal override void OnGetCellBorderParams(BaseGridCellBorderEventArgs e)
    {
      e.Style = DashStyle.Solid;

      if ((e.BorderType == GridCellBorderType.Left) || (e.BorderType == GridCellBorderType.Right))
      {
        e.Visible = Grid.LineOptions.VertLines;
        e.IsExtent = true;
      }
      else
      {
        e.Visible = Grid.LineOptions.HorzLines;
        e.IsExtent = true;
      }

      //e.Color = SystemColors.ButtonFace;
      //return;

      if ((e.BorderType == GridCellBorderType.Top || e.BorderType == GridCellBorderType.Left) &&
         ((e.ColIndex == Grid.ColCount) || (e.RowIndex == Grid.RowCount)))
        e.Color = Grid.LineOptions.DarkColor;
      else if (e.ColIndex < 0 || e.RowIndex < 0)
        e.Color = Grid.LineOptions.BrightColor;
      else if ((e.ColIndex < Grid.FixedColCount - Grid.FrozenColCount) ||
               (e.RowIndex < Grid.FixedRowCount - Grid.FrozenRowCount))
        e.Color = Grid.LineOptions.DarkColor;
      else if ((e.ColIndex == Grid.FixedColCount - 1) && (e.BorderType == GridCellBorderType.Right))
        e.Color = Grid.LineOptions.DarkColor;
      else if ((e.ColIndex == Grid.FixedRowCount - 1) && (e.BorderType == GridCellBorderType.Bottom))
        e.Color = Grid.LineOptions.DarkColor;
      else if ((e.RowIndex > Grid.RowCount - 1) && (e.BorderType == GridCellBorderType.Bottom))
      {
        e.Color = Grid.Footer.HorzLine.Color;
        e.Style = Grid.Footer.HorzLine.Style;
      }
      else
      {
        if (e.BorderType == GridCellBorderType.Left ||
            e.BorderType == GridCellBorderType.Right
            )
        {
          e.Visible = Grid.ColumnOptions.VertLine.Visible;
          e.IsExtent = true;
          e.Color = Grid.ColumnOptions.VertLine.Color;
          e.Style = Grid.ColumnOptions.VertLine.Style;
        }
        else
        {
          e.Visible = Grid.RowOptions.HorzLine.Visible;
          e.IsExtent = true;
          e.Color = Grid.RowOptions.HorzLine.Color;
          e.Style = Grid.RowOptions.HorzLine.Style;
        }
      }
    }

    protected internal override void OnContextMenuStripNeeded(BaseGridCellContextMenuStripNeededEventArgs e)
    {
      e.ContextMenuStrip = Grid.GetDefaultContextMenuStrip();
    }
    #endregion
  }

  public class DataGridEmptyTitleCellMan : DataGridBaseCellMan
  {
    public override string GetDisplayText(int areaColIndex, int areaRowIndex)
    {
      return null;
    }
  }

  public class DataGridEmptyDataCellMan : DataGridBaseCellMan
  {
    public override string GetDisplayText(int areaColIndex, int areaRowIndex)
    {
      return null;
    }

    protected internal override void OnPaintBackground(BaseGridCellPaintEventArgs e)
    {
      using (
        SolidBrush fillBrush = new SolidBrush(Grid.BackColor))
      {
        if ((BasePaintCellStates.Selected & e.State) != 0)
        {
          if (Grid.IsActiveControl())
            fillBrush.Color = SystemColors.Highlight;
          else
            fillBrush.Color = SystemColors.ButtonShadow;
        }

        Grid.PaintingFillRectangle(e.Graphics, fillBrush, e.CellClientRect);
      }
    }

    protected internal override void OnPaintEmptyArea(BaseGridCellPaintEventArgs e)
    {
      Color color;
      Color backColor = Grid.BackColor;
      int alpha;

      if (e.ColIndex == -1 &&
          e.RowIndex >= 0 &&
          Grid.VisibleColumns.Count > 0 &&
          Grid.VisibleRows.Count > 0 &&
          e.AreaRowIndex < Grid.VisibleRows.Count)
      {
        DataGridColumn col = Grid.VisibleColumns[Grid.VisibleColumns.Count - 1];
        DataGridRow row = Grid.VisibleRows[e.AreaRowIndex];
        //BaseGridCellManager cellMane = ()col.GetRowCell(row);

        DataAxisGridDataCellFormatParamsEventArgs formatParams = col.CreateDataCellFormatParamsEventArgs(
          e.ColIndex, e.RowIndex, e.AreaColIndex, e.AreaColIndex,
          e.CellRect, null, row, null, DataAxisGridDataCellFormatApplying.DisplayValue);

        formatParams.HorzAlign = HorizontalAlignment.Left;
        formatParams.VertAlign = Grid.ColumnOptions.VertAlign;
        formatParams.Font = Grid.ColumnOptions.Font;
        formatParams.BackColor = backColor;
        formatParams.ForeColor = Grid.ColumnOptions.ForeColor;
        formatParams.Padding = Padding.Empty;

        if (Grid != null)
          Grid.OnFillDataCellFormatParams(formatParams);

        backColor = formatParams.BackColor;
      }  

      if (!Grid.Background.Visible)
      {
        using (SolidBrush fillBrush = new SolidBrush(backColor))
        {
          Grid.PaintingFillRectangle(e.Graphics, fillBrush, e.CellRect);
        }
      }

      if ((BasePaintCellStates.CurrentRow & e.State) != 0)
      {
        alpha = 255 / 6 / 2;
        if (Grid.IsActiveControl())
          color = SystemColors.Highlight;
        else
          color = SystemColors.ButtonShadow;
        using (SolidBrush fillBrush = new SolidBrush(Color.FromArgb(alpha, color)))
        {
          Grid.PaintingFillRectangle(e.Graphics, fillBrush, e.CellRect);
        }
      }
    }

    protected internal override void OnGetCellBorderParams(BaseGridCellBorderEventArgs e)
    {
      base.OnGetCellBorderParams(e);

      if (e.BorderType == GridCellBorderType.Right)
      {
        GridLine gl = Grid.ColumnOptions.VertLine;
        e.Visible = gl.Visible;
        e.Color = gl.Color;
        e.Style = gl.Style;
        e.IsExtent = true;
      }
      else if (e.BorderType == GridCellBorderType.Bottom)
      {
        DataGridRow row = null;

        e.Visible = Grid.RowOptions.HorzLine.Visible;
        e.IsExtent = true;
        e.Color = Grid.RowOptions.HorzLine.Color;
        e.Style = Grid.RowOptions.HorzLine.Style;

        if (e.AreaRowIndex >= 0 && e.AreaRowIndex < Grid.VisibleRows.Count)
          row = Grid.VisibleRows[e.AreaRowIndex];

        var hlpe = new DataGridHorzLineParamsNeededEventArgs(null, row);
        hlpe.Visible = e.Visible;
        hlpe.Color = e.Color;
        hlpe.Style = e.Style;

        Grid.OnHorzLineParamsNeeded(hlpe);

        e.Visible = hlpe.Visible;
        e.Color = hlpe.Color;
        e.Style = hlpe.Style;
      }

      if (e.Visible && Grid.Background.Visible)
        e.Color = Color.FromArgb(Grid.Background.GridOpacityOptions.LinesOpacity, e.Color);
    }

    public override void GridCellPosToAreaCellPos(int gridColIndex, int gridRowIndex, out int areaColIndex, out int areaRowIndex)
    {
      areaColIndex = gridColIndex - Grid.StartDataColIndex;
      areaRowIndex = gridRowIndex - Grid.FixedRowCount;
    }

    public override void AreaCellPosToGridCellPos(int areaColIndex, int areaRowIndex, out int gridColIndex, out int gridRowIndex)
    {
      gridColIndex = areaColIndex + Grid.StartDataColIndex;
      gridRowIndex = areaRowIndex + Grid.FixedRowCount;
    }

  }

  public class DataGridBaseIndicatorCellMan : DataGridBaseCellMan
  {
    protected BaseGridFillFixedCellEventArgs StyledPaintArgs = new BaseGridFillFixedCellEventArgs();

    protected internal override void OnGetCellBorderParams(BaseGridCellBorderEventArgs e)
    {
      if ((e.BorderType == GridCellBorderType.Left) || (e.BorderType == GridCellBorderType.Right))
      {
        e.Visible = Grid.IndicatorColumn.VertLine.Visible;
        e.IsExtent = true;
      }
      else
      {
        e.Visible = Grid.IndicatorColumn.HorzLine.Visible;
        e.IsExtent = true;
      }

      if (e.BorderType == GridCellBorderType.Top || e.BorderType == GridCellBorderType.Bottom)
      {
        e.Color = Grid.IndicatorColumn.HorzLine.Color;
        e.Style = Grid.IndicatorColumn.HorzLine.Style;
      }
      else
      {
        e.Color = Grid.IndicatorColumn.VertLine.Color;
        e.Style = Grid.IndicatorColumn.VertLine.Style;
      }
    }

    protected internal override bool IsSelected(int areaColIndex, int areaRowIndex)
    {
      if (Grid.Selection.SelectionType == GridSelectionType.All)
        return true;
      else
        return false;
    }

    protected internal virtual bool IsPressed(int col, int row,
        Rectangle paintRect,
        BasePaintCellStates state,
        int dataColIndex, int dataRowIndex)
    {
      return false;
    }

    protected internal override void OnPaintBackground(BaseGridCellPaintEventArgs e)
    {

      Color backColor = Grid.FixedBackColor;

      StyledPaintArgs.CellRect = e.CellClientRect;
      StyledPaintArgs.Graphics = e.Graphics;
      StyledPaintArgs.IsHot = false;
      StyledPaintArgs.IsPressed = IsPressed(e.ColIndex, e.RowIndex, e.CellClientRect, e.State, e.AreaColIndex, e.AreaRowIndex);
      StyledPaintArgs.IsSelected = (e.State & BasePaintCellStates.Selected) != 0;
      StyledPaintArgs.BackColor = backColor;
      StyledPaintArgs.FillStyle = Grid.FixedBackFiller.FillStyle;
      StyledPaintArgs.InnerBorder = Grid.FixedBackFiller.InnerBorder;
      StyledPaintArgs.FillColor = Grid.FixedBackFiller.Color;
      StyledPaintArgs.SecondFillColor = Grid.FixedBackFiller.SecondColor;

      Grid.GetDrawStyle().FillTitleCell(Grid, StyledPaintArgs);
    }
  }

}
