using System.ComponentModel;
using System.Drawing;
using System.Drawing.Design;
using System.Windows.Forms;
using DevComponents.DotNetBar.SuperGrid.Primitives;
namespace DevComponents.DotNetBar.SuperGrid.Style
{
    ///
    /// CellVisualStyles
    ///
    [TypeConverter(typeof(VisualStylesConverter))]
    public class CellVisualStyles : VisualStyles
    {
    }
    /// 
    /// Represents the visual style of an element.
    /// 
    [TypeConverter(typeof(VisualStylesConverter))]
    public class CellVisualStyle : VisualStyle
    {
        #region Private variables
        private Tbool _AllowWrap = Tbool.NotSet;
        private Tbool _AllowMultiLine = Tbool.NotSet;
        private Alignment _Alignment = Alignment.NotSet;
        private Image _Image;
        private int _ImageIndex = -1;
        private Padding _ImagePadding;
        private Alignment _ImageAlignment = Alignment.NotSet;
        private ImageHighlightMode _ImageHighlightMode = ImageHighlightMode.NotSet;
        private ImageOverlay _ImageOverlay = ImageOverlay.NotSet;
        private SymbolDef _SymbolDef;
        #endregion
        #region Public properties
        #region Alignment
        /// 
        /// Gets or sets the alignment of the content within the cell
        /// 
        [DefaultValue(Alignment.NotSet), Category("Appearance")]
        [Description("Indicates the alignment of the content within the cell.")]
        public Alignment Alignment
        {
            get { return (_Alignment); }
            set
            {
                if (_Alignment != value)
                {
                    _Alignment = value;
                    OnPropertyChangedEx("Alignment", VisualChangeType.Layout);
                }
            }
        }
        #endregion
        #region AllowMultiLine
        /// 
        /// Gets or sets whether multiple text lines (via "\r\n" inclusion)
        /// are displayed when text wrapping (AllowWrap) is disabled.
        /// 
        [DefaultValue(Tbool.NotSet), Category("Appearance")]
        [Description("Indicates whether multiple text lines (via '\r\n' inclusion) are displayed when text wrapping (AllowWrap) is disabled.")]
        public Tbool AllowMultiLine
        {
            get { return (_AllowMultiLine); }
            set
            {
                if (_AllowMultiLine != value)
                {
                    _AllowMultiLine = value;
                    OnPropertyChangedEx("AllowMultiLine", VisualChangeType.Layout);
                }
            }
        }
        #endregion
        #region AllowWrap
        /// 
        /// Gets or sets whether text wrapping is permitted
        /// 
        [DefaultValue(Tbool.NotSet), Category("Appearance")]
        [Description("Indicates whether text wrapping is permitted.")]
        public Tbool AllowWrap
        {
            get { return (_AllowWrap); }
            set
            {
                if (_AllowWrap != value)
                {
                    _AllowWrap = value;
                    OnPropertyChangedEx("AllowWrap", VisualChangeType.Layout);
                }
            }
        }
        #endregion
        #region Image
        /// 
        /// Gets or sets the element Image
        /// 
        [DefaultValue(null), Category("Appearance")]
        [Description("Indicates the element image")]
        public Image Image
        {
            get { return (_Image); }
            set
            {
                if (_Image != value)
                {
                    _Image = value;
                    OnPropertyChangedEx("Image", VisualChangeType.Layout);
                }
            }
        }
        #endregion
        #region ImageAlignment
        /// 
        /// Gets or sets the alignment of the Image within the cell
        /// 
        [DefaultValue(Alignment.NotSet), Category("Appearance")]
        [Description("Indicates the alignment of the Image within the cell.")]
        public Alignment ImageAlignment
        {
            get { return (_ImageAlignment); }
            set
            {
                if (_ImageAlignment != value)
                {
                    _ImageAlignment = value;
                    OnPropertyChangedEx("ImageAlignment", VisualChangeType.Layout);
                }
            }
        }
        #endregion
        #region ImageHighlightMode
        /// 
        /// Gets or sets how cell images are
        /// highlighted when the cell is selected
        /// 
        [DefaultValue(ImageHighlightMode.NotSet), Category("Style")]
        [Description("Indicates how cell images are highlighted when the cell is selected")]
        public ImageHighlightMode ImageHighlightMode
        {
            get { return (_ImageHighlightMode); }
            set
            {
                if (value != _ImageHighlightMode)
                {
                    _ImageHighlightMode = value;
                    OnPropertyChangedEx("ImageHighlightMode", VisualChangeType.Render);
                }
            }
        }
        #endregion
        #region ImageIndex
        /// 
        /// Gets or sets the image index
        /// 
        [Browsable(true), DefaultValue(-1)]
        [Category("Appearance"), Description("Indicates the image index")]
        [Editor("DevComponents.SuperGrid.Design.ImageIndexEditor, DevComponents.SuperGrid.Design, Version=14.1.0.37, Culture=neutral,  PublicKeyToken=26d81176cfa2b486", typeof(UITypeEditor))]
        [TypeConverter(typeof(ImageIndexConverter))]
        public int ImageIndex
        {
            get { return (_ImageIndex); }
            set
            {
                if (_ImageIndex != value)
                {
                    _ImageIndex = value;
                    OnPropertyChangedEx("ImageIndex", VisualChangeType.Layout);
                }
            }
        }
        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        private void ResetImageIndex()
        {
            ImageIndex = -1;
        }
        #endregion
        #region ImageOverlay
        /// 
        /// Gets or sets how to overlay the cell image with respect to cell content
        /// 
        [DefaultValue(ImageOverlay.NotSet), Category("Appearance")]
        [Description("Indicates how to overlay the cell image with respect to cell content.")]
        public ImageOverlay ImageOverlay
        {
            get { return (_ImageOverlay); }
            set
            {
                if (_ImageOverlay != value)
                {
                    _ImageOverlay = value;
                    OnPropertyChangedEx("ImageOverlay", VisualChangeType.Layout);
                }
            }
        }
        #endregion
        #region ImagePadding
        /// 
        /// Gets or sets the spacing between content and edges of the Image
        /// 
        [Description("Indicates the spacing between content and edges of the Image")]
        public Padding ImagePadding
        {
            get
            {
                if (_ImagePadding == null)
                {
                    _ImagePadding = Padding.Empty;
                    UpdateChangeHandler(null, _ImagePadding);
                }
                return (_ImagePadding);
            }
            set
            {
                if (_ImagePadding != value)
                {
                    UpdateChangeHandler(_ImagePadding, value);
                    _ImagePadding = value;
                    OnPropertyChangedEx("ImagePadding", VisualChangeType.Layout);
                }
            }
        }
        /// 
        /// Gets whether property should be serialized.
        /// 
        [EditorBrowsable(EditorBrowsableState.Never)]
        private bool ShouldSerializeImagePadding()
        {
            return (_ImagePadding != null && _ImagePadding.IsEmpty == false);
        }
        /// 
        /// Resets property to its default value.
        /// 
        [EditorBrowsable(EditorBrowsableState.Never)]
        private void ResetImagePadding()
        {
            ImagePadding = null;
        }
        #endregion
        #region SymbolDef
        /// 
        /// Gets or sets the element Symbol Definition. Note that
        /// Symbol definition takes precedence over Image definition.
        /// 
        [DefaultValue(null), Category("Appearance")]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        [Description("Indicates the element Symbol Definition. Note that Symbol definition takes precedence over Image definition.")]
        public SymbolDef SymbolDef
        {
            get
            {
                if (_SymbolDef == null)
                {
                    _SymbolDef = new SymbolDef();
                    UpdateChangeHandler(null, _SymbolDef);
                }
                return (_SymbolDef);
            }
            set
            {
                if (_SymbolDef != value)
                {
                    UpdateChangeHandler(_SymbolDef, value);
                    _SymbolDef = value;
                    OnPropertyChangedEx("SymbolDef", VisualChangeType.Layout);
                }
            }
        }
        /// 
        /// Gets whether property should be serialized.
        /// 
        [EditorBrowsable(EditorBrowsableState.Never)]
        private bool ShouldSerializeSymbolDef()
        {
            return (_SymbolDef != null && _SymbolDef.IsEmpty == false);
        }
        /// 
        /// Resets property to its default value.
        /// 
        [EditorBrowsable(EditorBrowsableState.Never)]
        private void ResetSymbolDef()
        {
            SymbolDef = null;
        }
        #endregion
        #region IsEmpty
        /// 
        /// Gets whether the style is logically Empty.
        /// 
        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        [Description("Gets whether the style is logically Empty.")]
        public override bool IsEmpty
        {
            get
            {
                return ((_Alignment == Alignment.NotSet) &&
                    (_AllowWrap == Tbool.NotSet) &&
                    (_Image == null) &&
                    (_ImageAlignment == Alignment.NotSet) &&
                    (_ImageHighlightMode == ImageHighlightMode.NotSet) &&
                    (_ImageIndex == -1) &&
                    (_ImageOverlay == ImageOverlay.NotSet) &&
                    (_ImagePadding == null || _ImagePadding.IsEmpty == true) &&
                    (base.IsEmpty == true));
            }
        }
        #endregion
        #endregion
        #region Internal properties
        #region IsOverlayImage
        internal bool IsOverlayImage
        {
            get
            {
                return (_ImageOverlay == ImageOverlay.Top ||
                    _ImageOverlay == ImageOverlay.Bottom);
            }
        }
        #endregion
        #region IsSymbolFigure
        internal bool IsSymbolFigure
        {
            get { return (_SymbolDef != null && _SymbolDef.IsValidSymbol == true); }
        }
        #endregion
        #endregion
        #region ApplyStyle
        /// 
        /// Applies the style to instance of this style.
        /// 
        /// Style to apply.
        public void ApplyStyle(CellVisualStyle style)
        {
            if (style != null)
            {
                base.ApplyStyle(style);
                if (style.Alignment != Alignment.NotSet)
                    _Alignment = style.Alignment;
                if (style.AllowWrap != Tbool.NotSet)
                    _AllowWrap = style.AllowWrap;
                if (style.AllowMultiLine != Tbool.NotSet)
                    _AllowMultiLine = style.AllowMultiLine;
                if (style.ImageIndex >= 0)
                {
                    _Image = null;
                    _ImageIndex = style.ImageIndex;
                }
                if (style.Image != null)
                {
                    _Image = style.Image;
                    _ImageIndex = -1;
                }
                if (style.ImageAlignment != Alignment.NotSet)
                    _ImageAlignment = style.ImageAlignment;
                if (style.ImageHighlightMode != ImageHighlightMode.NotSet)
                    _ImageHighlightMode = style.ImageHighlightMode;
                if (style._ImagePadding != null && style._ImagePadding.IsEmpty == false)
                    _ImagePadding = style._ImagePadding.Copy();
                if (style.ImageOverlay != ImageOverlay.NotSet)
                    _ImageOverlay = style.ImageOverlay;
                if (style._SymbolDef != null && style._SymbolDef.IsEmpty == false)
                    _SymbolDef = style._SymbolDef.Copy();
            }
        }
        #endregion
        #region ApplyDefaults
        internal void ApplyDefaults()
        {
            if (_SymbolDef != null && _SymbolDef.IsValidSymbol == true)
            {
                if (_SymbolDef.SymbolSize == 0)
                {
                    if (Font != null)
                        _SymbolDef.SymbolSize = Font.SizeInPoints;
                }
                if (_SymbolDef.SymbolColor.IsEmpty == true)
                {
                    _SymbolDef.SymbolColor =
                        (TextColor.IsEmpty == false) ? TextColor : Color.Black;
                }
            }
        }
        #endregion
        #region GetTextFormatFlags
        internal virtual eTextFormat GetTextFormatFlags()
        {
            eTextFormat tf = eTextFormat.WordEllipsis |
                eTextFormat.NoPadding | eTextFormat.NoPrefix;
            if (AllowWrap == Tbool.True)
            {
                tf |= eTextFormat.WordBreak;
            }
            else
            {
                if (AllowMultiLine != Tbool.True)
                    tf |= eTextFormat.SingleLine;
            }
            switch (Alignment)
            {
                case Alignment.TopCenter:
                    tf |= eTextFormat.HorizontalCenter;
                    break;
                case Alignment.TopRight:
                    tf |= eTextFormat.Right;
                    break;
                case Alignment.MiddleLeft:
                    tf |= eTextFormat.VerticalCenter;
                    break;
                case Alignment.MiddleCenter:
                    tf |= eTextFormat.HorizontalCenter;
                    tf |= eTextFormat.VerticalCenter;
                    break;
                case Alignment.MiddleRight:
                    tf |= eTextFormat.Right;
                    tf |= eTextFormat.VerticalCenter;
                    break;
                case Alignment.BottomLeft:
                    tf |= eTextFormat.Bottom;
                    break;
                case Alignment.BottomCenter:
                    tf |= eTextFormat.Bottom;
                    tf |= eTextFormat.HorizontalCenter;
                    break;
                case Alignment.BottomRight:
                    tf |= eTextFormat.Bottom;
                    tf |= eTextFormat.Right;
                    break;
            }
            return (tf);
        }
        #endregion
        #region IsValidFigure
        internal bool IsValidFigure(GridPanel panel)
        {
            if (IsSymbolFigure == true)
                return (true);
            return (GetImage(panel) != null);
        }
        #endregion
        #region GetFigure
        internal object GetFigure(GridPanel panel)
        {
            if (_SymbolDef != null && _SymbolDef.IsEmpty == false)
                return (_SymbolDef);
            return (GetImage(panel));
        }
        #region GetImage
        private Image GetImage(GridPanel panel)
        {
            if (_Image != null)
                return (_Image);
            if (_ImageIndex >= 0 && panel != null)
            {
                ImageList imageList = panel.ImageList;
                if (imageList != null && _ImageIndex < imageList.Images.Count)
                    return (imageList.Images[_ImageIndex]);
            }
            return (null);
        }
        #endregion
        #region GetSymbol
        private string GetSymbol()
        {
            if (_SymbolDef != null && _SymbolDef.IsEmpty == false)
                return (_SymbolDef.SymbolRealized);
            return (null);
        }
        #endregion
        #endregion
        #region GetFigureSize
        internal Size GetFigureSize(GridPanel panel)
        {
            Size size = GetFigureSizeEx(panel);
            if (size.IsEmpty == false)
            {
                size.Width += Dpi.Width(ImagePadding.Horizontal);
                size.Height += Dpi.Height(ImagePadding.Vertical);
            }
            return (size);
        }
        internal Size GetFigureSizeEx(GridPanel panel)
        {
            Size size = Size.Empty;
            if (IsSymbolFigure == true)
            {
                size = _SymbolDef.GetSymbolSize(panel.SuperGrid);
            }
            else
            {
                Image image = GetImage(panel);
                if (image != null)
                    size = Dpi.Size(image.Size);
            }
            return (size);
        }
        #endregion
        #region GetFigureBounds
        internal Rectangle GetFigureBounds(GridPanel panel, Rectangle r)
        {
            Size size = GetFigureSize(panel);
            return (GetSizeBounds(panel, r, size));
        }
        #region GetSizeBounds
        private Rectangle GetSizeBounds(GridPanel panel, Rectangle r, Size size)
        {
            bool overlay = IsOverlayImage;
            switch (ImageAlignment)
            {
                case Alignment.TopLeft:
                    if (overlay == false)
                        r.X -= size.Width;
                    break;
                case Alignment.NotSet:
                case Alignment.MiddleLeft:
                    if (overlay == false)
                        r.X -= size.Width;
                    r.Y += (r.Height - size.Height) / 2;
                    break;
                case Alignment.BottomLeft:
                    if (overlay == false)
                        r.X -= size.Width;
                    r.Y = r.Bottom - size.Height;
                    break;
                case Alignment.TopCenter:
                    r.X += (r.Width - size.Width) / 2;
                    if (overlay == false)
                        r.Y -= size.Height;
                    break;
                case Alignment.MiddleCenter:
                    r.X += (r.Width - size.Width) / 2;
                    r.Y += (r.Height - size.Height) / 2;
                    break;
                case Alignment.BottomCenter:
                    r.X += (r.Width - size.Width) / 2;
                    r.Y = r.Bottom;
                    if (overlay == true)
                        r.Y -= size.Height;
                    break;
                case Alignment.TopRight:
                    r.X = r.Right;
                    if (overlay == true)
                        r.X -= size.Width;
                    break;
                case Alignment.MiddleRight:
                    r.X = r.Right;
                    if (overlay == true)
                        r.X -= size.Width;
                    r.Y += (r.Height - size.Height) / 2;
                    break;
                case Alignment.BottomRight:
                    r.X = r.Right;
                    r.Y = r.Bottom - size.Height;
                    if (overlay == true)
                        r.X -= size.Width;
                    break;
            }
            r.Size = GetFigureSizeEx(panel);
            r.X += ImagePadding.Left;
            r.Y += ImagePadding.Top;
            return (r);
        }
        #endregion
        #endregion
        #region GetNonFigureBounds
        internal Rectangle GetNonFigureBounds(GridPanel panel, Rectangle r)
        {
            object figure = GetFigure(panel);
            if (figure != null)
            {
                if (IsOverlayImage == false)
                {
                    Size size = GetFigureSize(panel);
                    switch (ImageAlignment)
                    {
                        case Alignment.TopCenter:
                            r.Y += size.Height;
                            r.Height -= size.Height;
                            break;
                        case Alignment.MiddleCenter:
                            break;
                        case Alignment.BottomCenter:
                            r.Height -= size.Height;
                            break;
                        case Alignment.TopRight:
                        case Alignment.MiddleRight:
                        case Alignment.BottomRight:
                            r.Width -= size.Width;
                            break;
                        default:
                            r.X += size.Width;
                            r.Width -= size.Width;
                            break;
                    }
                }
            }
            return (r);
        }
        #endregion
        #region RenderFigure
        internal void RenderFigure(Graphics g, GridPanel panel, Rectangle r)
        {
            RenderFigure(g, GetFigure(panel), r);
        }
        internal void RenderFigure(Graphics g, object figure, Rectangle r)
        {
            if (r.Width > 0 && r.Height > 0)
            {
                if (figure is Image)
                {
                    g.DrawImageUnscaledAndClipped((Image)figure, r);
                }
                else if (figure is SymbolDef)
                {
                    SymbolDef sd = (SymbolDef)figure;
                    using (Brush br = new SolidBrush(sd.SymbolColor))
                        g.DrawString(sd.SymbolRealized, sd.SymbolFont, br, r);
                }
            }
        }
        #endregion
        #region Copy
        /// 
        /// Returns the copy of the style.
        /// 
        /// Copy of the style.
        public new CellVisualStyle Copy()
        {
            CellVisualStyle style = new CellVisualStyle();
            CopyTo(style);
            return (style);
        }
        #endregion
        #region CopyTo
        /// 
        /// Returns the copy of the style.
        /// 
        /// Copy of the style.
        public void CopyTo(CellVisualStyle style)
        {
            base.CopyTo(style);
            style.Alignment = _Alignment;
            style.AllowWrap = _AllowWrap;
            style.AllowMultiLine = _AllowMultiLine;
            style.Image = _Image;
            style.ImageAlignment = _ImageAlignment;
            style.ImageHighlightMode = _ImageHighlightMode;
            style.ImageIndex = _ImageIndex;
            style.ImageOverlay = _ImageOverlay;
            style.ImagePadding = (_ImagePadding != null) ? _ImagePadding.Copy() : null;
            style.SymbolDef = (_SymbolDef != null) ? _SymbolDef.Copy() : null;
        }
        #endregion
        #region IDisposable
        /// 
        /// Dispose
        /// 
        public override void Dispose()
        {
            ImagePadding = null;
            SymbolDef = null;
            base.Dispose();
        }
        #endregion
    }
    #region VisualPropertyChangedEventArgs
    /// 
    /// Represents visual property changed event arguments.
    /// 
    public class VisualPropertyChangedEventArgs : PropertyChangedEventArgs
    {
        #region Public data
        /// 
        /// Gets the change type.
        /// 
        public readonly VisualChangeType ChangeType;
        #endregion
        /// 
        /// Initializes a new instance of the VisualPropertyChangedEventArgs class.
        /// 
        public VisualPropertyChangedEventArgs(string propertyName)
            : base(propertyName)
        {
            ChangeType = VisualChangeType.Layout;
        }
        /// 
        /// Initializes a new instance of the VisualPropertyChangedEventArgs class.
        /// 
        public VisualPropertyChangedEventArgs(string propertyName, VisualChangeType changeType)
            : base(propertyName)
        {
            ChangeType = changeType;
        }
    }
    #endregion
}