﻿namespace MetroFramework.Controls
{
    using MetroFramework;
    using MetroFramework.Components;
    using MetroFramework.Design;
    using MetroFramework.Drawing;
    using MetroFramework.Interfaces;
    using MetroFramework.Native;
    using System;
    using System.ComponentModel;
    using System.Drawing;
    using System.Windows.Forms;

    [ToolboxItem(false), Designer(typeof(MetroTabPageDesigner))]
    public class MetroTabPage : TabPage, IMetroControl
    {
        private MetroScrollBar horizontalScrollbar = new MetroScrollBar(MetroScrollOrientation.Horizontal);
        private MetroColorStyle metroStyle = MetroColorStyle.Blue;
        private MetroStyleManager metroStyleManager;
        private MetroThemeStyle metroTheme;
        [Category("Metro Appearance")]
        private bool showHorizontalScrollbar;
        [Category("Metro Appearance")]
        private bool showVerticalScrollbar;
        private bool useCustomBackground;
        private MetroScrollBar verticalScrollbar = new MetroScrollBar(MetroScrollOrientation.Vertical);

        public MetroTabPage()
        {
            base.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint | ControlStyles.SupportsTransparentBackColor | ControlStyles.ResizeRedraw | ControlStyles.UserPaint, true);
            base.Controls.Add(this.verticalScrollbar);
            base.Controls.Add(this.horizontalScrollbar);
            this.verticalScrollbar.UseBarColor = true;
            this.horizontalScrollbar.UseBarColor = true;
            this.verticalScrollbar.Scroll += new ScrollEventHandler(this.VerticalScrollbarScroll);
            this.horizontalScrollbar.Scroll += new ScrollEventHandler(this.HorizontalScrollbarScroll);
        }

        private void HorizontalScrollbarScroll(object sender, ScrollEventArgs e)
        {
            base.AutoScrollPosition = new System.Drawing.Point(e.NewValue, this.verticalScrollbar.Value);
            this.UpdateScrollBarPositions();
        }

        protected override void OnMouseWheel(MouseEventArgs e)
        {
            base.OnMouseWheel(e);
            this.verticalScrollbar.Value = base.VerticalScroll.Value;
            this.horizontalScrollbar.Value = base.HorizontalScroll.Value;
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            Color backColor = MetroPaint.BackColor.Form(this.Theme);
            if (this.useCustomBackground)
            {
                backColor = this.BackColor;
            }
            e.Graphics.Clear(backColor);
            if (base.DesignMode)
            {
                this.horizontalScrollbar.Visible = false;
                this.verticalScrollbar.Visible = false;
            }
            else
            {
                this.UpdateScrollBarPositions();
                if (this.HorizontalScrollbar)
                {
                    this.horizontalScrollbar.Visible = base.HorizontalScroll.Visible;
                }
                if (base.HorizontalScroll.Visible)
                {
                    this.horizontalScrollbar.Minimum = base.HorizontalScroll.Minimum;
                    this.horizontalScrollbar.Maximum = base.HorizontalScroll.Maximum;
                    this.horizontalScrollbar.SmallChange = base.HorizontalScroll.SmallChange;
                    this.horizontalScrollbar.LargeChange = base.HorizontalScroll.LargeChange;
                }
                if (this.VerticalScrollbar)
                {
                    this.verticalScrollbar.Visible = base.VerticalScroll.Visible;
                }
                if (base.VerticalScroll.Visible)
                {
                    this.verticalScrollbar.Minimum = base.VerticalScroll.Minimum;
                    this.verticalScrollbar.Maximum = base.VerticalScroll.Maximum;
                    this.verticalScrollbar.SmallChange = base.VerticalScroll.SmallChange;
                    this.verticalScrollbar.LargeChange = base.VerticalScroll.LargeChange;
                }
            }
        }

        private void UpdateScrollBarPositions()
        {
            if (!base.DesignMode)
            {
                if (!this.AutoScroll)
                {
                    this.verticalScrollbar.Visible = false;
                    this.horizontalScrollbar.Visible = false;
                }
                else
                {
                    if (this.VerticalScrollbar)
                    {
                        if (base.VerticalScroll.Visible)
                        {
                            this.verticalScrollbar.Location = new System.Drawing.Point(base.ClientRectangle.Width - this.verticalScrollbar.Width, base.ClientRectangle.Y);
                            this.verticalScrollbar.Height = base.ClientRectangle.Height;
                        }
                    }
                    else
                    {
                        this.verticalScrollbar.Visible = false;
                    }
                    if (this.HorizontalScrollbar)
                    {
                        if (base.HorizontalScroll.Visible)
                        {
                            this.horizontalScrollbar.Location = new System.Drawing.Point(base.ClientRectangle.X, base.ClientRectangle.Height - this.horizontalScrollbar.Height);
                            this.horizontalScrollbar.Width = base.ClientRectangle.Width;
                        }
                    }
                    else
                    {
                        this.horizontalScrollbar.Visible = false;
                    }
                }
            }
        }

        private void VerticalScrollbarScroll(object sender, ScrollEventArgs e)
        {
            base.AutoScrollPosition = new System.Drawing.Point(this.horizontalScrollbar.Value, e.NewValue);
            this.UpdateScrollBarPositions();
        }

        protected override void WndProc(ref Message m)
        {
            base.WndProc(ref m);
            if (!base.DesignMode)
            {
                WinApi.ShowScrollBar(base.Handle, 3, 0);
            }
        }

        [Category("Metro Appearance")]
        public bool AutoScroll
        {
            get
            {
                return base.AutoScroll;
            }
            set
            {
                if (value)
                {
                    this.showHorizontalScrollbar = true;
                    this.showVerticalScrollbar = true;
                }
                base.AutoScroll = value;
            }
        }

        [Category("Metro Appearance")]
        public bool CustomBackground
        {
            get
            {
                return this.useCustomBackground;
            }
            set
            {
                this.useCustomBackground = value;
            }
        }

        public bool HorizontalScrollbar
        {
            get
            {
                return this.showHorizontalScrollbar;
            }
            set
            {
                this.showHorizontalScrollbar = value;
            }
        }

        [Category("Metro Appearance")]
        public bool HorizontalScrollbarBarColor
        {
            get
            {
                return this.horizontalScrollbar.UseBarColor;
            }
            set
            {
                this.horizontalScrollbar.UseBarColor = value;
            }
        }

        [Category("Metro Appearance")]
        public bool HorizontalScrollbarHighlightOnWheel
        {
            get
            {
                return this.horizontalScrollbar.HighlightOnWheel;
            }
            set
            {
                this.horizontalScrollbar.HighlightOnWheel = value;
            }
        }

        [Category("Metro Appearance")]
        public int HorizontalScrollbarSize
        {
            get
            {
                return this.horizontalScrollbar.ScrollbarSize;
            }
            set
            {
                this.horizontalScrollbar.ScrollbarSize = value;
            }
        }

        [Category("Metro Appearance")]
        public MetroColorStyle Style
        {
            get
            {
                if (this.StyleManager != null)
                {
                    return this.StyleManager.Style;
                }
                return this.metroStyle;
            }
            set
            {
                this.metroStyle = value;
                this.horizontalScrollbar.Style = value;
                this.verticalScrollbar.Style = value;
            }
        }

        [Browsable(false)]
        public MetroStyleManager StyleManager
        {
            get
            {
                return this.metroStyleManager;
            }
            set
            {
                this.metroStyleManager = value;
                this.horizontalScrollbar.StyleManager = value;
                this.verticalScrollbar.StyleManager = value;
            }
        }

        [Category("Metro Appearance")]
        public MetroThemeStyle Theme
        {
            get
            {
                if (this.StyleManager != null)
                {
                    return this.StyleManager.Theme;
                }
                return this.metroTheme;
            }
            set
            {
                this.metroTheme = value;
                this.horizontalScrollbar.Theme = value;
                this.verticalScrollbar.Theme = value;
            }
        }

        public bool VerticalScrollbar
        {
            get
            {
                return this.showVerticalScrollbar;
            }
            set
            {
                this.showVerticalScrollbar = value;
            }
        }

        [Category("Metro Appearance")]
        public bool VerticalScrollbarBarColor
        {
            get
            {
                return this.verticalScrollbar.UseBarColor;
            }
            set
            {
                this.verticalScrollbar.UseBarColor = value;
            }
        }

        [Category("Metro Appearance")]
        public bool VerticalScrollbarHighlightOnWheel
        {
            get
            {
                return this.verticalScrollbar.HighlightOnWheel;
            }
            set
            {
                this.verticalScrollbar.HighlightOnWheel = value;
            }
        }

        [Category("Metro Appearance")]
        public int VerticalScrollbarSize
        {
            get
            {
                return this.verticalScrollbar.ScrollbarSize;
            }
            set
            {
                this.verticalScrollbar.ScrollbarSize = value;
            }
        }
    }
}

