#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. 
//
#endregion

namespace CSVideoPlayerExample
{
    using Microsoft.Win32;
    using System;
    using System.Timers;
    using System.Windows;
    using System.Windows.Input;

    /// <summary>
    /// The main example of using the CSVideoPlayer with WPF.
    /// </summary>
    public partial class MainWindow : Window
    {
        private static System.Timers.Timer _SliderTimer;
        private string _mediafile = "";

        int _rotate = 0;
        int _ScaleX = 0;

        /// <summary>
        /// Constructor.
        /// </summary>
        public MainWindow()
        {
            InitializeComponent();

            //Media seek handlers
            timelineSlider.AddHandler(MouseLeftButtonDownEvent, new MouseButtonEventHandler(Slider_MouseLeftButtonDown), true);
            timelineSlider.AddHandler(MouseLeftButtonUpEvent, new MouseButtonEventHandler(Slider_MouseLeftButtonUp), true);
        }

        /// <summary>
        /// Window_Loaded.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            //Init function, must be call before using any functionality of the library.

            //Set the path of the ffmpeg libraries
            string LibsPath = "";

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

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

            //Get the position with the _SliderTimer
            _SliderTimer = new System.Timers.Timer(200);
            _SliderTimer.Elapsed += OnTimedEvent;
            _SliderTimer.AutoReset = true;

        }

        /// <summary>
        /// Open the local media file to play.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cmdOpen_Click(object sender, RoutedEventArgs e)
        {
            //Open the local media file to play

            OpenFileDialog openFileDialog = new OpenFileDialog();

            if (openFileDialog.ShowDialog() != true)
            {
                return;
            }

            _mediafile = openFileDialog.FileName;

            videoPlayer.Open(new Uri(_mediafile));
        }

        /// <summary>
        /// Get the media properties in a new window.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cmdMediaProperties_Click(object sender, RoutedEventArgs e)
        {
            MediaPropertiesWindow mediaProperties = new MediaPropertiesWindow();

            mediaProperties.GetDetails(_mediafile);

            mediaProperties.ShowDialog();
        }

        /// <summary>
        /// Play.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cmdPlay_Click(object sender, RoutedEventArgs e)
        {
            videoPlayer.Play();

            _SliderTimer.Enabled = true;
        }

        /// <summary>
        /// Pause.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cmdPause_Click(object sender, RoutedEventArgs e)
        {
            videoPlayer.Pause();
        }

        /// <summary>
        /// UnPause.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cmdUnPause_Click(object sender, RoutedEventArgs e)
        {
            videoPlayer.Play();
        }

        /// <summary>
        /// Stop.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cmdStop_Click(object sender, RoutedEventArgs e)
        {
            videoPlayer.Stop();
            _SliderTimer.Enabled = false;
            timelineSlider.Value = 0;
        }

        /// <summary>
        /// Play on full screen.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cmdFullScreen_Click(object sender, RoutedEventArgs e)
        {
            videoPlayer.FullScreen();
        }

        /// <summary>
        /// Filp.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cmdFlip_Click(object sender, RoutedEventArgs e)
        {
            //Flip

            if (_ScaleX < 0)
                _ScaleX = 1;
            else
                _ScaleX = -1;

            videoPlayer.Flip(_ScaleX);
        }

        /// <summary>
        /// Rotate +90.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cmdRotate90_Click(object sender, RoutedEventArgs e)
        {
            //Rotate +90

            _rotate += 90;
            videoPlayer.Rotate(_rotate);
        }

        /// <summary>
        /// Rotate -90.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cmdRotate90Minus_Click(object sender, RoutedEventArgs e)
        {
            //Rotate -90

            _rotate -= 90;
            videoPlayer.Rotate(_rotate);
        }

        /// <summary>
        /// Speed ratio.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="args"></param>
        private void ChangeMediaSpeedRatio(object sender, RoutedPropertyChangedEventArgs<double> args)
        {
            //Set the SpeedRatio

            videoPlayer.SpeedRatio((int)speedRatioSlider.Value);
        }

        /// <summary>
        /// Volume.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ChangeMediaVolume(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            //Set the MediaVolume

            videoPlayer.Volume((int)volumeSlider.Value);
        }

        /// <summary>
        /// Window closing.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            e.Cancel = true;
            this.Visibility = Visibility.Hidden;
        }

        #region Events

        /// <summary>
        /// Timer event of the slider.
        /// </summary>
        /// <param name="source"></param>
        /// <param name="e"></param>
        private void OnTimedEvent(Object source, ElapsedEventArgs e)
        {
            //Set the slider

            Dispatcher.BeginInvoke(new Action(() => { timelineSlider.Value = videoPlayer.PositionGet().TotalMilliseconds; }));
        }

        /// <summary>
        /// Stop move the slider when MouseDown.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Slider_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            //Seek to the position when MouseLeftButtonUp

            int SliderValue = (int)timelineSlider.Value;

            TimeSpan ts = new TimeSpan(0, 0, 0, 0, SliderValue);
            videoPlayer.PositionSet(ts);
            _SliderTimer.Enabled = true;
        }

        /// <summary>
        /// Set the maximum property of the slider to the duration of the media file.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void videoPlayer_MediaOpened(object sender, CSVideoPlayer.MediaOpenedEventArgs e)
        {
            timelineSlider.Maximum = videoPlayer.NaturalDuration().TotalMilliseconds;
        }

        /// <summary>
        /// Stop move the slider when MouseLeftButtonDown.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Slider_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            _SliderTimer.Enabled = false;
        }

        /// <summary>
        /// On MediaChanged.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void videoPlayer_MediaChanged(object sender, CSVideoPlayer.MediaOpenedEventArgs e)
        {
            //On MediaChanged
        }

        /// <summary>
        /// On MediaChanging.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void videoPlayer_MediaChanging(object sender, CSVideoPlayer.MediaOpeningEventArgs e)
        {
            //On MediaChanging
        }

        /// <summary>
        /// On MediaClosed.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void videoPlayer_MediaClosed(object sender, EventArgs e)
        {
            //On MediaClosed
        }

        /// <summary>
        /// On MediaEnded.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void videoPlayer_MediaEnded(object sender, EventArgs e)
        {
            //On MediaEnded
        }

        /// <summary>
        /// On MediaFailed.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void videoPlayer_MediaFailed(object sender, CSVideoPlayer.MediaFailedEventArgs e)
        {
            //On MediaFailed
        }

        /// <summary>
        /// On MediaInitializing.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void videoPlayer_MediaInitializing(object sender, CSVideoPlayer.MediaInitializingEventArgs e)
        {
            //On MediaInitializing
        }

        /// <summary>
        /// On MediaOpening.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void videoPlayer_MediaOpening(object sender, CSVideoPlayer.MediaOpeningEventArgs e)
        {
            //On MediaOpening
        }

        /// <summary>
        /// On MediaReady.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void videoPlayer_MediaReady(object sender, EventArgs e)
        {
            //On MediaReady
        }

        /// <summary>
        /// On MediaStateChanged.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void videoPlayer_MediaStateChanged(object sender, CSVideoPlayer.MediaStateChangedEventArgs e)
        {
            //On MediaStateChanged
        }

        #endregion

        /// <summary>
        /// Order a license.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cmdOrderLicense_Click(object sender, RoutedEventArgs e)
        {
            /// 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");
        }
    }
}
