using System;
using System.Text;
using System.Drawing;
using System.ComponentModel;
using DevComponents.AdvTree;
using System.Drawing.Drawing2D;
using DevComponents.DotNetBar;
namespace DevComponents.WinForms.Drawing
{
    internal class RectangleShape : Shape
    {
        #region Internal Implementation
        /// 
        /// Renders rectangle on canvas.
        /// 
        /// Target graphics to render shape on.
        /// Shape bounds.
        public override void Paint(Graphics g, Rectangle bounds)
        {
            if (bounds.Width < 2 || bounds.Height < 2 || g == null || _Fill == null && _Border == null) return;
            GraphicsPath path = null;
            
            if (!_CornerRadius.IsZero)
            {
                path = DisplayHelp.GetRoundedRectanglePath(bounds, _CornerRadius.TopLeft, _CornerRadius.TopRight, 
                    _CornerRadius.BottomRight, _CornerRadius.BottomLeft);
            }
            if (_Fill != null)
            {
                Brush brush = _Fill.CreateBrush(bounds);
                if (brush != null)
                {
                    SmoothingMode sm = g.SmoothingMode;
                    if (brush is SolidBrush && path==null)
                        g.SmoothingMode = SmoothingMode.None;
                    if (path == null)
                        g.FillRectangle(brush, bounds);
                    else
                        g.FillPath(brush, path);
                    g.SmoothingMode = sm;
                    brush.Dispose();
                }
            }
            if (_Border != null)
            {
                Pen pen = _Border.CreatePen();
                if (pen != null)
                {
                    if (path == null)
                        g.DrawRectangle(pen, bounds);
                    else
                        g.DrawPath(pen, path);
                    pen.Dispose();
                }
            }
            Shape content = this.Content;
            if (content != null)
            {
                Rectangle contentBounds = Border.Deflate(bounds, _Border);
                Region oldClip = null;
                if (path != null && ClipToBounds)
                {
                    oldClip = g.Clip;
                    g.SetClip(path, CombineMode.Intersect);
                }
                content.Paint(g, contentBounds);
                if (oldClip != null) g.Clip = oldClip;
            }
            if (path != null) path.Dispose();
        }
        private Border _Border;
        /// 
        /// Gets or sets shape border.
        /// 
        [DefaultValue(null), Description("Indicates shape border.")]
        public Border Border
        {
            get { return _Border; }
            set { _Border = value; }
        }
        private Fill _Fill = null;
        /// 
        /// Gets or sets the shape fill.
        /// 
        [DefaultValue(null), Description("Indicates shape fill")]
        public Fill Fill
        {
            get { return _Fill; }
            set { _Fill = value; }
        }
        private CornerRadius _CornerRadius;
        /// 
        /// Gets or sets the CornerRadius.
        /// 
        public CornerRadius CornerRadius
        {
            get { return _CornerRadius; }
            set { _CornerRadius = value; }
        }
        /// 
        /// Gets whether property should be serialized.
        /// 
        [EditorBrowsable(EditorBrowsableState.Never)]
        public bool ShouldSerializeCornerRadius()
        {
            return !_CornerRadius.IsZero;
        }
        /// 
        /// Resets the property to its default value.
        /// 
        [EditorBrowsable(EditorBrowsableState.Never)]
        public void ResetCornerRadius()
        {
            CornerRadius = new CornerRadius();
        }
        #endregion
    }
}