using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using System.IO;
using System.Media;
using System.Diagnostics;

namespace SoundBoard
{
    public partial class formMain : Form
    {

        public SoundPlayer player = new SoundPlayer();
        public Random randomNum = new Random();
        bool willLoopSound = false;

        public formMain()
        {
            InitializeComponent();
            //create event handlers for onClick events in the ListBoxes
            gamerPoopListBox.MouseClick += new MouseEventHandler(soundListBox_OnLeftClick);
            asdfMovieListBox.MouseClick += new MouseEventHandler(soundListBox_OnLeftClick);
        }

        private void soundListBox_OnLeftClick(object sender, MouseEventArgs e)
        {
            if (soundboardTabControl.SelectedIndex == 0)
            {
                string fullPath = Path.GetFullPath("gamer_poop/") + gamerPoopListBox.SelectedItem + ".wav";
                player.SoundLocation = fullPath;
                player.Load();
                if (willLoopSound)
                {
                    player.PlayLooping();
                }
                else
                {
                    player.Play();
                }               
            }
            else if (soundboardTabControl.SelectedIndex == 1)
            {
                string fullPath = Path.GetFullPath("asdfmovie/") + asdfMovieListBox.SelectedItem + ".wav";
                player.SoundLocation = fullPath;
                player.Load();
                if (willLoopSound)
                {
                    player.PlayLooping();
                }
                else
                {
                    player.Play();
                }   
            }
        }

        private void formMain_Load(object sender, EventArgs e)
        {
            string[] soundFiles;
            soundFiles = Directory.GetFiles(Path.GetFullPath("gamer_poop/"));
            foreach (string str in soundFiles)
            {
                this.gamerPoopListBox.Items.Add(Path.GetFileNameWithoutExtension(str));
            }
            soundFiles = Directory.GetFiles(Path.GetFullPath("asdfmovie/"));
            foreach (string str in soundFiles)
            {
                asdfMovieListBox.Items.Add(Path.GetFileNameWithoutExtension(str));
            }
        }

        private void btnRandom_Click(object sender, EventArgs e)
        {
            int iterations = (int)randomIterationNumericUpDown.Value;
            string fullPath;
            //disable the Random Button and the NumericUpDown box
            btnRandom.Enabled = false;
            randomIterationNumericUpDown.Enabled = false;
            loopSelectedSoundCheckBox.Enabled = false;

            if (mainMenuPlayAll.Checked == true)
            {
                playRandomSoundBothColumns(iterations);
                btnRandom.Enabled = true;
                randomIterationNumericUpDown.Enabled = true;
                loopSelectedSoundCheckBox.Enabled = true;
                return;
            }

            for (int i = 0; i < iterations; i++)
            {
                int tempRandomNumber;

                if (soundboardTabControl.SelectedIndex == 0)
                {
                    tempRandomNumber = randomNum.Next(gamerPoopListBox.Items.Count - 1);
                    gamerPoopListBox.SelectedIndex = tempRandomNumber;
                    fullPath = Path.GetFullPath("gamer_poop/") + gamerPoopListBox.SelectedItem + ".wav";
                    player.SoundLocation = fullPath;
                    player.Load();
                    player.PlaySync();
                }
                else if (soundboardTabControl.SelectedIndex == 1)
                {
                    tempRandomNumber = randomNum.Next(asdfMovieListBox.Items.Count - 1);
                    asdfMovieListBox.SelectedIndex = tempRandomNumber;
                    fullPath = Path.GetFullPath("asdfmovie/") + asdfMovieListBox.SelectedItem + ".wav";
                    player.SoundLocation = fullPath;
                    player.Load();
                    player.PlaySync();
                }
                else
                {
                    MessageBox.Show("The " + soundboardTabControl.SelectedTab.Text + " tab has no sounds to play.", "No Waves Here");
                    return;
                }
            }
            //renable the controls
            btnRandom.Enabled = true;
            randomIterationNumericUpDown.Enabled = true;
            loopSelectedSoundCheckBox.Enabled = true;
        }

        private void loopSelectedSoundCheckBox_CheckedChanged(object sender, EventArgs e)
        {
            if (loopSelectedSoundCheckBox.Checked == true)
            {
                willLoopSound = true;
            }
            else
            {
                willLoopSound = false;
                player.Stop();
            }
        }

        private void playRandomSoundBothColumns(int iterations)
        {
            int tempRandomNumber = 0;
            int incrementor = 0;
            string soundPath;
            
            while (incrementor < iterations)
            {
                Random rand = new Random();
                tempRandomNumber = rand.Next(0, 200);
                if (tempRandomNumber <= 100)
                {
                    soundboardTabControl.SelectedIndex = 0;
                    tempRandomNumber = randomNum.Next(gamerPoopListBox.Items.Count - 1);
                    gamerPoopListBox.SelectedIndex = tempRandomNumber;
                    soundPath = Path.GetFullPath("gamer_poop/") + gamerPoopListBox.SelectedItem + ".wav";
                    player.SoundLocation = soundPath;
                    player.Load();
                    player.PlaySync();
                }
                else
                {
                    soundboardTabControl.SelectedIndex = 1;
                    tempRandomNumber = randomNum.Next(asdfMovieListBox.Items.Count - 1);
                    asdfMovieListBox.SelectedIndex = tempRandomNumber;
                    soundPath = Path.GetFullPath("asdfmovie/") + asdfMovieListBox.SelectedItem + ".wav";
                    player.SoundLocation = soundPath;
                    player.Load();
                    player.PlaySync();
                }
                incrementor++;
            }
        }

        private void mainMenuFileShowOnDisk_Click(object sender, EventArgs e)
        {
            string filePath;
            string arg;

            if (soundboardTabControl.SelectedIndex == 0)
            {
                filePath = Path.GetFullPath("gamer_poop/") + gamerPoopListBox.SelectedItem + ".wav";
                arg = "/select, " + '"' + filePath + '"';
                Process.Start("explorer.exe", arg);
            }
            else if (soundboardTabControl.SelectedIndex == 1)
            {
                filePath = Path.GetFullPath("asdfmovie/") + asdfMovieListBox.SelectedItem + ".wav";
                arg = "/select, " + '"' + filePath + '"';
                Process.Start("explorer.exe", arg);
            }
            else
            {
                MessageBox.Show("Unable to open files in this tab.", "Error");
            }
        }

        private void mainMenuHelpAbout_Click(object sender, EventArgs e)
        {
            aboutForm aboutDialogForm = new aboutForm();
            aboutDialogForm.ShowDialog();
        }

        private void mainMenuFileExit_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}
