// TitleBarCloseButton.cs
// Copyright (c) 2018 Bentley Systems, Incorporated. All Rights Reserved.

using System;
using System.Drawing;
using System.Windows.Forms;
using Haestad.Support.Support;

namespace Haestad.Framework.UI.Integration
{
    /// <summary>
    /// Class represents the close button on an application parent form title bar.
    /// </summary>
    internal class TitleBarCloseButton : TitleBarButton
    {
        #region Constructor
        public TitleBarCloseButton(EventHandler clickEventHandler, ITitleBarColorInformation titleBarColorInformation, ToolTip toolTip, double sizeFactor)
            : base(clickEventHandler, titleBarColorInformation, toolTip, sizeFactor)
        {
            Windows10Red = Color.FromArgb(255, 232, 21, 34);
        }
        #endregion

        #region Protected Override Methods
        protected override void OnPaint(PaintEventArgs pevent)
        {
            base.OnPaint(pevent);

            double HORIZONTAL_BUFFER_PERCENT = 0.275;
            double VERTICAL_BUFFER_PERCENT = 0.325;
            double LEG_WIDTH_PERCENT = 0.12;

            if (RibbonExternalConfiguration.Instance.Windows10StyleEnabled)
            {
                HORIZONTAL_BUFFER_PERCENT = 0.5;
                VERTICAL_BUFFER_PERCENT = 0.325;
                LEG_WIDTH_PERCENT = 0.01;
            }

            int edgeDistance = DrawRectangle.Height; // Note: <whar> Buttons are always square in this mode.
            int horizontalBufferDistance = Convert.ToInt32(Math.Round((HORIZONTAL_BUFFER_PERCENT * edgeDistance), MidpointRounding.AwayFromZero));
            int verticalBufferDistance = Convert.ToInt32(Math.Round((VERTICAL_BUFFER_PERCENT * edgeDistance), MidpointRounding.AwayFromZero));
            int legHorizontalWidth = Convert.ToInt32(Math.Round((LEG_WIDTH_PERCENT * edgeDistance), MidpointRounding.AwayFromZero));

            int left = DrawRectangle.Left + horizontalBufferDistance;
            int top = DrawRectangle.Top + verticalBufferDistance;
            int bottom = DrawRectangle.Bottom - verticalBufferDistance;
            int right = DrawRectangle.Right - horizontalBufferDistance;

            Point outsideTopLeft = new Point(left, top);
            Point outsideBottomRight = new Point(right, bottom);
            Point outsideTopRight = new Point(right, top);
            Point outsideBottomLeft = new Point(left, bottom);

            Point insideTopLeft = new Point(left + legHorizontalWidth, top);
            Point insideBottomRight = new Point(right - legHorizontalWidth, bottom);
            Point insideTopRight = new Point(right - legHorizontalWidth, top);
            Point insideBottomLeft = new Point(left + legHorizontalWidth, bottom);

            const float PEN_WIDTH = 0.5f;
            using (Brush brush = new SolidBrush(ForeColor))
            using (Pen pen = new Pen(brush, PEN_WIDTH))
            {
                Point[] blackSlashLeg = new Point[] { outsideTopLeft, insideTopLeft, outsideBottomRight, insideBottomRight };
                Point[] forwardSlashLeg = new Point[] { outsideTopRight, insideTopRight, outsideBottomLeft, insideBottomLeft };
                pevent.Graphics.DrawPolygon(pen, blackSlashLeg);
                pevent.Graphics.DrawPolygon(pen, forwardSlashLeg);
                pevent.Graphics.FillPolygon(brush, blackSlashLeg);
                pevent.Graphics.FillPolygon(brush, forwardSlashLeg);
            }
        }
        #endregion

        #region Public Override Properties
        public override Color BackColor
        {
            get
            {
                if (!RibbonExternalConfiguration.Instance.Windows10StyleEnabled)
                    return base.BackColor;

                if (IsMouseOver)
                    return Windows10Red;
                else
                    return TitleBarColorInformation.GetTitleBarButtonBackColor();
            }
        }

        public override Color ForeColor
        {
            get
            {
                if (RibbonExternalConfiguration.Instance.Windows10StyleEnabled && IsMouseOver)
                    return Color.White;
                else
                    return TitleBarColorInformation.GetTitleBarButtonForeColor();
            }
        }
        #endregion

        #region Protected Override Properties
        protected override string ToolTipText
        {
            get
            {
                return TextManager.Current["hfui_titleBarCloseButtonToolTip"];
            }
        }
        #endregion

        #region Private Properties
        private Color Windows10Red
        {
            get;
            set;
        }
        #endregion
    }
}
