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

using System.ComponentModel;
using System.Drawing;

namespace EhLib
{

  [DataAxisGridDataCellDesignTimeVisibleAttribute(true)]
  public class DataAxisGridProgressBarDataCell : DataAxisGridDataCellMan
  {
    #region privates
    private double minValue;
    private double maxValue = 100;
    private Color backColor = Color.Empty;
    private Color frameColor = Color.Empty;
    private Color textColor = Color.Empty;
    private bool showText = true;
    private ProgressBarTextType textType = ProgressBarTextType.AsPercent;
    private int textDecimalPlaces;
    private ProgressBarFrameSizeType frameSizeType = ProgressBarFrameSizeType.Val;
    #endregion

    #region constructor
    public DataAxisGridProgressBarDataCell()
    {
      MinValue = 0;
      MaxValue = 100;
      FrameColor = Color.Empty;
      TextColor = Color.Empty;
      ShowText = true;
      TextType = ProgressBarTextType.AsPercent;
      TextDecimalPlaces = TextDecimalPlaces;
      FrameSizeType = FrameSizeType;
    }
    #endregion

    #region properties
    //new public DataGridProgressBarColumn Column
    //{
    //  get { return (DataGridProgressBarColumn)base.Column; }
    //}

    //public Double CurrentValue { get; set; }
    [DefaultValue(0)]
    public double MinValue
    {
      get { return minValue; }
      set { minValue = value; }
    }

    [DefaultValue(100)]
    public double MaxValue
    {
      get { return maxValue; }
      set { maxValue = value; }
    }

    //[DefaultValue(Color.Empty)]

    public Color FrameColor
    {
      get { return frameColor; }
      set { frameColor = value; }
    }

    public Color TextColor
    {
      get { return textColor; }
      set { textColor = value; }
    }

    [DefaultValue(true)]
    public bool ShowText
    {
      get { return showText; }
      set { showText = value; }
    }

    [DefaultValue(ProgressBarTextType.AsPercent)]
    public ProgressBarTextType TextType
    {
      get { return textType; }
      set { textType = value; }
    }

    [DefaultValue(0)]
    public int TextDecimalPlaces
    {
      get { return textDecimalPlaces; }
      set { textDecimalPlaces = value; }
    }

    [DefaultValue(ProgressBarFrameSizeType.Val)]
    public ProgressBarFrameSizeType FrameSizeType
    {
      get { return frameSizeType; }
      set { frameSizeType = value; }
    }
    #endregion

    #region public methods
    public override string GetDisplayText(PropertyAxisBar propAxisBar, object listItemBar)
    {
      return base.GetDisplayText(propAxisBar, listItemBar);
    }
    #endregion

    #region internal methods
    //FrameColor
    public virtual Color DefaultFrameColor()
    {
      return Color.Empty;
    }

    protected internal virtual bool ShouldSerializeFrameColor()
    {
      if (frameColor == DefaultFrameColor())
        return false;
      else
        return true;
    }

    public virtual void ResetFrameColor()
    {
      frameColor = DefaultFrameColor();
      FrameColorChanged();
    }

    protected internal void FrameColorChanged()
    {
      if (Grid != null)
        Grid.Invalidate();
    }

    //FrameTextColor
    protected internal virtual Color DefaultTextColor()
    {
      return Color.Empty;
    }

    protected internal virtual bool ShouldSerializeTextColor()
    {
      if (backColor.IsEmpty)
        return false;
      else
        return true;
    }

    public virtual void ResetTextColor()
    {
      textColor = DefaultTextColor();
      TextColorChanged();
    }

    protected internal void TextColorChanged()
    {
      if (Grid != null)
        Grid.Invalidate();
    }

    //FrameFore
    public override Color DefaultForeColor()
    {
      return Color.Empty;
    }

    //Other
    protected override void PaintForeground(DataAxisGridDataCellPaintEventArgs e)
    {
      if (e.PropAxisBar == null) return;

      ProgressBarPaintEventArgs args = EhLibDrawStyle.DefaultProgressBarDrawStyle.CreateProgressBarPaintArgs();

      EhLibDrawStyle.DefaultProgressBarDrawStyle.FillProgressBarPaintArgsDefaultValues(args);

      if (e.PropAxisBar != null)
      {
        args.MinValue = MinValue;
        args.MaxValue = MaxValue;
        args.CurrentValue = args.MinValue;

        object val = e.PropAxisBar.GetValue(e.ListItemBar);
        if (!EhLibUtils.DBValueEqual(val, null))
        {
          TypeConverter typeConverter = TypeDescriptor.GetConverter(val.GetType());

          if (!EhLibUtils.DBValueEqual(val, null) &&
              typeConverter.CanConvertTo(typeof(double)))
          {
            //if (!EhLibUtils.DBValueEqual(val, null) && (EhLibManager.DefaultEhLibManager.GetTypeCategoryForType(val.GetType()) == TypeCategory.Number))
            object dval = typeConverter.ConvertTo(val, typeof(double));
            if (dval != null)
            {
              args.CurrentValue = (double)dval;
            }
          }
        }

        args.Font = Font;
        if (TextColor == Color.Empty)
          args.TextColor = SystemColors.WindowText;
        else
          args.TextColor = TextColor;

        args.OutbarTextColor = args.TextColor;

        if ((BasePaintCellStates.Current & e.State) != 0)
        {
          float backBright = e.BackFilledColor.GetBrightness();
          float fontBright = args.TextColor.GetBrightness();
          if ((backBright < 0.5 && fontBright < 0.5) ||
              (backBright >= 0.5 && fontBright >= 0.5)
          )
            args.OutbarTextColor = SystemColors.HighlightText;
        }

        args.ShowText = ShowText;

        if (ForeColor != Color.Empty)
          args.ForeColor = ForeColor;
        if (FrameColor != Color.Empty)
          args.FrameColor = FrameColor;
        args.BackgroundColor = Color.Empty;

        args.TextType = TextType;
        args.TextDecimalPlaces = TextDecimalPlaces;
        //args.TextAlignment
        args.FrameSizeType = FrameSizeType;
        args.Indent = 0;

      }

      Rectangle progressBarPaintRect = EhLibUtils.TrimPadding(e.CellRect, Padding);

      EhLibDrawStyle.DefaultProgressBarDrawStyle.DrawBar(e.Graphics, progressBarPaintRect, args);
    }
    #endregion
  }
}
