Imports System.IO
Imports Stax
Imports Stax.UI
Imports System.Text.RegularExpressions
Imports System.Xml
Imports System.Text

Namespace AppFramework
    Public Class HelpForm
        Inherits Form

#Region " Windows Form Designer generated code "
        <System.Diagnostics.DebuggerNonUserCode()> _
        Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
            If disposing AndAlso components IsNot Nothing Then
                components.Dispose()
            End If
            MyBase.Dispose(disposing)
        End Sub

        'Required by the Windows Form Designer
        Private components As System.ComponentModel.IContainer

        Friend WithEvents Browser As System.Windows.Forms.WebBrowser
        Friend WithEvents tsbBack As System.Windows.Forms.ToolStripButton
        Friend WithEvents SmartTagProvider As Stax.UI.Design.SmartTagProvider
        Friend WithEvents ToolStrip As System.Windows.Forms.ToolStrip
        <System.Diagnostics.DebuggerStepThrough()> _
        Private Sub InitializeComponent()
            Me.Browser = New System.Windows.Forms.WebBrowser
            Me.ToolStrip = New System.Windows.Forms.ToolStrip
            Me.tsbBack = New System.Windows.Forms.ToolStripButton
            Me.SmartTagProvider = New Stax.UI.Design.SmartTagProvider
            Me.ToolStrip.SuspendLayout()
            Me.SuspendLayout()
            '
            'Browser
            '
            Me.Browser.Dock = System.Windows.Forms.DockStyle.Fill
            Me.Browser.Location = New System.Drawing.Point(0, 25)
            Me.Browser.Name = "Browser"
            Me.Browser.Size = New System.Drawing.Size(694, 474)
            Me.Browser.TabIndex = 0
            '
            'ToolStrip
            '
            Me.ToolStrip.GripStyle = System.Windows.Forms.ToolStripGripStyle.Hidden
            Me.ToolStrip.Items.AddRange(New System.Windows.Forms.ToolStripItem() {Me.tsbBack})
            Me.ToolStrip.Location = New System.Drawing.Point(0, 0)
            Me.ToolStrip.Name = "ToolStrip"
            Me.ToolStrip.ShowItemToolTips = False
            Me.ToolStrip.Size = New System.Drawing.Size(694, 25)
            Me.ToolStrip.TabIndex = 1
            Me.ToolStrip.Text = "ToolStrip"
            '
            'tsbBack
            '
            Me.tsbBack.Image = Global.Stax.My.Resources.Resources.Left16
            Me.tsbBack.ImageTransparentColor = System.Drawing.Color.Magenta
            Me.tsbBack.Name = "tsbBack"
            Me.tsbBack.Size = New System.Drawing.Size(49, 22)
            Me.tsbBack.Text = "Back"
            '
            'HelpForm
            '
            Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
            Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
            Me.ClientSize = New System.Drawing.Size(694, 499)
            Me.Controls.Add(Me.Browser)
            Me.Controls.Add(Me.ToolStrip)
            Me.KeyPreview = True
            Me.Name = "HelpForm"
            Me.StartPosition = System.Windows.Forms.FormStartPosition.Manual
            Me.ToolStrip.ResumeLayout(False)
            Me.ToolStrip.PerformLayout()
            Me.ResumeLayout(False)
            Me.PerformLayout()

        End Sub
#End Region

        Public Shared ButtonsFunction As FunctionDelegate(Of List(Of ToolStripButton))
        Public Shared AutoGenPath As String
        Public Shared InfoFilePath As String

        Public Sub New()
            InitializeComponent()

            For Each i As ToolStripButton In ButtonsFunction()
                ToolStrip.Items.Add(i)
                AddHandler i.Click, AddressOf ButtonClick
            Next

            Icon = StaxApplication.Icon
            StaxApplication.WindowPositions.Restore(Me, WindowPositions.Mode.Position Or WindowPositions.Mode.WindowState Or WindowPositions.Mode.Size)
        End Sub

        Public Sub New(ByVal path As String)
            Me.New()
            Browser.Navigate(path)
        End Sub

        Private Sub ButtonClick(ByVal sender As Object, ByVal e As EventArgs)
            Dim b As ToolStripButton = CType(sender, ToolStripButton)
            Text = b.Text
            Browser.Navigate(b.Tag.ToString)
        End Sub

        Private DocumentValue As HelpDocument

        Public Property Document() As HelpDocument
            Get
                If DocumentValue Is Nothing Then
                    DocumentValue = New HelpDocument(AutoGenPath)
                End If

                Return DocumentValue
            End Get
            Set(ByVal Value As HelpDocument)
                DocumentValue = Value
            End Set
        End Property

        Public Overloads Shared Sub ShowDialog(ByVal heading As String, ByVal tips As StringPairList)
            ShowDialog(heading, tips, Nothing)
        End Sub

        Public Overloads Shared Sub ShowDialog(ByVal heading As String, ByVal tips As StringPairList, ByVal summary As String)
            Dim f As New HelpForm()
            f.Document.WriteStart(heading)

            If Not summary Is Nothing Then
                f.Document.WriteParagraph(summary)
            End If

            f.Document.WriteTips(tips)
            f.Show()
        End Sub

        Private Sub HelpForm_FormClosed(ByVal sender As Object, ByVal e As FormClosedEventArgs) Handles MyBase.FormClosed
            Dispose()
        End Sub

        Private Sub Browser_Navigated(ByVal sender As Object, ByVal e As WebBrowserNavigatedEventArgs) Handles Browser.Navigated
            For Each i As ToolStripItem In ToolStrip.Items
                If TypeOf i Is ToolStripButton Then
                    Dim b As ToolStripButton = DirectCast(i, ToolStripButton)

                    If Not b.Tag Is Nothing Then
                        If Browser.Url.LocalPath = i.Tag.ToString Then
                            b.Checked = True
                        Else
                            b.Checked = False
                        End If
                    End If
                End If
            Next

            Text = Browser.DocumentTitle
        End Sub

        Private Sub tsbBack_Click(ByVal sender As Object, ByVal e As EventArgs) Handles tsbBack.Click
            Browser.GoBack()
        End Sub

        Public Shadows Sub Show()
            If Not DocumentValue Is Nothing Then
                DocumentValue.WriteDocument(Browser)
            End If

            MyBase.Show()
        End Sub

        Private Sub HelpForm_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
            StaxApplication.WindowPositions.Save(Me, WindowPositions.Mode.Position Or WindowPositions.Mode.WindowState Or WindowPositions.Mode.Size)
        End Sub
    End Class
End Namespace