#Region "LICENSE"
'
' Copyright (c) Microncode.com
' 
' This source is subject to the Microsoft Public License. 
' See https://opensource.org/licenses/ms-pl.html.
' All other rights reserved. 
' 
' THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
' EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED 
' WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE. 
'
#End Region

''' <summary>
''' The main example of using the CSffmpeg libraries.
''' </summary>
Public Class frmMain
    Inherits Form

    'Create global object so we can stop / pause globally
    Private _CSFFmpeg As CSFFmpeg.CSFFmpeg

    ''' <summary>
    ''' Form_Load
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    Private Sub frmMain_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    End Sub

    ''' <summary>
    ''' Get all the information about the source media file in a new window.
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    Private Sub cmdMediaProperties_Click(sender As Object, e As EventArgs) Handles cmdMediaProperties.Click
        If Equals(Me.txtSource.Text, "") Then Return
        Dim fMediaInformation As CSFFmpegExample.frmMediaProperties = New CSFFmpegExample.frmMediaProperties()
        fMediaInformation.LoadMediaFile(Me.txtSource.Text)
        fMediaInformation.ShowDialog(Me)
    End Sub

    ''' <summary>
    ''' Open media file and get the information.
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    Private Async Sub cmdOpenMedia_Click(sender As Object, e As EventArgs) Handles cmdOpenMedia.Click

        Try
            Dim openFileDialog1 As OpenFileDialog = New OpenFileDialog()

            If openFileDialog1.ShowDialog() <> DialogResult.OK Then
                Return
            End If

            Me.txtSource.Text = openFileDialog1.FileName

            'Open the source media file and get the details to the mediaProperties object

            'Create a new instance of the library with the direction of the ffmpeg libraries

            Dim LibsPath As String = ""

            'If the LibsPath is empty or "" the library will search for
            'the ffmpeg libraries under the path:
            '[AssemblyDirectory]\CSPlugins\FFmpeg\[x86|x64]\

            _CSFFmpeg = New CSFFmpeg.CSFFmpeg("UserName", "RegKey", LibsPath)

            'Open the media file and get the properties of the media
            Dim mediaProperties As CSMediaProperties.MediaProperties
            mediaProperties = Await _CSFFmpeg.Open(Me.txtSource.Text)

#Region "GetID3Tags"

            If mediaProperties.Format Is Nothing Then Return
            If mediaProperties.Format.Tags.FirstOrDefault(Function(tag) Equals(tag.Key, "title")) IsNot Nothing Then Debug.WriteLine(Enumerable.FirstOrDefault(mediaProperties.Format.Tags, Function(tag) Equals(tag.Key, "title")).Value)
            If mediaProperties.Format.Tags.FirstOrDefault(Function(tag) Equals(tag.Key, "comment")) IsNot Nothing Then Debug.WriteLine(Enumerable.FirstOrDefault(mediaProperties.Format.Tags, Function(tag) Equals(tag.Key, "comment")).Value)
            If mediaProperties.Format.Tags.FirstOrDefault(Function(tag) Equals(tag.Key, "artist")) IsNot Nothing Then Debug.WriteLine(Enumerable.FirstOrDefault(mediaProperties.Format.Tags, Function(tag) Equals(tag.Key, "artist")).Value)
            If mediaProperties.Format.Tags.FirstOrDefault(Function(tag) Equals(tag.Key, "album")) IsNot Nothing Then Debug.WriteLine(Enumerable.FirstOrDefault(mediaProperties.Format.Tags, Function(tag) Equals(tag.Key, "album")).Value)
            If mediaProperties.Format.Tags.FirstOrDefault(Function(tag) Equals(tag.Key, "date")) IsNot Nothing Then Debug.WriteLine(Enumerable.FirstOrDefault(mediaProperties.Format.Tags, Function(tag) Equals(tag.Key, "date")).Value)

#End Region

            'Dispose the library
            _CSFFmpeg.Dispose()
            _CSFFmpeg = Nothing
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        Finally
        End Try

    End Sub

    ''' <summary>
    ''' Run the operation.
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    Private Sub cmdRun_Click(sender As Object, e As EventArgs) Handles cmdRun.Click
        Try
            'Create a new instance of the library with the direction of the ffmpeg libraries

            Dim LibsPath As String = ""

            'If the LibsPath is empty or "" the library will search for
            'the ffmpeg libraries under the path:
            '[AssemblyDirectory]\CSPlugins\FFmpeg\[x86|x64]\

            _CSFFmpeg = New CSFFmpeg.CSFFmpeg("UserName", "RegKey", LibsPath)

            'Set the arguments
            _CSFFmpeg.ArgsDirect = Me.txtArguments.Text

            'Events
#Region "Events"

            'On ffmpeg output event
            AddHandler _CSFFmpeg.OperationOutput, Sub(o, s)
                                                      'Display in the results in the text box
                                                      Me.txtResults.AppendText(s)
                                                      Me.txtResults.AppendText(Environment.NewLine)
                                                      Me.txtResults.SelectionStart = Me.txtResults.Text.Length
                                                      Me.txtResults.ScrollToCaret()
                                                      Me.txtResults.Refresh()
                                                  End Sub


            'On start event
            AddHandler _CSFFmpeg.OperationStart, Sub(o) Me.lblStatus.Text = "Start"

            'On progress event
            AddHandler _CSFFmpeg.OperationProgress, Sub(o, s) Me.progressBar1.Value = s

            'Done
            AddHandler _CSFFmpeg.OperationDone, Sub(o)
                                                    Me.Invoke(New Action(Sub() Me.lblStatus.Text = "Done"))
                                                End Sub


#End Region

            'Run the ffmpeg from the program
            _CSFFmpeg.Run()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        Finally
        End Try
    End Sub

    ''' <summary>
    ''' Run the operation async.
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    Private Async Sub cmdRunAsync_Click(sender As Object, e As EventArgs) Handles cmdRunAsync.Click
        Try
            'Create a new instance of the library with the direction of the ffmpeg libraries

            Dim LibsPath As String = ""

            'If the LibsPath is empty or "" the library will search for
            'the ffmpeg libraries under the path:
            '[AssemblyDirectory]\CSPlugins\FFmpeg\[x86|x64]\

            _CSFFmpeg = New CSFFmpeg.CSFFmpeg("UserName", "RegKey", LibsPath)

            'Set the source file
            _CSFFmpeg.ArgsDirect = Me.txtArguments.Text

            'Run async and get the results. This technique is not recommended for long operation.
            Dim results As String = Await _CSFFmpeg.RunAsync()

            'Display in the results text box
            Me.txtResults.Text = results

            'Clean
            _CSFFmpeg.Dispose()
            _CSFFmpeg = Nothing
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        Finally
        End Try
    End Sub

    ''' <summary>
    ''' Stop.
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    Private Sub cmdStop_Click(sender As Object, e As EventArgs) Handles cmdStop.Click
        Try
            _CSFFmpeg.Abort()
        Catch
        End Try
    End Sub

    ''' <summary>
    ''' Pause.
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    Private Sub cmdPause_Click(sender As Object, e As EventArgs) Handles cmdPause.Click
        Try
            _CSFFmpeg.Pause()
        Catch
        End Try
    End Sub

    ''' <summary>
    ''' Resume.
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    Private Sub cmdResume_Click(sender As Object, e As EventArgs) Handles cmdResume.Click
        Try
            _CSFFmpeg.Resume()
        Catch
        End Try
    End Sub

    ''' <summary>
    ''' Order a license.
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    Private Sub cmdOrderLicense_Click(sender As Object, e As EventArgs) Handles cmdOrderLicense.Click
        ' In order to get your own UserName and RegKey and to distribute this library
        ' with your own projects for commercial or any other purpose, please order a license at:
        ' https://www.microncode.com/developers/cs-ffmpeg/?cmd=order

        Process.Start("https://www.microncode.com/developers/cs-ffmpeg/?cmd=order")
    End Sub
End Class
