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

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Haestad.Support.Support;

namespace Haestad.Framework.UI.Integration
{
    /// <summary>
    /// Class represents the maximize/restore button on an application parent form title bar.
    /// </summary>
    internal class TitleBarMaximizeRestoreButton : TitleBarButton
    {
        #region Constructor
        public TitleBarMaximizeRestoreButton(EventHandler clickEventHandler, ITitleBarColorInformation titleBarColorInformation, ToolTip toolTip, double sizeFactor, IQueryIsMaximized queryIsMaximized)
            : base(clickEventHandler, titleBarColorInformation, toolTip, sizeFactor)
        {
            QueryIsMaximized = queryIsMaximized;
        }
        #endregion

        #region Public Methods
        public void RefreshToolTip()
        {
            InitializeToolTip();
        }
        #endregion

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

            DrawWindowIcon(pevent.Graphics, QueryIsMaximized.IsMaximized);
        }
        #endregion

        #region Private Methods
        private void DrawWindowIcon(Graphics graphics, bool drawRestoreDownIcon)
        {
            using (Brush backBrush = new SolidBrush(BackColor))
            using (Brush foreBrush = new SolidBrush(ForeColor))
            {
                if (drawRestoreDownIcon)
                {
                    double BOUNDS_BUFFER_PERECENT = 0.3;
                    double RESTORE_DELTA_OFFSET_PERCENT = 0.1;
                    double INNER_RECT_BOUNDS_BUFFER_PERCENT = 0.2;
                    double INNER_RECT_Y_OFFSET_PERCENT = 0.15;
                    double SECOND_DRAW_BACK_EXTENT_EXTENSION_PERCENT = 0.05;
                    double ADDITIONAL_WIDTH_BUFFER = 0.0;
                    bool shouldDrawBackRectangle = true;

                    if (RibbonExternalConfiguration.Instance.Windows10StyleEnabled)
                    {
                        BOUNDS_BUFFER_PERECENT = 0.3;
                        RESTORE_DELTA_OFFSET_PERCENT = 0.05;
                        INNER_RECT_BOUNDS_BUFFER_PERCENT = 0.1;
                        INNER_RECT_Y_OFFSET_PERCENT = 0.0;
                        SECOND_DRAW_BACK_EXTENT_EXTENSION_PERCENT = 0.05;
                        ADDITIONAL_WIDTH_BUFFER = 0.22;
                        shouldDrawBackRectangle = false;
                    }

                    Rectangle outerRect = DrawOffsetRect(DrawRectangle, graphics, foreBrush, BOUNDS_BUFFER_PERECENT,
                        RESTORE_DELTA_OFFSET_PERCENT + ADDITIONAL_WIDTH_BUFFER, -RESTORE_DELTA_OFFSET_PERCENT + ADDITIONAL_WIDTH_BUFFER, -RESTORE_DELTA_OFFSET_PERCENT, RESTORE_DELTA_OFFSET_PERCENT);
                    DrawOffsetRect(outerRect, graphics, backBrush, INNER_RECT_BOUNDS_BUFFER_PERCENT,
                        0.0, 0.0, INNER_RECT_Y_OFFSET_PERCENT, 0.0);

                    if (shouldDrawBackRectangle)
                    {
                        DrawOffsetRect(DrawRectangle, graphics, backBrush, BOUNDS_BUFFER_PERECENT,
                            -RESTORE_DELTA_OFFSET_PERCENT + 0.2 - SECOND_DRAW_BACK_EXTENT_EXTENSION_PERCENT + 0.2, RESTORE_DELTA_OFFSET_PERCENT - SECOND_DRAW_BACK_EXTENT_EXTENSION_PERCENT,
                            RESTORE_DELTA_OFFSET_PERCENT - SECOND_DRAW_BACK_EXTENT_EXTENSION_PERCENT, -RESTORE_DELTA_OFFSET_PERCENT - SECOND_DRAW_BACK_EXTENT_EXTENSION_PERCENT);
                    }

                    outerRect = DrawOffsetRect(DrawRectangle, graphics, foreBrush, BOUNDS_BUFFER_PERECENT,
                        -RESTORE_DELTA_OFFSET_PERCENT + ADDITIONAL_WIDTH_BUFFER, RESTORE_DELTA_OFFSET_PERCENT + ADDITIONAL_WIDTH_BUFFER, RESTORE_DELTA_OFFSET_PERCENT, -RESTORE_DELTA_OFFSET_PERCENT);
                    DrawOffsetRect(outerRect, graphics, backBrush, INNER_RECT_BOUNDS_BUFFER_PERCENT,
                        0.0, 0.0, INNER_RECT_Y_OFFSET_PERCENT, 0.0);
                }
                else
                {
                    double BOUNDS_BUFFER_PERECENT = 0.25;
                    double INNER_RECT_BOUNDS_BUFFER_PERCENT = 0.2;
                    double INNER_RECT_Y_OFFSET_PERCENT = 0.15;
                    double LEFT_RIGHT_BUFFER_PERCENT = 0.0;

                    if (RibbonExternalConfiguration.Instance.Windows10StyleEnabled)
                    {
                        BOUNDS_BUFFER_PERECENT = 0.3;
                        INNER_RECT_BOUNDS_BUFFER_PERCENT = 0.1;
                        INNER_RECT_Y_OFFSET_PERCENT = 0.0;
                        LEFT_RIGHT_BUFFER_PERCENT = 0.2;
                    }

                    Rectangle outerRect = DrawOffsetRect(DrawRectangle, graphics, foreBrush, BOUNDS_BUFFER_PERECENT,
                        LEFT_RIGHT_BUFFER_PERCENT, LEFT_RIGHT_BUFFER_PERCENT, 0.0, 0.0);
                    DrawOffsetRect(outerRect, graphics, backBrush, INNER_RECT_BOUNDS_BUFFER_PERCENT,
                        0.0, 0.0, INNER_RECT_Y_OFFSET_PERCENT, 0.0);
                }
            }
        }
        #endregion

        #region Protected Override Properties
        protected override string ToolTipText
        {
            get
            {
                if (QueryIsMaximized.IsMaximized)
                    return TextManager.Current["hfui_titleBarRestoreDownButtonToolTip"];
                else
                    return TextManager.Current["hfui_titleBarMaximizeButtonToolTip"];
            }
        }
        #endregion

        #region Private Properties
        private IQueryIsMaximized QueryIsMaximized
        {
            get;
            set;
        }
        #endregion

    }
}
