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

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

namespace EhLib
{

  [TypeConverter(typeof(ExpandableObjectConverter))]
  [ToolboxItem(false)]
  [DesignerCategory("Code")]
  public class DataVertGridIndicatorRow : Component
  {

    #region private consts
    #endregion private consts

    #region privates
    private Font font;
    private Font defaultFont;
    private bool visible = false;
    private bool showRecNo = false;
    private DataVertGridEh grid;
    #endregion

    public DataVertGridIndicatorRow(DataVertGridEh grid)
    {
      this.grid = grid;
    }

    #region design-time properties
    [DefaultValue(false)]
    public bool Visible
    {
      get
      {
        return visible;
      }

      set
      {
        if (visible != value)
        {
          visible = value;
          Grid.UpdateRowsList();
        }
      }
    }

    [DefaultValue(false)]
    public bool ShowRecNo
    {
      get
      {
        return showRecNo;
      }

      set
      {
        if (showRecNo != value)
        {
          showRecNo = value;
          Grid.UpdateRowsList();
        }
      }
    }

    //[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
    public Font Font
    {
      get
      {
        if (font != null)
          return font;
        else
          return DefaultFont();
      }

      set
      {
        font = value;
        FontChanged();
      }
    }
    #endregion

    #region design-time properties
    [Browsable(false)]
    public DataVertGridEh Grid
    {
      get { return grid; }
    }
    #endregion

    #region methods

    //Font
    protected virtual Font DefaultFont()
    {
      if (defaultFont == null)
      {
        defaultFont = new Font(Grid.Font.Name,
                               Grid.Font.Size / 100 * 80,
                               FontStyle.Regular,
                               Grid.Font.Unit,
                               Grid.Font.GdiCharSet);
      }
      return defaultFont;
    }

    protected virtual bool ShouldSerializeFont()
    {
      return (!ReferenceEquals(Font, defaultFont));
    }

    public virtual void ResetFont()
    {
      font = null;
      FontChanged();
    }

    protected virtual void FontChanged()
    {
      if (Grid != null)
        Grid.UpdateBaseFixedBands();
    }

    public virtual void UpdateDefaultFont()
    {
      defaultFont = new Font(Grid.Font.Name,
                             Grid.Font.Size / 100 * 80,
                             FontStyle.Regular,
                             Grid.Font.Unit,
                             Grid.Font.GdiCharSet);
      ResetFont();
    }

    //Other
    #endregion
  }

  public class DataVertGridIndicatorDataCellMan : DataVertGridBaseIndicatorCellMan
  {
    //Painting
   protected internal override void OnPaintForeground(BaseGridCellPaintEventArgs e)
    {
      int widthReducer = 0;
      Rectangle sectRect;

      if (Grid.IndicatorRow.ShowRecNo)
      {
        sectRect = e.CellClientRect;
        sectRect.Width = e.CellClientRect.Width - widthReducer;
        PaintRowNum(e.Graphics, sectRect, e.State, e.AreaColIndex, e.AreaRowIndex);
      }

    }

    protected internal virtual void PaintRowNum(Graphics graphics,
                                          Rectangle paintRect,
                                          BasePaintCellStates state,
                                          int areaColIndex,
                                          int areaRowIndex)
    {
      //TextFormatFlags aligntFlags;

      //aligntFlags = TextFormatFlags.Left | TextFormatFlags.VerticalCenter;
      Font font = Grid.IndicatorRow.Font;
      Color foreColor = Grid.ForeColor;

      if ((state & BasePaintCellStates.Selected) != 0)
        foreColor = Grid.GetDrawStyle().GetFixedSelectedForeColor(Grid, Grid.FixedBackFiller.Color, foreColor);

      Rectangle dataRect = paintRect;

      string text = GetDisplayText(areaColIndex, areaRowIndex);
      Grid.PaintingDrawText(graphics, text, font, dataRect, foreColor, 
        HorizontalAlignment.Center, VerticalAlignment.Center, false);

    }

    //Other
    public override string GetDisplayText(int areaColIndex, int areaRowIndex)
    {
      if (Grid.CurrencyManager != null && Grid.CurrencyManager.Position >= 0)
      {
        int pos = Grid.CurrencyManager.Position + 1;
        return pos.ToString();
      }
      else
      {
        return "";
      }
    }

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

      if (e.BorderType == GridCellBorderType.Right || e.BorderType == GridCellBorderType.Left)
      {
        GridLine gl = Grid.TitleBar.VertLine;
        e.Visible = gl.Visible;
        e.Color = gl.Color;
        e.Style = gl.Style;
        e.IsExtent = true;
      }
      else
      {
        GridLine gl = Grid.TitleBar.HorzLine;
        e.Visible = gl.Visible;
        e.Color = gl.Color;
        e.Style = gl.Style;
        e.IsExtent = true;
      }
    }

  }
}