Imports Stax
Imports Stax.UI
Imports Stax.AppFramework

Public Class CommandLineAudioSettingsForm
    Inherits ExtendedForm

#Region " Designer "
    <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
    Friend WithEvents CmdlScriptControl As StaxRip.CmdlScriptControl
    Friend WithEvents tbStreamName As System.Windows.Forms.TextBox
    Friend WithEvents lStreamName As System.Windows.Forms.Label

    Private components As System.ComponentModel.IContainer

    <System.Diagnostics.DebuggerStepThrough()> _
    Private Sub InitializeComponent()
        Me.components = New System.ComponentModel.Container
        Me.bCancel = New System.Windows.Forms.Button
        Me.bOK = New System.Windows.Forms.Button
        Me.tbInput = New System.Windows.Forms.TextBox
        Me.Label4 = New System.Windows.Forms.Label
        Me.bHelp = New System.Windows.Forms.Button
        Me.lOutput = New System.Windows.Forms.Label
        Me.lBitrate = New System.Windows.Forms.Label
        Me.tbBitrate = New System.Windows.Forms.TextBox
        Me.lLanguage = New System.Windows.Forms.Label
        Me.lDelay = New System.Windows.Forms.Label
        Me.cbLanguage = New System.Windows.Forms.ComboBox
        Me.tbDelay = New System.Windows.Forms.TextBox
        Me.SmartTagProvider = New Stax.UI.Design.SmartTagProvider
        Me.TipProvider = New Stax.UI.TipProvider(Me.components)
        Me.cbType = New System.Windows.Forms.ComboBox
        Me.tbStreamName = New System.Windows.Forms.TextBox
        Me.ValidationProvider = New Stax.UI.ValidationProvider
        Me.CmdlScriptControl = New StaxRip.CmdlScriptControl
        Me.lStreamName = New System.Windows.Forms.Label
        CType(Me.TipProvider, System.ComponentModel.ISupportInitialize).BeginInit()
        Me.SuspendLayout()
        '
        'bCancel
        '
        Me.bCancel.Anchor = CType((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.bCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel
        Me.bCancel.Location = New System.Drawing.Point(525, 229)
        Me.bCancel.Name = "bCancel"
        Me.bCancel.Size = New System.Drawing.Size(75, 24)
        Me.bCancel.TabIndex = 8
        Me.bCancel.Text = "Cancel"
        '
        'bOK
        '
        Me.bOK.Anchor = CType((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.bOK.DialogResult = System.Windows.Forms.DialogResult.OK
        Me.bOK.Location = New System.Drawing.Point(444, 229)
        Me.bOK.Name = "bOK"
        Me.bOK.Size = New System.Drawing.Size(75, 24)
        Me.bOK.TabIndex = 7
        Me.bOK.Text = "OK"
        '
        'tbInput
        '
        Me.tbInput.Location = New System.Drawing.Point(178, 26)
        Me.tbInput.Name = "tbInput"
        Me.ValidationProvider.SetPattern(Me.tbInput, "^[a-z0-9, ]+$")
        Me.tbInput.Size = New System.Drawing.Size(160, 20)
        Me.tbInput.TabIndex = 1
        Me.TipProvider.SetTipName(Me.tbInput, "Input")
        Me.TipProvider.SetTipText(Me.tbInput, "The file types the application accepts as input.")
        '
        'Label4
        '
        Me.Label4.AutoSize = True
        Me.Label4.Location = New System.Drawing.Point(175, 10)
        Me.Label4.Name = "Label4"
        Me.Label4.Size = New System.Drawing.Size(34, 13)
        Me.Label4.TabIndex = 21
        Me.Label4.Text = "Input:"
        '
        'bHelp
        '
        Me.bHelp.Anchor = CType((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.bHelp.Location = New System.Drawing.Point(606, 229)
        Me.bHelp.Name = "bHelp"
        Me.bHelp.Size = New System.Drawing.Size(75, 24)
        Me.bHelp.TabIndex = 9
        Me.bHelp.Text = "Help"
        Me.bHelp.TextImageRelation = System.Windows.Forms.TextImageRelation.ImageBeforeText
        '
        'lOutput
        '
        Me.lOutput.AutoSize = True
        Me.lOutput.Location = New System.Drawing.Point(341, 11)
        Me.lOutput.Name = "lOutput"
        Me.lOutput.Size = New System.Drawing.Size(69, 13)
        Me.lOutput.TabIndex = 26
        Me.lOutput.Text = "Output Type:"
        '
        'lBitrate
        '
        Me.lBitrate.AutoSize = True
        Me.lBitrate.Location = New System.Drawing.Point(462, 10)
        Me.lBitrate.Name = "lBitrate"
        Me.lBitrate.Size = New System.Drawing.Size(40, 13)
        Me.lBitrate.TabIndex = 28
        Me.lBitrate.Text = "Bitrate:"
        '
        'tbBitrate
        '
        Me.tbBitrate.Location = New System.Drawing.Point(462, 26)
        Me.tbBitrate.Name = "tbBitrate"
        Me.ValidationProvider.SetPattern(Me.tbBitrate, "^[1-9]+\d*$")
        Me.tbBitrate.Size = New System.Drawing.Size(40, 20)
        Me.tbBitrate.TabIndex = 3
        Me.tbBitrate.TextAlign = System.Windows.Forms.HorizontalAlignment.Center
        Me.TipProvider.SetTipName(Me.tbBitrate, "Bitrate")
        Me.TipProvider.SetTipText(Me.tbBitrate, "The targeted bitrate of the output file.")
        '
        'lLanguage
        '
        Me.lLanguage.AutoSize = True
        Me.lLanguage.Location = New System.Drawing.Point(508, 10)
        Me.lLanguage.Name = "lLanguage"
        Me.lLanguage.Size = New System.Drawing.Size(58, 13)
        Me.lLanguage.TabIndex = 30
        Me.lLanguage.Text = "Language:"
        '
        'lDelay
        '
        Me.lDelay.AutoSize = True
        Me.lDelay.Location = New System.Drawing.Point(638, 11)
        Me.lDelay.Name = "lDelay"
        Me.lDelay.Size = New System.Drawing.Size(37, 13)
        Me.lDelay.TabIndex = 31
        Me.lDelay.Text = "Delay:"
        '
        'cbLanguage
        '
        Me.cbLanguage.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList
        Me.cbLanguage.FormattingEnabled = True
        Me.cbLanguage.Location = New System.Drawing.Point(508, 26)
        Me.cbLanguage.MaxDropDownItems = 20
        Me.cbLanguage.Name = "cbLanguage"
        Me.cbLanguage.Size = New System.Drawing.Size(127, 21)
        Me.cbLanguage.TabIndex = 4
        Me.TipProvider.SetTipName(Me.cbLanguage, "Language")
        Me.TipProvider.SetTipText(Me.cbLanguage, "Display language of the track.")
        '
        'tbDelay
        '
        Me.tbDelay.Location = New System.Drawing.Point(641, 27)
        Me.tbDelay.Name = "tbDelay"
        Me.ValidationProvider.SetPattern(Me.tbDelay, "^(-?[1-9]+\d*|-?0)$")
        Me.tbDelay.Size = New System.Drawing.Size(40, 20)
        Me.tbDelay.TabIndex = 5
        Me.tbDelay.TextAlign = System.Windows.Forms.HorizontalAlignment.Center
        Me.TipProvider.SetTipName(Me.tbDelay, "Delay")
        Me.TipProvider.SetTipText(Me.tbDelay, "Audio delay to be fixed.")
        '
        'cbType
        '
        Me.cbType.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList
        Me.cbType.FormattingEnabled = True
        Me.cbType.Location = New System.Drawing.Point(344, 26)
        Me.cbType.MaxDropDownItems = 20
        Me.cbType.Name = "cbType"
        Me.cbType.Size = New System.Drawing.Size(112, 21)
        Me.cbType.Sorted = True
        Me.cbType.TabIndex = 2
        Me.TipProvider.SetTipName(Me.cbType, "Output Type")
        Me.TipProvider.SetTipText(Me.cbType, "Type of the output file.")
        '
        'tbStreamName
        '
        Me.tbStreamName.Location = New System.Drawing.Point(12, 26)
        Me.tbStreamName.Name = "tbStreamName"
        Me.tbStreamName.Size = New System.Drawing.Size(160, 20)
        Me.tbStreamName.TabIndex = 0
        Me.TipProvider.SetTipName(Me.tbStreamName, "Stream Name")
        Me.TipProvider.SetTipText(Me.tbStreamName, "Stream name used by the muxer. The stream name may contain macros.")
        '
        'CmdlScriptControl
        '
        Me.CmdlScriptControl.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _
                    Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.CmdlScriptControl.Location = New System.Drawing.Point(12, 53)
        Me.CmdlScriptControl.Name = "CmdlScriptControl"
        Me.CmdlScriptControl.Size = New System.Drawing.Size(669, 170)
        Me.CmdlScriptControl.TabIndex = 6
        Me.CmdlScriptControl.Text = "Command Lines"
        '
        'lStreamName
        '
        Me.lStreamName.AutoSize = True
        Me.lStreamName.Location = New System.Drawing.Point(9, 10)
        Me.lStreamName.Name = "lStreamName"
        Me.lStreamName.Size = New System.Drawing.Size(74, 13)
        Me.lStreamName.TabIndex = 35
        Me.lStreamName.Text = "Stream Name:"
        '
        'CommandLineAudioSettingsForm
        '
        Me.AcceptButton = Me.bOK
        Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
        Me.CancelButton = Me.bCancel
        Me.ClientSize = New System.Drawing.Size(693, 265)
        Me.Controls.Add(Me.lStreamName)
        Me.Controls.Add(Me.tbStreamName)
        Me.Controls.Add(Me.CmdlScriptControl)
        Me.Controls.Add(Me.cbType)
        Me.Controls.Add(Me.lLanguage)
        Me.Controls.Add(Me.cbLanguage)
        Me.Controls.Add(Me.tbDelay)
        Me.Controls.Add(Me.lDelay)
        Me.Controls.Add(Me.tbBitrate)
        Me.Controls.Add(Me.lBitrate)
        Me.Controls.Add(Me.lOutput)
        Me.Controls.Add(Me.bHelp)
        Me.Controls.Add(Me.tbInput)
        Me.Controls.Add(Me.Label4)
        Me.Controls.Add(Me.bCancel)
        Me.Controls.Add(Me.bOK)
        Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog
        Me.HelpButton = True
        Me.MaximizeBox = False
        Me.MinimizeBox = False
        Me.Name = "CommandLineAudioSettingsForm"
        Me.ShowIcon = False
        Me.ShowInTaskbar = False
        Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent
        Me.Text = "Audio Settings"
        CType(Me.TipProvider, System.ComponentModel.ISupportInitialize).EndInit()
        Me.ResumeLayout(False)
        Me.PerformLayout()

    End Sub
    Friend WithEvents bCancel As System.Windows.Forms.Button
    Friend WithEvents bOK As System.Windows.Forms.Button
    Friend WithEvents tbInput As System.Windows.Forms.TextBox
    Friend WithEvents Label4 As System.Windows.Forms.Label
    Friend WithEvents bHelp As System.Windows.Forms.Button
    Friend WithEvents lOutput As System.Windows.Forms.Label
    Friend WithEvents lBitrate As System.Windows.Forms.Label
    Friend WithEvents tbBitrate As System.Windows.Forms.TextBox
    Friend WithEvents lLanguage As System.Windows.Forms.Label
    Friend WithEvents lDelay As System.Windows.Forms.Label
    Friend WithEvents cbLanguage As System.Windows.Forms.ComboBox
    Friend WithEvents tbDelay As System.Windows.Forms.TextBox
    Friend WithEvents TipProvider As Stax.UI.TipProvider
    Friend WithEvents ValidationProvider As Stax.UI.ValidationProvider
    Friend WithEvents SmartTagProvider As Stax.UI.Design.SmartTagProvider
    Friend WithEvents cbType As System.Windows.Forms.ComboBox
#End Region

    Private Profile As CommandLineAudioProfile
    Private AudioSettings As AudioSettings
    Private SpecialMacrosHelp As New StringPairList

    Public Sub New(ByVal profile As CommandLineAudioProfile, ByVal audioSettings As AudioSettings)
        InitializeComponent()

        Me.Profile = profile
        Me.AudioSettings = audioSettings

        SpecialMacrosHelp.Add("%input%", "Audio source file")
        SpecialMacrosHelp.Add("%output%", "Audio target File")
        SpecialMacrosHelp.Add("%delay%", "Audio delay")
        SpecialMacrosHelp.Add("%language_native%", "Native language name")
        SpecialMacrosHelp.Add("%language_english%", "English language name")

        CmdlScriptControl.SpecialMacrosFunction = AddressOf profile.SolveMacros
        CmdlScriptControl.SpecialMacrosHelp = SpecialMacrosHelp

        ListBag(Of StreamType).PopulateList(cbType)
        ListBag(Of StreamType).SelectItem(cbType, profile.StreamType)

        tbInput.Text = String.Join(", ", profile.SupportedInputFileTypes.ToArray)
        CmdlScriptControl.CommandLines = profile.CommandLines
        CmdlScriptControl.HelpFile = profile.HelpFile

        tbStreamName.Text = profile.StreamName
        tbBitrate.Text = profile.Bitrate.ToString

        cbLanguage.Items.AddRange(Language.Languages)

        For i As Integer = 0 To cbLanguage.Items.Count - 1
            If DirectCast(cbLanguage.Items(i), Language).ToString = audioSettings.Language.ToString Then
                cbLanguage.SelectedIndex = i
                Exit For
            End If
        Next

        tbDelay.Text = audioSettings.Delay.ToString
    End Sub

    Private Sub bOK_Click(ByVal sender As Object, ByVal e As EventArgs) Handles bOK.Click
        Profile.SupportedInputFileTypes = New List(Of String)(StringHelp.Split(tbInput.Text.Replace(" ", ""), ","))
        Profile.StreamType = ListBag(Of StreamType).GetValue(cbType)
        Profile.CommandLines = CmdlScriptControl.CommandLines
        Profile.Bitrate = CInt(tbBitrate.Text)
        Profile.HelpFile = CmdlScriptControl.HelpFile
        Profile.StreamName = tbStreamName.Text

        AudioSettings.Language = DirectCast(cbLanguage.SelectedItem, Language)
        AudioSettings.Delay = CInt(tbDelay.Text)
    End Sub

    Private Sub bHelp_Click(ByVal sender As Object, ByVal e As EventArgs) Handles bHelp.Click
        Dim f As New HelpForm()
        f.Document.WriteStart(Text)
        f.Document.WriteParagraph("The command line audio settings define a command line application to transform a audio file into a new format.")
        f.Document.WriteParagraph(Strings.ProjectAudioSettingsHelp)
        f.Document.WriteTips(TipProvider.GetTips, CmdlScriptControl.TipProvider.GetTips)
        f.Document.WriteTable("Special Macros", SpecialMacrosHelp)
        f.Document.WriteTable("Standard Macros", Macro.GetTips)
        f.Show()
    End Sub

    Private Sub CommandLineAudioSettingsForm_ActivatedFirstTime() Handles Me.ActivatedFirstTime
        If Not s.StringList.Contains(Me.GetType.FullName) Then
            bHelp.PerformClick()
            s.StringList.Add(Me.GetType.FullName)
        End If
    End Sub
End Class