using System;
using System.IO;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using Microsoft.DirectX;
using Microsoft.DirectX.DirectSound;

namespace Icarus.SoundEngine {

    /// <summary>
    /// Summary description for SoundTester.
    /// </summary>
    public class SoundTester : System.Windows.Forms.Form {

        #region GUI Components
        private System.Windows.Forms.Button playMusicButton;
        private System.Windows.Forms.Button playSoundButton1;
        private System.Windows.Forms.Button playSoundButton2;
        private System.Windows.Forms.Button exitButton;
        private System.Windows.Forms.Label musicFilenameLabel;
        private System.Windows.Forms.Label soundFilenameLabel1;
        private System.Windows.Forms.Label soundFilenameLabel2;
        private System.Windows.Forms.Button musicFileButton;
        private System.Windows.Forms.Button soundFileButton1;
        private System.Windows.Forms.Button soundFileButton2;
        private System.Windows.Forms.Button stopMusicButton;
        private System.Windows.Forms.Button pauseButton;
        private System.Windows.Forms.TrackBar musicVolumeBar;
        private System.Windows.Forms.TrackBar soundVolumeBar;
        private System.Windows.Forms.Button PauseAll;
        private System.Windows.Forms.Button PlayAll;
        private System.Windows.Forms.Button StopAll;
        private System.Windows.Forms.Button removeOverlay;
        private System.Windows.Forms.ComboBox overlaySelector;
        private System.Windows.Forms.Button setNext;
        #endregion

        #region Instance Variables
        /// <summary>
        /// Generic file path name holder for loading music and sound effects.
        /// </summary>
        private string PathSoundFile = string.Empty;
        /// <summary>
        /// The reference to the singleton SoundEngine.
        /// </summary>
        private SoundEngine engine = null;
        /// <summary>
        /// Currently selected overlay stream being manipulated.
        /// </summary>
        private int selectedOverlay = 0;           
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.Container components = null;
        #endregion

        #region Execution And Initialization
    
        /// <summary>
        /// Display the Sound Tester Form
        /// </summary>
        public static void Main() {
            new SoundTester().Show();
            Console.WriteLine("You are now running the tester program for the SoundEngine of the Icarus Game Engine");
        }

        /// <summary>
        /// Initialize the SoundTester form
        /// </summary>
        public SoundTester() {
            // Required for Windows Form Designer support
            InitializeComponent();
            engine = SoundEngine.Instance;
            engine.Initialize(this);

            for (int i = 0; i < SoundSettings.Instance.gs_n_bgMusic; i++) {
                overlaySelector.Items.Add(i+1);
            }
        }

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose( bool disposing ) {
            if( disposing ) {
                if(components != null) {
                    components.Dispose();
                }
            }
            base.Dispose( disposing );
        }
        #endregion

        #region Windows Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent() {
            this.playMusicButton = new System.Windows.Forms.Button();
            this.playSoundButton1 = new System.Windows.Forms.Button();
            this.playSoundButton2 = new System.Windows.Forms.Button();
            this.exitButton = new System.Windows.Forms.Button();
            this.musicFilenameLabel = new System.Windows.Forms.Label();
            this.soundFilenameLabel1 = new System.Windows.Forms.Label();
            this.soundFilenameLabel2 = new System.Windows.Forms.Label();
            this.musicFileButton = new System.Windows.Forms.Button();
            this.soundFileButton1 = new System.Windows.Forms.Button();
            this.soundFileButton2 = new System.Windows.Forms.Button();
            this.stopMusicButton = new System.Windows.Forms.Button();
            this.pauseButton = new System.Windows.Forms.Button();
            this.musicVolumeBar = new System.Windows.Forms.TrackBar();
            this.soundVolumeBar = new System.Windows.Forms.TrackBar();
            this.PauseAll = new System.Windows.Forms.Button();
            this.PlayAll = new System.Windows.Forms.Button();
            this.StopAll = new System.Windows.Forms.Button();
            this.overlaySelector = new System.Windows.Forms.ComboBox();
            this.removeOverlay = new System.Windows.Forms.Button();
            this.setNext = new System.Windows.Forms.Button();
            ((System.ComponentModel.ISupportInitialize)(this.musicVolumeBar)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.soundVolumeBar)).BeginInit();
            this.SuspendLayout();
            // 
            // playMusicButton
            // 
            this.playMusicButton.Location = new System.Drawing.Point(104, 120);
            this.playMusicButton.Name = "playMusicButton";
            this.playMusicButton.Size = new System.Drawing.Size(80, 23);
            this.playMusicButton.TabIndex = 0;
            this.playMusicButton.Text = "Play";
            this.playMusicButton.Click += new System.EventHandler(this.playMusicButton_Click);
            // 
            // playSoundButton1
            // 
            this.playSoundButton1.Location = new System.Drawing.Point(480, 184);
            this.playSoundButton1.Name = "playSoundButton1";
            this.playSoundButton1.Size = new System.Drawing.Size(88, 23);
            this.playSoundButton1.TabIndex = 1;
            this.playSoundButton1.Text = "Play Sound #1";
            this.playSoundButton1.Click += new System.EventHandler(this.playSoundButton1_Click);
            // 
            // playSoundButton2
            // 
            this.playSoundButton2.Location = new System.Drawing.Point(480, 232);
            this.playSoundButton2.Name = "playSoundButton2";
            this.playSoundButton2.Size = new System.Drawing.Size(88, 23);
            this.playSoundButton2.TabIndex = 2;
            this.playSoundButton2.Text = "Play Sound #2";
            this.playSoundButton2.Click += new System.EventHandler(this.playSoundButton2_Click);
            // 
            // exitButton
            // 
            this.exitButton.Location = new System.Drawing.Point(504, 8);
            this.exitButton.Name = "exitButton";
            this.exitButton.Size = new System.Drawing.Size(64, 40);
            this.exitButton.TabIndex = 3;
            this.exitButton.Text = "Exit";
            this.exitButton.Click += new System.EventHandler(this.exitButton_Click);
            // 
            // musicFilenameLabel
            // 
            this.musicFilenameLabel.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
            this.musicFilenameLabel.Location = new System.Drawing.Point(104, 72);
            this.musicFilenameLabel.Name = "musicFilenameLabel";
            this.musicFilenameLabel.Size = new System.Drawing.Size(368, 40);
            this.musicFilenameLabel.TabIndex = 4;
            this.musicFilenameLabel.Text = "No file loaded.";
            this.musicFilenameLabel.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
            // 
            // soundFilenameLabel1
            // 
            this.soundFilenameLabel1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
            this.soundFilenameLabel1.Location = new System.Drawing.Point(104, 176);
            this.soundFilenameLabel1.Name = "soundFilenameLabel1";
            this.soundFilenameLabel1.Size = new System.Drawing.Size(368, 40);
            this.soundFilenameLabel1.TabIndex = 5;
            this.soundFilenameLabel1.Text = "No file loaded.";
            this.soundFilenameLabel1.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
            // 
            // soundFilenameLabel2
            // 
            this.soundFilenameLabel2.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
            this.soundFilenameLabel2.Location = new System.Drawing.Point(104, 224);
            this.soundFilenameLabel2.Name = "soundFilenameLabel2";
            this.soundFilenameLabel2.Size = new System.Drawing.Size(368, 40);
            this.soundFilenameLabel2.TabIndex = 6;
            this.soundFilenameLabel2.Text = "No file loaded.";
            this.soundFilenameLabel2.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
            // 
            // musicFileButton
            // 
            this.musicFileButton.Location = new System.Drawing.Point(16, 80);
            this.musicFileButton.Name = "musicFileButton";
            this.musicFileButton.Size = new System.Drawing.Size(72, 23);
            this.musicFileButton.TabIndex = 7;
            this.musicFileButton.Text = "Music File";
            this.musicFileButton.Click += new System.EventHandler(this.musicFileButton_Click);
            // 
            // soundFileButton1
            // 
            this.soundFileButton1.Location = new System.Drawing.Point(16, 184);
            this.soundFileButton1.Name = "soundFileButton1";
            this.soundFileButton1.Size = new System.Drawing.Size(72, 23);
            this.soundFileButton1.TabIndex = 8;
            this.soundFileButton1.Text = "Sound File";
            this.soundFileButton1.Click += new System.EventHandler(this.soundFileButton1_Click);
            // 
            // soundFileButton2
            // 
            this.soundFileButton2.Location = new System.Drawing.Point(16, 232);
            this.soundFileButton2.Name = "soundFileButton2";
            this.soundFileButton2.Size = new System.Drawing.Size(72, 23);
            this.soundFileButton2.TabIndex = 9;
            this.soundFileButton2.Text = "Sound File";
            this.soundFileButton2.Click += new System.EventHandler(this.soundFileButton2_Click);
            // 
            // stopMusicButton
            // 
            this.stopMusicButton.Location = new System.Drawing.Point(280, 120);
            this.stopMusicButton.Name = "stopMusicButton";
            this.stopMusicButton.Size = new System.Drawing.Size(80, 23);
            this.stopMusicButton.TabIndex = 10;
            this.stopMusicButton.Text = "Stop";
            this.stopMusicButton.Click += new System.EventHandler(this.stopMusicButton_Click);
            // 
            // pauseButton
            // 
            this.pauseButton.Location = new System.Drawing.Point(192, 120);
            this.pauseButton.Name = "pauseButton";
            this.pauseButton.Size = new System.Drawing.Size(80, 23);
            this.pauseButton.TabIndex = 11;
            this.pauseButton.Text = "Pause";
            this.pauseButton.Click += new System.EventHandler(this.pauseButton_Click);
            // 
            // musicVolumeBar
            // 
            this.musicVolumeBar.Location = new System.Drawing.Point(8, 112);
            this.musicVolumeBar.Maximum = 100;
            this.musicVolumeBar.Name = "musicVolumeBar";
            this.musicVolumeBar.Size = new System.Drawing.Size(88, 45);
            this.musicVolumeBar.TabIndex = 10;
            this.musicVolumeBar.Tag = "";
            this.musicVolumeBar.Value = 100;
            this.musicVolumeBar.Scroll += new System.EventHandler(this.musicVolumeBar_Scroll);
            // 
            // soundVolumeBar
            // 
            this.soundVolumeBar.Location = new System.Drawing.Point(8, 264);
            this.soundVolumeBar.Maximum = 100;
            this.soundVolumeBar.Name = "soundVolumeBar";
            this.soundVolumeBar.Size = new System.Drawing.Size(88, 45);
            this.soundVolumeBar.TabIndex = 10;
            this.soundVolumeBar.Value = 100;
            this.soundVolumeBar.Scroll += new System.EventHandler(this.soundVolumeBar_Scroll);
            // 
            // PauseAll
            // 
            this.PauseAll.Location = new System.Drawing.Point(176, 24);
            this.PauseAll.Name = "PauseAll";
            this.PauseAll.Size = new System.Drawing.Size(112, 23);
            this.PauseAll.TabIndex = 12;
            this.PauseAll.Text = "Pause All Overlays";
            this.PauseAll.Click += new System.EventHandler(this.PauseAll_Click);
            // 
            // PlayAll
            // 
            this.PlayAll.Location = new System.Drawing.Point(40, 24);
            this.PlayAll.Name = "PlayAll";
            this.PlayAll.Size = new System.Drawing.Size(112, 23);
            this.PlayAll.TabIndex = 13;
            this.PlayAll.Text = "Play All Overlays";
            this.PlayAll.Click += new System.EventHandler(this.PlayAll_Click);
            // 
            // StopAll
            // 
            this.StopAll.Location = new System.Drawing.Point(312, 24);
            this.StopAll.Name = "StopAll";
            this.StopAll.Size = new System.Drawing.Size(112, 23);
            this.StopAll.TabIndex = 14;
            this.StopAll.Text = "Stop All Overlays";
            this.StopAll.Click += new System.EventHandler(this.StopAll_Click);
            // 
            // overlaySelector
            // 
            this.overlaySelector.Location = new System.Drawing.Point(480, 80);
            this.overlaySelector.Name = "overlaySelector";
            this.overlaySelector.Size = new System.Drawing.Size(64, 21);
            this.overlaySelector.TabIndex = 15;
            this.overlaySelector.Text = "Overlay";
            this.overlaySelector.SelectedIndexChanged += new System.EventHandler(this.overlaysSelector_SelectedIndexChanged);
            // 
            // removeOverlay
            // 
            this.removeOverlay.Location = new System.Drawing.Point(480, 120);
            this.removeOverlay.Name = "removeOverlay";
            this.removeOverlay.TabIndex = 16;
            this.removeOverlay.Text = "Remove";
            this.removeOverlay.Click += new System.EventHandler(this.removeOverlay_Click);
            // 
            // setNext
            // 
            this.setNext.Location = new System.Drawing.Point(392, 120);
            this.setNext.Name = "setNext";
            this.setNext.Size = new System.Drawing.Size(80, 23);
            this.setNext.TabIndex = 17;
            this.setNext.Text = "Set Next";
            this.setNext.Click += new System.EventHandler(this.setNext_Click);
            // 
            // SoundTester
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(576, 318);
            this.Controls.Add(this.setNext);
            this.Controls.Add(this.removeOverlay);
            this.Controls.Add(this.overlaySelector);
            this.Controls.Add(this.StopAll);
            this.Controls.Add(this.PlayAll);
            this.Controls.Add(this.PauseAll);
            this.Controls.Add(this.soundVolumeBar);
            this.Controls.Add(this.musicVolumeBar);
            this.Controls.Add(this.pauseButton);
            this.Controls.Add(this.stopMusicButton);
            this.Controls.Add(this.soundFileButton2);
            this.Controls.Add(this.soundFileButton1);
            this.Controls.Add(this.musicFileButton);
            this.Controls.Add(this.soundFilenameLabel2);
            this.Controls.Add(this.soundFilenameLabel1);
            this.Controls.Add(this.musicFilenameLabel);
            this.Controls.Add(this.exitButton);
            this.Controls.Add(this.playSoundButton2);
            this.Controls.Add(this.playSoundButton1);
            this.Controls.Add(this.playMusicButton);
            this.Name = "SoundTester";
            this.Text = "SoundTester";
            ((System.ComponentModel.ISupportInitialize)(this.musicVolumeBar)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.soundVolumeBar)).EndInit();
            this.ResumeLayout(false);

        }
        #endregion

        #region GUI Component Actions
        private void playMusicButton_Click(object sender, System.EventArgs e) {
            //Console.WriteLine("play music stream " + selectedOverlay);
            engine.PlayMusic(musicFilenameLabel.Text, selectedOverlay);
        }

        private void playSoundButton1_Click(object sender, System.EventArgs e) {
            //Console.WriteLine("play sound 1");
            engine.PlaySound(soundFilenameLabel1.Text);
        }

        private void playSoundButton2_Click(object sender, System.EventArgs e) {
            //Console.WriteLine("play sound 2");
            engine.PlaySound(soundFilenameLabel2.Text);
        }

        private void exitButton_Click(object sender, System.EventArgs e) {
            Console.WriteLine("You have exited the tester program for the SoundEngine of the Icarus Game Engine.");
            Console.WriteLine("Have A Nice Day!");            
            Close();
        }

        private void musicFileButton_Click(object sender, System.EventArgs e) {
            //Console.WriteLine("music file");
            OpenFileDialog ofd = new OpenFileDialog();

            ofd.InitialDirectory = PathSoundFile;
            ofd.Filter=  "All Files(*.*)|*.*";
            if(DialogResult.Cancel == ofd.ShowDialog()) {
                return;
            }

            PathSoundFile = Path.GetFullPath(ofd.FileName);
            musicFilenameLabel.Text = Path.GetFullPath(ofd.FileName);      
        }    

        private void soundFileButton1_Click(object sender, System.EventArgs e) {
            //Console.WriteLine("sound file 1");
            OpenFileDialog ofd = new OpenFileDialog();

            ofd.InitialDirectory = PathSoundFile;
            ofd.Filter=  "Wave files(*.wav)|*.wav";
            if(DialogResult.Cancel == ofd.ShowDialog()) {
                return;
            }

            PathSoundFile = Path.GetFullPath(ofd.FileName);
            soundFilenameLabel1.Text = Path.GetFullPath(ofd.FileName);
        }

        private void soundFileButton2_Click(object sender, System.EventArgs e) {
            //Console.WriteLine("sound file 2");
            OpenFileDialog ofd = new OpenFileDialog();

            ofd.InitialDirectory = PathSoundFile;
            ofd.Filter=  "Wave files(*.wav)|*.wav";
            if(DialogResult.Cancel == ofd.ShowDialog()) {
                return;
            }

            PathSoundFile = Path.GetFullPath(ofd.FileName);
            soundFilenameLabel2.Text = Path.GetFullPath(ofd.FileName);
        }

        private void stopMusicButton_Click(object sender, System.EventArgs e) {
            //Console.WriteLine("stop music stream " + selectedOverlay);
            engine.StopMusic(selectedOverlay);
        }

        private void pauseButton_Click(object sender, System.EventArgs e) {
            //Console.WriteLine("pause music stream " + selectedOverlay);
            engine.PauseMusic(selectedOverlay);
        }

        private void musicVolumeBar_Scroll(object sender, System.EventArgs e) {
            //Console.WriteLine("music=" + musicVolumeBar.Value);
            engine.SetMusicVolume(musicVolumeBar.Value);
        }

        private void soundVolumeBar_Scroll(object sender, System.EventArgs e) {
            //Console.WriteLine("sound=" + soundVolumeBar.Value);
            engine.SetSoundVolume(soundVolumeBar.Value);
        }

        private void PlayAll_Click(object sender, System.EventArgs e) {
            //Console.WriteLine("play music");
            engine.PlayMusic();
        }

        private void PauseAll_Click(object sender, System.EventArgs e) {
            //Console.WriteLine("pause music");
            engine.PauseMusic();
        }

        private void StopAll_Click(object sender, System.EventArgs e) {
            //Console.WriteLine("stop music");
            engine.StopMusic();
        }

        private void overlaysSelector_SelectedIndexChanged(object sender, System.EventArgs e) {
            //Console.WriteLine(overlaySelector.SelectedIndex);
            selectedOverlay = (int) overlaySelector.SelectedIndex;      
            this.musicFilenameLabel.Text = engine.getStreamFilename(selectedOverlay);
        }

        private void removeOverlay_Click(object sender, System.EventArgs e) {
            //Console.WriteLine("remove music stream " + selectedOverlay);
            engine.RemoveStream(selectedOverlay);
        }

        private void setNext_Click(object sender, System.EventArgs e) {
            //Console.WriteLine("set next file on music stream " + selectedOverlay);
            engine.SetNextFileOnStream(musicFilenameLabel.Text, selectedOverlay);
        }
        #endregion
    }

} //End Name Space