Imports System.ComponentModel

Public Class frmVideo

    Public Property FrameSize As Size = New Size(640, 480)
    Public Property FrameRate As Integer = 25

    Private project As IProject
    Private WithEvents worker As BackgroundWorker

    Public Sub New(project As IProject)
        InitializeComponent()

        Me.project = project
        Me.worker = New BackgroundWorker()
    End Sub

    Private Sub frmVideo_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Me.txTitle.Text = Me.project.VideoTitle
        Me.txCredits.Text = Me.project.VideoCredits
        Me.txPath.Text = Me.project.VideoPath
    End Sub

    Private Sub frmVideo_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        If worker.IsBusy Then
            MessageBox.Show("Video wird gerade gespeichert ...", "Video exportieren", MessageBoxButtons.OK, MessageBoxIcon.Information)
            e.Cancel = True
        End If
    End Sub

    Private Sub btnLoad_Click(sender As System.Object, e As System.EventArgs) Handles btnLoad.Click
        Dim dlg As New SaveFileDialog With {
            .Filter = "AVI-Video (*.avi)|*.avi|Alle Dateien (*.*)|*.*",
            .RestoreDirectory = True
        }
        If dlg.ShowDialog() = Windows.Forms.DialogResult.Cancel Then Return
        Me.txPath.Text = dlg.FileName
    End Sub

    Private Sub btnSave_Click(sender As System.Object, e As System.EventArgs) Handles btnSave.Click
        Me.project.VideoTitle = Me.txTitle.Text
        Me.project.VideoCredits = Me.txCredits.Text
        If Me.txPath.Text <> "" AndAlso IO.Directory.Exists(IO.Path.GetDirectoryName(Me.txPath.Text)) Then
            Me.project.VideoPath = Me.txPath.Text
            Me.txTitle.Enabled = False
            Me.txCredits.Enabled = False
            Me.btnLoad.Enabled = False
            Me.btnSave.Enabled = False
            Me.worker.RunWorkerAsync()
        Else
            MessageBox.Show("Pfad wurde nicht gefunden!", "Video exportieren", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End If
    End Sub

    Private Sub worker_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles worker.DoWork
        Me.project.SaveVideo(Me.project.VideoPath, Me.FrameSize, Me.FrameRate)
    End Sub

    Private Sub worker_RunWorkerCompleted(sender As Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles worker.RunWorkerCompleted
        Me.txTitle.Enabled = True
        Me.txCredits.Enabled = True
        Me.btnLoad.Enabled = True
        Me.btnSave.Enabled = True
        If Not e.Cancelled And e.Error Is Nothing Then
            MessageBox.Show("Video wurde erfolgreich unter '" & Me.project.VideoPath & "' gespeichert.", "Video exportieren", MessageBoxButtons.OK, MessageBoxIcon.Information)
            Me.Close()
        Else
            MessageBox.Show("Video konnte nicht gespeichert werden!" & vbCrLf & "Stellen Sie sicher, das die Datei nicht mit einem anderen Programm geöffnet wurde.", "Video exportieren", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End If
    End Sub
End Class