#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 CSFFmpegExample
{
    using System;
    using System.Diagnostics;
    using System.Linq;
    using System.Windows.Forms;

    /// <summary>
    /// The main example of using the CSffmpeg libraries.
    /// </summary>
    public partial class frmMain : Form
    {
        //Create global object so we can stop / pause globally
        CSFFmpeg.CSFFmpeg _CSFFmpeg;

        public frmMain()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Get all the information about the source media file in a new window
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cmdMediaInformation_Click(object sender, EventArgs e)
        {
            if (txtSource.Text == "") return;

            frmMediaProperties fMediaInformation = new frmMediaProperties();

            fMediaInformation.LoadMediaFile(txtSource.Text);

            fMediaInformation.ShowDialog(this);

        }

        /// <summary>
        /// Open media file and get the information
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private async void cmdOpenMedia_Click(object sender, EventArgs e)
        {
            try
            {
                OpenFileDialog openFileDialog1 = new OpenFileDialog();

                if (openFileDialog1.ShowDialog() != System.Windows.Forms.DialogResult.OK)
                {
                    return;
                }

                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

                string LibsPath = "";

                //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
                CSMediaProperties.MediaProperties mediaProperties;
                mediaProperties = await _CSFFmpeg.Open(txtSource.Text);

                #region GetID3Tags

                if (mediaProperties.Format == null)
                    return;

                if (mediaProperties.Format.Tags.FirstOrDefault(tag => tag.Key == "title") != null)
                    Debug.WriteLine(mediaProperties.Format.Tags.FirstOrDefault(tag => tag.Key == "title").Value);

                if (mediaProperties.Format.Tags.FirstOrDefault(tag => tag.Key == "comment") != null)
                    Debug.WriteLine(mediaProperties.Format.Tags.FirstOrDefault(tag => tag.Key == "comment").Value);

                if (mediaProperties.Format.Tags.FirstOrDefault(tag => tag.Key == "artist") != null)
                    Debug.WriteLine(mediaProperties.Format.Tags.FirstOrDefault(tag => tag.Key == "artist").Value);

                if (mediaProperties.Format.Tags.FirstOrDefault(tag => tag.Key == "album") != null)
                    Debug.WriteLine(mediaProperties.Format.Tags.FirstOrDefault(tag => tag.Key == "album").Value);

                if (mediaProperties.Format.Tags.FirstOrDefault(tag => tag.Key == "date") != null)
                    Debug.WriteLine(mediaProperties.Format.Tags.FirstOrDefault(tag => tag.Key == "date").Value);

                #endregion

                //Dispose the library
                _CSFFmpeg.Dispose();
                _CSFFmpeg = null;

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {

            }
        }

        /// <summary>
        /// Run the operation
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cmdRun_Click(object sender, EventArgs e)
        {
            try
            {
                //Create a new instance of the library with the direction 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]\

                _CSFFmpeg = new CSFFmpeg.CSFFmpeg("UserName", "RegKey", LibsPath);

                //Set the arguments
                _CSFFmpeg.ArgsDirect = txtArguments.Text;

                //Events
                #region Events

                //On ffmpeg output event
                _CSFFmpeg.OperationOutput += (o, s) =>
                {
                    //Display in the results in the text box
                    txtResults.AppendText(s);
                    txtResults.AppendText(Environment.NewLine);
                    txtResults.SelectionStart = txtResults.Text.Length;
                    txtResults.ScrollToCaret();
                    txtResults.Refresh();
                };

                //On start event
                _CSFFmpeg.OperationStart += (o) =>
                {
                    lblStatus.Text = "Start";

                };

                //On progress event
                _CSFFmpeg.OperationProgress += (o, s) =>
                {
                    progressBar1.Value = s;
                };

                //Done
                _CSFFmpeg.OperationDone += (o) =>
                {
                    this.Invoke(new Action(() =>
                    {
                        lblStatus.Text = "Done";
                    }));
                };

                #endregion

                //Run the ffmpeg from the program
                _CSFFmpeg.Run();
            }

            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {

            }

        }

        /// <summary>
        /// Run the operation async
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private async void cmdRunAsync_Click(object sender, EventArgs e)
        {
            try
            {
                //Create a new instance of the library with the direction 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]\

                _CSFFmpeg = new CSFFmpeg.CSFFmpeg("UserName", "RegKey", LibsPath);

                //Set the source file
                _CSFFmpeg.ArgsDirect = txtArguments.Text;

                //Run async and get the results. This technique is not recommended for long operation.
                string results = await _CSFFmpeg.RunAsync();

                //Display in the results text box
                txtResults.Text = results;

                //Clean
                _CSFFmpeg.Dispose();
                _CSFFmpeg = null;

            }

            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {

            }
        }

        /// <summary>
        /// Stop
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cmdStop_Click(object sender, EventArgs e)
        {
            try { _CSFFmpeg.Abort(); }
            catch { }
        }

        /// <summary>
        /// Pause
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cmdPause_Click(object sender, EventArgs e)
        {
            try { _CSFFmpeg.Pause(); }
            catch { }
        }

        /// <summary>
        /// Resume
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cmdResume_Click(object sender, EventArgs e)
        {
            try { _CSFFmpeg.Resume(); }
            catch { }
        }

        /// <summary>
        /// Form_Load
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void frmMain_Load(object sender, EventArgs e)
        {

        }

        /// <summary>
        /// Order a license.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cmdOrderLicense_Click(object sender, EventArgs 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-ffmpeg/?cmd=order

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

    }
}
