using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

namespace DockableUI.Tabs
{
    class TabsRenderer : System.Windows.Forms.ToolStripProfessionalRenderer
    {
        readonly Color Background = Color.FromArgb(235, 244, 255);

        readonly Color SelectedTabBorder = Color.Black;
        readonly Color SelectedTabBackground = SystemColors.Control; //Color.LightSkyBlue;

        readonly Color HoverTabBackground = Color.LightSkyBlue;

        public TabsRenderer()
        {
            base.RoundedEdges = false;
        }

        protected override void OnRenderToolStripBackground(System.Windows.Forms.ToolStripRenderEventArgs e)
        {
            if (e.ToolStrip.Dock == System.Windows.Forms.DockStyle.Top)
            {
                e.Graphics.FillRectangle(new SolidBrush(Background), e.AffectedBounds);
                e.Graphics.DrawLine(new Pen(SelectedTabBorder), 0, e.ToolStrip.Height - 3, e.ToolStrip.Width, e.ToolStrip.Height - 3);
                e.Graphics.FillRectangle(new SolidBrush(SelectedTabBackground), 0, e.ToolStrip.Height - 2, e.ToolStrip.Width, 2);
            }
            else
            {
                e.Graphics.FillRectangle(new SolidBrush(Background), e.AffectedBounds);
                e.Graphics.DrawLine(new Pen(SelectedTabBorder), 0, 1, e.ToolStrip.Width, 1);
                e.Graphics.FillRectangle(new SolidBrush(SelectedTabBackground), 0, 0, e.ToolStrip.Width, 1);
            }
            //base.OnRenderToolStripBackground(e);
        }

        protected override void OnRenderButtonBackground(System.Windows.Forms.ToolStripItemRenderEventArgs e)
        {
            TabButton item = e.Item as TabButton;
            if (item != null)
            {
                if (e.ToolStrip.Dock == System.Windows.Forms.DockStyle.Top)
                {
                    if (item.Checked)
                    {
                        if (item.Selected)
                        {
                            e.Graphics.FillRectangle(new LinearGradientBrush(new Point(0, 1), new Point(0, item.Height), HoverTabBackground, SelectedTabBackground), 0, 0, item.Width, item.Height);
                        }
                        else
                        {
                            e.Graphics.FillRectangle(new SolidBrush(SelectedTabBackground), 0, 0, e.Item.Width, e.Item.Height);
                        }
                        Pen border = new Pen(SelectedTabBorder, 1);
                        e.Graphics.DrawLine(border, 0, 0, 0, item.Height); //left
                        e.Graphics.DrawLine(border, 0, 0, item.Width, 0);  //top
                        e.Graphics.DrawLine(border, item.Width - 1, 0, item.Width - 1, item.Height);
                    }
                    else
                    {
                        if (e.Item.Selected)
                        {
                            e.Graphics.FillRectangle(new LinearGradientBrush(new Point(0, 1), new Point(0, e.Item.Height - 1), HoverTabBackground, Background), 0, 0, e.Item.Width, e.Item.Height - 1);
                        }
                    }
                }
                else
                {
                    if (item.Checked)
                    {
                        if (item.Selected)
                        {
                            e.Graphics.FillRectangle(new LinearGradientBrush(new Point(0, 0), new Point(0, item.Height), SelectedTabBackground, HoverTabBackground), 0, 0, item.Width, item.Height);
                        }
                        else
                        {
                            e.Graphics.FillRectangle(new SolidBrush(SelectedTabBackground), 0, 0, e.Item.Width, e.Item.Height);
                        }
                        Pen border = new Pen(SelectedTabBorder, 1);
                        e.Graphics.DrawLine(border, 0, 0, 0, item.Height); //left
                        e.Graphics.DrawLine(border, 0, item.Height - 1, item.Width, item.Height - 1);  //top
                        e.Graphics.DrawLine(border, item.Width - 1, 0, item.Width - 1, item.Height);
                    }
                    else
                    {
                        if (e.Item.Selected)
                        {
                            e.Graphics.FillRectangle(new LinearGradientBrush(new Point(0, 1), new Point(0, e.Item.Height - 1), Background, HoverTabBackground), 0, 1, e.Item.Width, e.Item.Height);
                        }
                    }
                }
            }
            else
            {
                base.OnRenderButtonBackground(e);
            }
        }

        protected override void OnRenderItemImage(System.Windows.Forms.ToolStripItemImageRenderEventArgs e)
        {
            TabButton item = e.Item as TabButton;
            if (item != null)
            {
                e.Graphics.DrawImage(e.Image, e.ImageRectangle);
            }
            else
            {
                base.OnRenderItemImage(e);
            }
        }

        protected override void OnRenderToolStripBorder(ToolStripRenderEventArgs e)
        {
            //base.OnRenderToolStripBorder(e);
        }

        protected override void OnRenderGrip(ToolStripGripRenderEventArgs e)
        {
            //base.OnRenderGrip(e);
        }
    }
}
