Imports System
Imports System.Reflection
Imports System.Collections
Imports System.ComponentModel
Imports System.ComponentModel.Design
Imports System.Drawing
Imports System.Drawing.Design
Imports System.Windows.Forms
Imports System.Windows.Forms.Design
Imports System.Drawing.Drawing2D

Namespace UI
    <Designer(GetType(PageControlDesigner))> _
    Public Class PageControl
        Inherits Panel

        Private ActivePageValue As Control

        Public Property ActivePage() As Control
            Get
                Return ActivePageValue
            End Get
            Set(ByVal Value As Control)
                For Each i As Control In Controls
                    i.Visible = False
                Next

                ActivePageValue = Value

                If Not ActivePageValue Is Nothing Then
                    ActivePageValue.Visible = True
                    ActivePageValue.BringToFront()

                    If DesignMode Then
                        Dim ccs As IComponentChangeService = CType(GetService(GetType(IComponentChangeService)), IComponentChangeService)
                        ccs.OnComponentChanged(Me, Nothing, Nothing, Nothing)
                    End If
                End If
            End Set
        End Property
    End Class

    Public Class PageControlDesigner
        Inherits ScrollableControlDesigner

        Private PageControl As PageControl

        Public Overloads Overrides Sub Initialize(ByVal component As IComponent)
            MyBase.Initialize(component)

            If TypeOf Control Is PageControl Then
                PageControl = CType(Control, PageControl)
            End If
        End Sub

        Public Overloads Overrides ReadOnly Property Verbs() As DesignerVerbCollection
            Get
                Dim vc As New DesignerVerbCollection

                For Each i As Control In Control.Controls
                    Dim v As New DesignerVerb("Select " + i.Name, AddressOf SelectHandler)
                    vc.Add(v)
                Next

                vc.Add(New DesignerVerb("Add Panel", AddressOf AddPanel))
                vc.Add(New DesignerVerb("Add GroupBox", AddressOf AddGroupBox))
                vc.Add(New DesignerVerb("Add DialogPanel", AddressOf AddDialogPanel))

                Return vc
            End Get
        End Property

        Public Sub SelectHandler(ByVal sender As Object, ByVal e As EventArgs)
            Try
                Dim v As DesignerVerb = CType(sender, DesignerVerb)

                For Each i As Control In Control.Controls
                    If i.Name = StringHelp.GetRight(v.Text, "Select ") Then
                        PageControl.ActivePage = i
                    End If
                Next
            Catch ex As Exception
                Msg.Show(ex.ToString(), MsgIcon.Error)
            End Try
        End Sub

        Public Sub AddPanel(ByVal sender As Object, ByVal e As EventArgs)
            Dim v As DesignerVerb = DirectCast(sender, DesignerVerb)
            Dim host As IDesignerHost = CType(GetService(GetType(IDesignerHost)), IDesignerHost)
            Dim p As Panel = DirectCast(host.CreateComponent(GetType(Panel), GetName("Panel")), Panel)
            p.Dock = DockStyle.Fill
            PageControl.ActivePage = p
            PageControl.Controls.Add(p)
        End Sub

        Public Sub AddDialogPanel(ByVal sender As Object, ByVal e As EventArgs)
            Dim v As DesignerVerb = DirectCast(sender, DesignerVerb)
            Dim host As IDesignerHost = DirectCast(GetService(GetType(IDesignerHost)), IDesignerHost)
            Dim dp As DialogPanel = DirectCast(host.CreateComponent(GetType(DialogPanel), GetName("DialogPanel")), DialogPanel)
            dp.Dock = DockStyle.Fill
            PageControl.ActivePage = dp
            PageControl.Controls.Add(dp)
        End Sub

        Public Sub AddGroupBox(ByVal sender As Object, ByVal e As EventArgs)
            Dim v As DesignerVerb = DirectCast(sender, DesignerVerb)
            Dim host As IDesignerHost = DirectCast(GetService(GetType(IDesignerHost)), IDesignerHost)
            Dim c As Control = DirectCast(host.CreateComponent(GetType(GroupBox), GetName("GroupBox")), Control)
            c.Dock = DockStyle.Fill
            PageControl.ActivePage = c
            PageControl.Controls.Add(c)
        End Sub

        Public Function GetName(ByVal typeName As String) As String
            Dim num As Integer = 1
            Dim rs As IReferenceService = CType(GetService(GetType(IReferenceService)), IReferenceService)
            Dim ret As String = typeName + num.ToString

            While Not rs.GetReference(ret) Is Nothing
                num += 1
                ret = typeName + num.ToString
            End While

            Return ret
        End Function

        Private Sub DrawBorder(ByVal graphics As Graphics)
            Dim col As Color
            Dim c As Control = Me.Control
            Dim rect As Rectangle = c.ClientRectangle

            If c.BackColor.GetBrightness < 0.5 Then
                col = ControlPaint.Light(c.BackColor)
            Else
                col = ControlPaint.Dark(c.BackColor)
            End If

            Dim pen As New Pen(col)
            pen.DashStyle = DashStyle.Dash
            rect.Width -= 1
            rect.Height -= 1
            graphics.DrawRectangle(pen, rect)
            pen.Dispose()
        End Sub

        Protected Overrides Sub OnPaintAdornments(ByVal pe As PaintEventArgs)
            Dim p As Panel = CType(MyBase.Component, Panel)

            If p.BorderStyle = BorderStyle.None Then
                DrawBorder(pe.Graphics)
            End If

            MyBase.OnPaintAdornments(pe)
        End Sub
    End Class
End Namespace