using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MPLATFORMLib;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.Button;

namespace YouTube_Playback_Sample
{
    public partial class Form : System.Windows.Forms.Form
    {

        private MPreviewClass m_objPreview = new MPreviewClass();
        private MFileClass m_objFile = new MFileClass();
        private List<YVLink> VideoQualityLinks = new List <YVLink> ();
        private List<YALink> AudioQualityLinks = new List<YALink>();


        public Form()
        {
            InitializeComponent();
        }

        private void Form_Load(object sender, EventArgs e)
        {
            // Enable audio and video on the preview according to the states of the checkBoxes:
            m_objPreview.PreviewEnable("", checkBoxAudio.Checked ? 1 : 0, checkBoxVideo.Checked ? 1 : 0);

            // Assigning panel1 as the Preview:
            m_objPreview.PreviewWindowSet("", PreviewPanel.Handle.ToInt32());
        }

        private void ButtonOpenURL_Click(object sender, EventArgs e)
        {
            m_objPreview.ObjectStart(m_objFile);

            string OpenARG = "";

            if ((comboBoxVideoFormatList.SelectedItem is YVLink) && (comboBoxAudioFormatList.SelectedItem is YALink))
            {
                OpenARG = (comboBoxVideoFormatList.SelectedItem as YVLink).GetOpenArgumentPart1st() + (comboBoxAudioFormatList.SelectedItem as YALink).GetOpenArgumentPart2nd();
            }

            m_objFile.FileNameSet(textBoxURL.Text, OpenARG);
            m_objFile.PropsSet("loop", checkBoxLoop.Checked ? "true" : "false");
            m_objFile.FilePlayStart();
        }


        private void checkBoxVideo_CheckedChanged(object sender, EventArgs e)
        {
            // Enable audio and video on the preview according to the states of the checkBoxes:
            m_objPreview.PreviewEnable("", checkBoxAudio.Checked ? 1 : 0, checkBoxVideo.Checked ? 1 : 0);
        }

        private void checkBoxAudio_CheckedChanged(object sender, EventArgs e)
        {
            // Enable audio and video on the preview according to the states of the checkBoxes:
            m_objPreview.PreviewEnable("", checkBoxAudio.Checked ? 1 : 0, checkBoxVideo.Checked ? 1 : 0);
        }

        // Resize the preview when the main form is being resized:
        private void PreviewPanel_Resize(object sender, EventArgs e)
        {
            if (m_objPreview != null) { m_objPreview.PreviewWindowSet("", PreviewPanel.Handle.ToInt64()); }
        }

        private void ButtonGetFormatsList_Click(object sender, EventArgs e)
        {

            ButtonOpenURL.Enabled = false;
            ButtonGetFormatsList.Enabled = false;

            comboBoxVideoFormatList.Items.Clear();
            comboBoxAudioFormatList.Items.Clear();
            VideoQualityLinks.Clear();
            AudioQualityLinks.Clear();

            Process process = new Process();
            process.StartInfo.FileName = @"yt-dlp.exe";
            process.StartInfo.Arguments = @"-F " + textBoxURL.Text;
            process.StartInfo.UseShellExecute = false;
            process.StartInfo.CreateNoWindow = true;
            process.StartInfo.RedirectStandardOutput = true;
            process.Start();
            
            string [] AppOut = process.StandardOutput.ReadToEnd().Split('\n');
            VideoQualityLinks.Capacity = AppOut.Length;
            AudioQualityLinks.Capacity = AppOut.Length;


            foreach (string link in AppOut)
            {
                string[] parsedARG = link.Split(' ').Where(val => val != String.Empty).ToArray();

                if (parsedARG.Length > 0)
                {
                    if (int.TryParse(parsedARG.First(), out _))
                    {
                        if (parsedARG[2] != "audio" && parsedARG[3] != "only")
                        {
                            VideoQualityLinks.Add(new YVLink(parsedARG[0], parsedARG[2], parsedARG[3], parsedARG[1].ToUpper(), parsedARG[5]));
                            comboBoxVideoFormatList.Items.Add(VideoQualityLinks.Last());
                        }
                        else if (parsedARG[2] == "audio" && parsedARG[3] == "only")
                        {
                            AudioQualityLinks.Add(new YALink(parsedARG[0], parsedARG[1].ToUpper(), parsedARG[5], parsedARG[6], parsedARG[11]));
                            comboBoxAudioFormatList.Items.Add(AudioQualityLinks.Last());
                        }
                    }
                }
            }

            comboBoxVideoFormatList.SelectedIndex = 0;
            comboBoxAudioFormatList.SelectedIndex = 0;


            process.WaitForExit();

            ButtonOpenURL.Enabled = true;
            ButtonGetFormatsList.Enabled = true;
        }

        private void checkBoxLoop_CheckedChanged(object sender, EventArgs e)
        {
            m_objFile.PropsSet("loop", checkBoxLoop.Checked ? "true" : "false");
        }
    }
}
