Imports System.ComponentModel

Imports Stax

Public Class SubtitleControl
    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 TipProvider As Stax.UI.TipProvider

    Private components As System.ComponentModel.IContainer

    <System.Diagnostics.DebuggerStepThrough()> _
    Private Sub InitializeComponent()
        Me.components = New System.ComponentModel.Container
        Me.clbSubtitles = New System.Windows.Forms.CheckedListBox
        Me.lSourceFile = New System.Windows.Forms.Label
        Me.gbSubtitles = New System.Windows.Forms.GroupBox
        Me.tbSourceFile = New System.Windows.Forms.TextBox
        Me.bSourceFile = New System.Windows.Forms.Button
        Me.SmartTagProvider = New Stax.UI.Design.SmartTagProvider
        Me.TipProvider = New Stax.UI.TipProvider(Me.components)
        Me.gbSubtitles.SuspendLayout()
        CType(Me.TipProvider, System.ComponentModel.ISupportInitialize).BeginInit()
        Me.SuspendLayout()
        '
        'clbSubtitles
        '
        Me.clbSubtitles.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.clbSubtitles.CheckOnClick = True
        Me.clbSubtitles.ColumnWidth = 120
        Me.clbSubtitles.IntegralHeight = False
        Me.clbSubtitles.Location = New System.Drawing.Point(9, 47)
        Me.clbSubtitles.MultiColumn = True
        Me.clbSubtitles.Name = "clbSubtitles"
        Me.clbSubtitles.Size = New System.Drawing.Size(275, 157)
        Me.clbSubtitles.TabIndex = 1
        '
        'lSourceFile
        '
        Me.lSourceFile.AutoSize = True
        Me.lSourceFile.Location = New System.Drawing.Point(6, 22)
        Me.lSourceFile.Name = "lSourceFile"
        Me.lSourceFile.Size = New System.Drawing.Size(63, 13)
        Me.lSourceFile.TabIndex = 2
        Me.lSourceFile.Text = "Source File:"
        '
        'gbSubtitles
        '
        Me.gbSubtitles.Controls.Add(Me.tbSourceFile)
        Me.gbSubtitles.Controls.Add(Me.bSourceFile)
        Me.gbSubtitles.Controls.Add(Me.clbSubtitles)
        Me.gbSubtitles.Controls.Add(Me.lSourceFile)
        Me.gbSubtitles.Dock = System.Windows.Forms.DockStyle.Fill
        Me.gbSubtitles.Location = New System.Drawing.Point(0, 0)
        Me.gbSubtitles.Name = "gbSubtitles"
        Me.gbSubtitles.Size = New System.Drawing.Size(290, 210)
        Me.gbSubtitles.TabIndex = 3
        Me.gbSubtitles.TabStop = False
        Me.gbSubtitles.Text = "Subtitles"
        '
        'tbSourceFile
        '
        Me.tbSourceFile.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _
                    Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.tbSourceFile.Location = New System.Drawing.Point(73, 19)
        Me.tbSourceFile.Name = "tbSourceFile"
        Me.tbSourceFile.Size = New System.Drawing.Size(181, 20)
        Me.tbSourceFile.TabIndex = 4
        Me.TipProvider.SetTipName(Me.tbSourceFile, "Subtitle Source File")
        Me.TipProvider.SetTipText(Me.tbSourceFile, "The subtitle source file can be created using [[http://www.planetdvb.net/dvx/modu" & _
                "les/VSRip_1.0.0.6.zip | VSRip]].")
        '
        'bSourceFile
        '
        Me.bSourceFile.Anchor = CType((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.bSourceFile.Image = Global.StaxRip.My.Resources.Resources.OpenFile16
        Me.bSourceFile.Location = New System.Drawing.Point(260, 17)
        Me.bSourceFile.Name = "bSourceFile"
        Me.bSourceFile.Size = New System.Drawing.Size(24, 24)
        Me.bSourceFile.TabIndex = 3
        Me.bSourceFile.UseVisualStyleBackColor = True
        '
        'SubtitleControl
        '
        Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
        Me.Controls.Add(Me.gbSubtitles)
        Me.Name = "SubtitleControl"
        Me.Size = New System.Drawing.Size(290, 210)
        Me.gbSubtitles.ResumeLayout(False)
        Me.gbSubtitles.PerformLayout()
        CType(Me.TipProvider, System.ComponentModel.ISupportInitialize).EndInit()
        Me.ResumeLayout(False)

    End Sub
    Friend WithEvents clbSubtitles As System.Windows.Forms.CheckedListBox
    Friend WithEvents lSourceFile As System.Windows.Forms.Label
    Friend WithEvents SmartTagProvider As Stax.UI.Design.SmartTagProvider
    Friend WithEvents gbSubtitles As System.Windows.Forms.GroupBox
    Friend WithEvents tbSourceFile As System.Windows.Forms.TextBox
    Friend WithEvents bSourceFile As System.Windows.Forms.Button

#End Region

    Private IsLoading As Boolean

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

    Private IDXValue As IDX

    <DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)> _
    Public Property IDX() As IDX
        Get
            Return IDXValue
        End Get
        Set(ByVal Value As IDX)
            IDXValue = DirectCast(ObjectHelp.GetCopy(Value), IDX)
            LoadIDX(IDXValue)
        End Set
    End Property

    Private Sub LoadIDX(ByVal idx As IDX)
        IsLoading = True
        tbSourceFile.Text = idx.Path
        clbSubtitles.Items.Clear()

        For Each i As Subtitle In idx.Subtitles
            clbSubtitles.Items.Add(i, i.Enabled)
        Next

        IsLoading = False
    End Sub

    Private Sub bSourceFile_Click(ByVal sender As Object, ByVal e As EventArgs) Handles bSourceFile.Click
        Dim d As New OpenFileDialog
        d.Filter = "*.idx|*.idx"

        If File.Exists(tbSourceFile.Text) Then
            d.FileName = tbSourceFile.Text
        End If

        If d.ShowDialog = DialogResult.OK Then
            tbSourceFile.Text = d.FileName
        End If
    End Sub

    Private Sub clbSubtitles_ItemCheck(ByVal sender As Object, ByVal e As ItemCheckEventArgs) Handles clbSubtitles.ItemCheck
        Dim li As Subtitle = DirectCast(clbSubtitles.Items(e.Index), Subtitle)
        li.Enabled = (e.NewValue = CheckState.Checked)
    End Sub

    Private Sub tbSourceFile_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles tbSourceFile.DoubleClick
        bSourceFile.PerformClick()
    End Sub

    Private Sub tbSourceFile_TextChanged(ByVal sender As Object, ByVal e As EventArgs) Handles tbSourceFile.TextChanged
        If Not IsLoading Then
            IDX.Path = tbSourceFile.Text
            LoadIDX(IDX)
        End If
    End Sub
End Class