#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

Imports System
Imports System.Timers
Imports System.Windows.Forms

Public Class frmMain

    Inherits Form

    Private _SliderTimer As Timers.Timer
    Private _mediafile As String = ""
    Private _rotate As Integer = 0
    Private _ScaleX As Integer = 0

    Private Sub frmMain_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        'Init function, must be call before using any functionality of the library.

        'Set the path 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]\

        Me.videoPlayer.Init("UserName", "RegKey", LibsPath)

        'Get the position with the _SliderTimer
        _SliderTimer = New Timers.Timer(200)
        AddHandler _SliderTimer.Elapsed, AddressOf OnTimedEvent
        _SliderTimer.AutoReset = True

    End Sub

    ''' <summary>
    ''' Open the local media file to play.
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    Private Sub cmdOpen_Click(sender As Object, e As EventArgs) Handles cmdOpen.Click
        Dim openFileDialog As OpenFileDialog = New OpenFileDialog()

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

        _mediafile = openFileDialog.FileName
        Me.videoPlayer.Open(New Uri(_mediafile))

    End Sub

    ''' <summary>
    ''' Play.
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    Private Sub cmdPlay_Click(sender As Object, e As EventArgs) Handles cmdPlay.Click
        Me.videoPlayer.Play()
        _SliderTimer.Enabled = True
    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
        Me.videoPlayer.Pause()
    End Sub

    ''' <summary>
    ''' UnPause.
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    Private Sub cmdUnPause_Click(sender As Object, e As EventArgs) Handles cmdUnPause.Click
        Me.videoPlayer.Play()
    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
        Me.videoPlayer.Stop()
        _SliderTimer.Enabled = False
        Me.timelineSlider.Value = 0
    End Sub

    ''' <summary>
    ''' Rotate +90.
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    Private Sub cmdRotate90_Click(sender As Object, e As EventArgs) Handles cmdRotate90.Click
        _rotate += 90
        Me.videoPlayer.Rotate(_rotate)
    End Sub

    ''' <summary>
    ''' Rotate -90.
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    Private Sub cmdRotate90Minus_Click(sender As Object, e As EventArgs) Handles cmdRotate90Minus.Click
        _rotate -= 90
        Me.videoPlayer.Rotate(_rotate)
    End Sub

    ''' <summary>
    ''' Flip.
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    Private Sub cmdFlip_Click(sender As Object, e As EventArgs) Handles cmdFlip.Click
        If _ScaleX < 0 Then
            _ScaleX = 1
        Else
            _ScaleX = -1
        End If

        Me.videoPlayer.Flip(_ScaleX)
    End Sub

    ''' <summary>
    ''' Get the media properties 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

        Dim frmMediaProperties As CSVideoPlayerExampleWinForms.frmMediaProperties = New CSVideoPlayerExampleWinForms.frmMediaProperties()
        frmMediaProperties.GetDetails(_mediafile)
        frmMediaProperties.ShowDialog()

    End Sub

    ''' <summary>
    ''' Play on full screen.
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    Private Sub cmdFullScreen_Click(sender As Object, e As EventArgs) Handles cmdFullScreen.Click
        Me.videoPlayer.BringToFront()
        FormBorderStyle = FormBorderStyle.None
        Me.videoPlayer.Dock = DockStyle.Fill
        WindowState = FormWindowState.Maximized
    End Sub

    ''' <summary>
    ''' Exit the full screen mode on ESC key pressed.
    ''' </summary>
    ''' <param name="keyData"></param>
    ''' <returns></returns>
    Protected Overrides Function ProcessDialogKey(ByVal keyData As Keys) As Boolean
        If ModifierKeys = Keys.None AndAlso keyData = Keys.Escape Then
            WindowState = FormWindowState.Normal
            Me.videoPlayer.Dock = DockStyle.None
            FormBorderStyle = FormBorderStyle.Sizable
        End If

        Return MyBase.ProcessDialogKey(keyData)
    End Function

    ''' <summary>
    ''' Volume.
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    Private Sub volumeSlider_ValueChanged(sender As Object, e As EventArgs) Handles volumeSlider.ValueChanged
        On Error Resume Next

        Me.videoPlayer.Volume(Me.volumeSlider.Value)
    End Sub

    ''' <summary>
    ''' Ratio.
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    Private Sub speedRatioSlider_ValueChanged(sender As Object, e As EventArgs) Handles speedRatioSlider.ValueChanged
        On Error Resume Next

        Me.videoPlayer.SpeedRatio(Me.speedRatioSlider.Value)
    End Sub


#Region "Events"

    ''' <summary>
    ''' Timer event of the slider.
    ''' </summary>
    ''' <param name="source"></param>
    ''' <param name="e"></param>
    Private Sub OnTimedEvent(ByVal source As Object, ByVal e As ElapsedEventArgs)
        Me.Invoke(New Action(Sub() Me.timelineSlider.Value = Convert.ToInt32(Me.videoPlayer.PositionGet().TotalMilliseconds)))
    End Sub

    ''' <summary>
    ''' Stop move the slider when MouseDown.
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    Private Sub timelineSlider_MouseDown(sender As Object, e As MouseEventArgs) Handles timelineSlider.MouseDown
        _SliderTimer.Enabled = False
    End Sub

    ''' <summary>
    ''' Seek to the position when MouseUp.
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    Private Sub timelineSlider_MouseUp(sender As Object, e As MouseEventArgs) Handles timelineSlider.MouseUp
        Dim SliderValue As Integer = CInt(Me.timelineSlider.Value)
        Dim ts As TimeSpan = New TimeSpan(0, 0, 0, 0, SliderValue)
        Me.videoPlayer.PositionSet(ts)
        _SliderTimer.Enabled = True
    End Sub

    ''' <summary>
    ''' Media opened.
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    Private Sub videoPlayer_MediaOpened(sender As Object, e As CSVideoPlayer.MediaOpenedEventArgs) Handles videoPlayer.MediaOpened
        Console.WriteLine(Me.videoPlayer.NaturalDuration().TotalMilliseconds)
        Me.timelineSlider.Maximum = Convert.ToInt32(Me.videoPlayer.NaturalDuration().TotalMilliseconds)
    End Sub

    ''' <summary>
    ''' On MediaChanged.
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    Private Sub videoPlayer_MediaChanged(sender As Object, e As CSVideoPlayer.MediaOpenedEventArgs) Handles videoPlayer.MediaChanged

    End Sub

    ''' <summary>
    ''' MediaChanging.
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    Private Sub videoPlayer_MediaChanging(sender As Object, e As CSVideoPlayer.MediaOpeningEventArgs) Handles videoPlayer.MediaChanging

    End Sub

    ''' <summary>
    ''' MediaClosed.
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    Private Sub videoPlayer_MediaClosed(sender As Object, e As EventArgs) Handles videoPlayer.MediaClosed

    End Sub

    ''' <summary>
    ''' MediaEnded.
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    Private Sub videoPlayer_MediaEnded(sender As Object, e As EventArgs) Handles videoPlayer.MediaEnded

    End Sub

    ''' <summary>
    ''' MediaFailed.
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    Private Sub videoPlayer_MediaFailed(sender As Object, e As CSVideoPlayer.MediaFailedEventArgs) Handles videoPlayer.MediaFailed

    End Sub

    ''' <summary>
    ''' MediaInitializing.
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    Private Sub videoPlayer_MediaInitializing(sender As Object, e As CSVideoPlayer.MediaInitializingEventArgs) Handles videoPlayer.MediaInitializing

    End Sub

    ''' <summary>
    ''' MediaOpening.
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    Private Sub videoPlayer_MediaOpening(sender As Object, e As CSVideoPlayer.MediaOpeningEventArgs) Handles videoPlayer.MediaOpening

    End Sub

    ''' <summary>
    ''' MediaReady.
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    Private Sub videoPlayer_MediaReady(sender As Object, e As EventArgs) Handles videoPlayer.MediaReady

    End Sub

    ''' <summary>
    ''' MediaStateChanged.
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    Private Sub videoPlayer_MediaStateChanged(sender As Object, e As CSVideoPlayer.MediaStateChangedEventArgs) Handles videoPlayer.MediaStateChanged

    End Sub

#End Region

    ''' <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-video-player/?cmd=order

        System.Diagnostics.Process.Start("https://www.microncode.com/developers/cs-video-player/?cmd=order")

    End Sub

End Class
