Imports System.ComponentModel

Imports Stax
Imports Stax.AppFramework
Imports Stax.UI

Public Class CmdlScriptControl
    Inherits UserControl

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

        MyBase.Dispose(disposing)
    End Sub
    Friend WithEvents TabControl As System.Windows.Forms.TabControl
    Friend WithEvents tpEdit As System.Windows.Forms.TabPage
    Friend WithEvents tpPreview As System.Windows.Forms.TabPage
    Friend WithEvents gb As System.Windows.Forms.GroupBox
    Friend WithEvents tpTools As System.Windows.Forms.TabPage
    Friend WithEvents tbEdit As System.Windows.Forms.TextBox
    Friend WithEvents tbPreview As System.Windows.Forms.TextBox
    Friend WithEvents bHelpFile As System.Windows.Forms.Button
    Friend WithEvents tbHelpFile As System.Windows.Forms.TextBox
    Friend WithEvents lHelpFile As System.Windows.Forms.Label
    Friend WithEvents SmartTagProvider As Stax.UI.Design.SmartTagProvider
    Friend WithEvents bMacros As System.Windows.Forms.Button
    Friend WithEvents bHelp As System.Windows.Forms.Button
    Friend WithEvents bExecute As System.Windows.Forms.Button
    Friend WithEvents BackgroundWorker As System.ComponentModel.BackgroundWorker
    Friend WithEvents TipProvider As Stax.UI.TipProvider
    Friend WithEvents ButtonFlowLayoutPanel As System.Windows.Forms.FlowLayoutPanel

    Private components As System.ComponentModel.IContainer

    <System.Diagnostics.DebuggerStepThrough()> _
    Private Sub InitializeComponent()
        Me.components = New System.ComponentModel.Container
        Me.TabControl = New System.Windows.Forms.TabControl
        Me.tpEdit = New System.Windows.Forms.TabPage
        Me.tbEdit = New System.Windows.Forms.TextBox
        Me.tpPreview = New System.Windows.Forms.TabPage
        Me.tbPreview = New System.Windows.Forms.TextBox
        Me.tpTools = New System.Windows.Forms.TabPage
        Me.bHelpFile = New System.Windows.Forms.Button
        Me.tbHelpFile = New System.Windows.Forms.TextBox
        Me.lHelpFile = New System.Windows.Forms.Label
        Me.gb = New System.Windows.Forms.GroupBox
        Me.ButtonFlowLayoutPanel = New System.Windows.Forms.FlowLayoutPanel
        Me.bHelp = New System.Windows.Forms.Button
        Me.bMacros = New System.Windows.Forms.Button
        Me.bExecute = New System.Windows.Forms.Button
        Me.BackgroundWorker = New System.ComponentModel.BackgroundWorker
        Me.SmartTagProvider = New Stax.UI.Design.SmartTagProvider
        Me.TipProvider = New Stax.UI.TipProvider(Me.components)
        Me.TabControl.SuspendLayout()
        Me.tpEdit.SuspendLayout()
        Me.tpPreview.SuspendLayout()
        Me.tpTools.SuspendLayout()
        Me.gb.SuspendLayout()
        Me.ButtonFlowLayoutPanel.SuspendLayout()
        CType(Me.TipProvider, System.ComponentModel.ISupportInitialize).BeginInit()
        Me.SuspendLayout()
        '
        'TabControl
        '
        Me.TabControl.Anchor = CType((((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
                    Or System.Windows.Forms.AnchorStyles.Left) _
                    Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.TabControl.Controls.Add(Me.tpEdit)
        Me.TabControl.Controls.Add(Me.tpPreview)
        Me.TabControl.Controls.Add(Me.tpTools)
        Me.TabControl.Location = New System.Drawing.Point(6, 19)
        Me.TabControl.Name = "TabControl"
        Me.TabControl.SelectedIndex = 0
        Me.TabControl.Size = New System.Drawing.Size(608, 95)
        Me.TabControl.TabIndex = 0
        '
        'tpEdit
        '
        Me.tpEdit.Controls.Add(Me.tbEdit)
        Me.tpEdit.Location = New System.Drawing.Point(4, 22)
        Me.tpEdit.Name = "tpEdit"
        Me.tpEdit.Padding = New System.Windows.Forms.Padding(3)
        Me.tpEdit.Size = New System.Drawing.Size(600, 69)
        Me.tpEdit.TabIndex = 0
        Me.tpEdit.Text = "Edit"
        Me.tpEdit.UseVisualStyleBackColor = True
        '
        'tbEdit
        '
        Me.tbEdit.AcceptsReturn = True
        Me.tbEdit.AcceptsTab = True
        Me.tbEdit.Anchor = CType((((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
                    Or System.Windows.Forms.AnchorStyles.Left) _
                    Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.tbEdit.Location = New System.Drawing.Point(4, 6)
        Me.tbEdit.Multiline = True
        Me.tbEdit.Name = "tbEdit"
        Me.tbEdit.Size = New System.Drawing.Size(590, 59)
        Me.tbEdit.TabIndex = 0
        '
        'tpPreview
        '
        Me.tpPreview.Controls.Add(Me.tbPreview)
        Me.tpPreview.Location = New System.Drawing.Point(4, 22)
        Me.tpPreview.Name = "tpPreview"
        Me.tpPreview.Padding = New System.Windows.Forms.Padding(3)
        Me.tpPreview.Size = New System.Drawing.Size(600, 69)
        Me.tpPreview.TabIndex = 1
        Me.tpPreview.Text = "Preview"
        Me.tpPreview.UseVisualStyleBackColor = True
        '
        'tbPreview
        '
        Me.tbPreview.Anchor = CType((((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
                    Or System.Windows.Forms.AnchorStyles.Left) _
                    Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.tbPreview.Location = New System.Drawing.Point(4, 6)
        Me.tbPreview.Multiline = True
        Me.tbPreview.Name = "tbPreview"
        Me.tbPreview.ReadOnly = True
        Me.tbPreview.Size = New System.Drawing.Size(590, 57)
        Me.tbPreview.TabIndex = 0
        '
        'tpTools
        '
        Me.tpTools.Controls.Add(Me.bHelpFile)
        Me.tpTools.Controls.Add(Me.tbHelpFile)
        Me.tpTools.Controls.Add(Me.lHelpFile)
        Me.tpTools.Location = New System.Drawing.Point(4, 22)
        Me.tpTools.Name = "tpTools"
        Me.tpTools.Padding = New System.Windows.Forms.Padding(3)
        Me.tpTools.Size = New System.Drawing.Size(600, 69)
        Me.tpTools.TabIndex = 2
        Me.tpTools.Text = "Tools"
        Me.tpTools.UseVisualStyleBackColor = True
        '
        'bHelpFile
        '
        Me.bHelpFile.Anchor = System.Windows.Forms.AnchorStyles.Right
        Me.bHelpFile.Image = Global.StaxRip.My.Resources.Resources.OpenFile16
        Me.bHelpFile.Location = New System.Drawing.Point(567, 21)
        Me.bHelpFile.Name = "bHelpFile"
        Me.bHelpFile.Size = New System.Drawing.Size(24, 24)
        Me.bHelpFile.TabIndex = 2
        Me.bHelpFile.UseVisualStyleBackColor = True
        '
        'tbHelpFile
        '
        Me.tbHelpFile.Anchor = CType((System.Windows.Forms.AnchorStyles.Left Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.tbHelpFile.Location = New System.Drawing.Point(63, 23)
        Me.tbHelpFile.Name = "tbHelpFile"
        Me.tbHelpFile.Size = New System.Drawing.Size(498, 20)
        Me.tbHelpFile.TabIndex = 1
        Me.TipProvider.SetTipName(Me.tbHelpFile, "Parameter Help File")
        Me.TipProvider.SetTipText(Me.tbHelpFile, "Shows the parameter help file in the StaxRip help browser when clicking the 'Para" & _
                "meter Help' button. The file may be a local file, a remote URL and may contain m" & _
                "acros.")
        '
        'lHelpFile
        '
        Me.lHelpFile.Anchor = System.Windows.Forms.AnchorStyles.Left
        Me.lHelpFile.AutoSize = True
        Me.lHelpFile.Location = New System.Drawing.Point(6, 26)
        Me.lHelpFile.Name = "lHelpFile"
        Me.lHelpFile.Size = New System.Drawing.Size(51, 13)
        Me.lHelpFile.TabIndex = 0
        Me.lHelpFile.Text = "Help File:"
        '
        'gb
        '
        Me.gb.Controls.Add(Me.ButtonFlowLayoutPanel)
        Me.gb.Controls.Add(Me.TabControl)
        Me.gb.Dock = System.Windows.Forms.DockStyle.Fill
        Me.gb.Location = New System.Drawing.Point(0, 0)
        Me.gb.Name = "gb"
        Me.gb.Size = New System.Drawing.Size(620, 150)
        Me.gb.TabIndex = 1
        Me.gb.TabStop = False
        Me.gb.Text = "Text"
        '
        'ButtonFlowLayoutPanel
        '
        Me.ButtonFlowLayoutPanel.Anchor = CType((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Left), System.Windows.Forms.AnchorStyles)
        Me.ButtonFlowLayoutPanel.Controls.Add(Me.bHelp)
        Me.ButtonFlowLayoutPanel.Controls.Add(Me.bMacros)
        Me.ButtonFlowLayoutPanel.Controls.Add(Me.bExecute)
        Me.ButtonFlowLayoutPanel.Location = New System.Drawing.Point(2, 116)
        Me.ButtonFlowLayoutPanel.Name = "ButtonFlowLayoutPanel"
        Me.ButtonFlowLayoutPanel.Size = New System.Drawing.Size(349, 31)
        Me.ButtonFlowLayoutPanel.TabIndex = 4
        '
        'bHelp
        '
        Me.bHelp.Anchor = CType((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Left), System.Windows.Forms.AnchorStyles)
        Me.bHelp.Location = New System.Drawing.Point(3, 3)
        Me.bHelp.Name = "bHelp"
        Me.bHelp.Size = New System.Drawing.Size(75, 24)
        Me.bHelp.TabIndex = 2
        Me.bHelp.Text = "Help"
        Me.TipProvider.SetTipText(Me.bHelp, "Shows the help file defined in the tools tab using the StaxRip help browser. The " & _
                "file may be a local file, a remote URL and may contain macros.")
        Me.bHelp.UseVisualStyleBackColor = True
        '
        'bMacros
        '
        Me.bMacros.Anchor = CType((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Left), System.Windows.Forms.AnchorStyles)
        Me.bMacros.Location = New System.Drawing.Point(84, 3)
        Me.bMacros.Name = "bMacros"
        Me.bMacros.Size = New System.Drawing.Size(75, 24)
        Me.bMacros.TabIndex = 3
        Me.bMacros.Text = "Macros..."
        Me.TipProvider.SetTipText(Me.bMacros, "Shows the available macros.")
        Me.bMacros.UseVisualStyleBackColor = True
        '
        'bExecute
        '
        Me.bExecute.Anchor = CType((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Left), System.Windows.Forms.AnchorStyles)
        Me.bExecute.Location = New System.Drawing.Point(165, 3)
        Me.bExecute.Name = "bExecute"
        Me.bExecute.Size = New System.Drawing.Size(75, 24)
        Me.bExecute.TabIndex = 1
        Me.bExecute.Text = "Execute"
        Me.TipProvider.SetTipText(Me.bExecute, "Executes all command lines one by one.")
        Me.bExecute.UseVisualStyleBackColor = True
        '
        'BackgroundWorker
        '
        '
        'CmdlScriptControl
        '
        Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
        Me.Controls.Add(Me.gb)
        Me.Name = "CmdlScriptControl"
        Me.Size = New System.Drawing.Size(620, 150)
        Me.TabControl.ResumeLayout(False)
        Me.tpEdit.ResumeLayout(False)
        Me.tpEdit.PerformLayout()
        Me.tpPreview.ResumeLayout(False)
        Me.tpPreview.PerformLayout()
        Me.tpTools.ResumeLayout(False)
        Me.tpTools.PerformLayout()
        Me.gb.ResumeLayout(False)
        Me.ButtonFlowLayoutPanel.ResumeLayout(False)
        CType(Me.TipProvider, System.ComponentModel.ISupportInitialize).EndInit()
        Me.ResumeLayout(False)

    End Sub

#End Region

    Public Event ParameterFileTextChanged()

    Public Sub New()
        MyBase.New()
        InitializeComponent()
    End Sub

    <Category("Appearance"), DefaultValue(""), Bindable(True), _
    DesignerSerializationVisibility(DesignerSerializationVisibility.Visible), _
    Browsable(True), EditorBrowsable(EditorBrowsableState.Always)> _
    Public Overrides Property Text() As String
        Get
            Return gb.Text
        End Get
        Set(ByVal Value As String)
            gb.Text = Value
        End Set
    End Property

    <Browsable(False), _
    DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)> _
    Public Property CommandLines() As String
        Get
            Return StringHelp.FixBreak(tbEdit.Text)
        End Get
        Set(ByVal value As String)
            tbEdit.Text = value
        End Set
    End Property

    <Browsable(False), _
    DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)> _
    Public Property HelpFile() As String
        Get
            Return tbHelpFile.Text
        End Get
        Set(ByVal Value As String)
            tbHelpFile.Text = Value
        End Set
    End Property

    Private SpecialMacrosFunctionValue As ParamFunctionDelegate(Of String)

    <Browsable(False), _
    DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)> _
    Public Property SpecialMacrosFunction() As ParamFunctionDelegate(Of String)
        Get
            Return SpecialMacrosFunctionValue
        End Get
        Set(ByVal value As ParamFunctionDelegate(Of String))
            SpecialMacrosFunctionValue = value
        End Set
    End Property

    Private SpecialMacrosHelpValue As StringPairList

    <Browsable(False), _
    DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)> _
    Public Property SpecialMacrosHelp() As StringPairList
        Get
            Return SpecialMacrosHelpValue
        End Get
        Set(ByVal Value As StringPairList)
            SpecialMacrosHelpValue = Value
        End Set
    End Property

    Private AutoWrapValue As Boolean = True

    <Browsable(False), _
    DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)> _
    Public Property AutoWrap() As Boolean
        Get
            Return AutoWrapValue
        End Get
        Set(ByVal Value As Boolean)
            AutoWrapValue = Value
        End Set
    End Property

    Public Sub SetCmdlDefaults()
        AutoWrap = True
        bExecute.Visible = True
        TipProvider.SetTip(tbEdit, "Edit", "Allows to edit the command lines. It's possible to enter multible command lines separated with a new line.")
        TipProvider.SetTip(tbPreview, "Preview", "Previews the command lines with solved macros.")
    End Sub

    Public Sub SetScriptDefaults()
        AutoWrap = False
        ButtonFlowLayoutPanel.Controls.Remove(bExecute)
        bExecute.Dispose()
        TipProvider.SetTip(tbEdit, "Edit", "Allows to edit the script.")
        TipProvider.SetTip(tbPreview, "Preview", "Previews the script with solved macros.")
    End Sub

    Public Sub RemoveTools()
        ButtonFlowLayoutPanel.Controls.Remove(bHelp)
        bHelp.Dispose()

        TabControl.TabPages.Remove(tpTools)
        tpTools.Dispose()

        tbHelpFile.Visible = False
    End Sub

    Private Sub tbEdit_TextChanged(ByVal sender As Object, ByVal e As EventArgs) Handles tbEdit.TextChanged
        If Not SpecialMacrosFunction Is Nothing Then
            tbPreview.Text = SpecialMacrosFunction.Invoke(tbEdit.Text)
        Else
            tbPreview.Text = Macro.Solve(tbEdit.Text)
        End If

        UpdateWrapMode(tbEdit)
    End Sub

    Private Sub bHelpFile_Click(ByVal sender As Object, ByVal e As EventArgs) Handles bHelpFile.Click
        Using f As New OpenFileDialog
            f.Filter = "*.htm;*.txt|*.htm;*.txt|All Files|*.*"

            If f.ShowDialog = DialogResult.OK Then
                tbHelpFile.Text = f.FileName
            End If
        End Using
    End Sub

    Private Sub tbHelpFile_DoubleClick(ByVal sender As Object, ByVal e As EventArgs) Handles tbHelpFile.DoubleClick
        bHelpFile.PerformClick()
    End Sub

    Private Sub bHelp_Click(ByVal sender As Object, ByVal e As EventArgs) Handles bHelp.Click
        If tbHelpFile.Text = "" Then
            TabControl.SelectedTab = tpTools
            Msg.Show("Please select a help file first.", MsgIcon.Warning)
        Else
            g.MainForm.OpenHelpURL(Macro.Solve(tbHelpFile.Text))
        End If
    End Sub

    Private Sub BackgroundWorker_DoWork(ByVal sender As Object, ByVal e As DoWorkEventArgs) Handles BackgroundWorker.DoWork
        For Each i As String In StringHelp.SplitLines(tbPreview.Text)
            Dim pw As New ProcessWrapper
            pw.CommandLine = i
            pw.Wait = True
            pw.Start()
        Next
    End Sub

    Private Sub bMacros_Click(ByVal sender As Object, ByVal e As EventArgs) Handles bMacros.Click
        Macro.ShowHelp(SpecialMacrosHelp)
    End Sub

    Private Sub tbPreview_TextChanged(ByVal sender As Object, ByVal e As EventArgs) Handles tbPreview.TextChanged
        UpdateWrapMode(tbPreview)
    End Sub

    Private Sub UpdateWrapMode(ByVal tb As TextBox)
        If AutoWrap Then
            If tb.Text.Contains(vbCrLf) Then
                tb.WordWrap = False
                tb.ScrollBars = ScrollBars.Both
            Else
                tb.WordWrap = True
                tb.ScrollBars = ScrollBars.Vertical
            End If
        End If
    End Sub

    Private Sub tbParameterFile_TextChanged(ByVal sender As Object, ByVal e As EventArgs) Handles tbHelpFile.TextChanged
        RaiseEvent ParameterFileTextChanged()
    End Sub

    Private Sub bExecute_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bExecute.Click
        BackgroundWorker.RunWorkerAsync()
    End Sub
End Class