#pragma once
#include <windows.h>
#include "audioClass.h"
#include "stringFunctions.h"
#include "dbClass.h"
#include "song.h"
#include "addSong.h"
#include <string>

namespace logiAmp {

	using namespace System;
	using namespace System::ComponentModel;
	using namespace System::Collections;
	using namespace System::Windows::Forms;
	using namespace System::Data;
	using namespace System::Drawing;
	using namespace System::Data::OleDb;

	/// <summary>
	/// Summary for Form1
	/// </summary>
	public ref class Form1 : public System::Windows::Forms::Form
	{
	public:
		Form1(void)
		{
			InitializeComponent();
			//
			//TODO: Add the constructor code here
			//
		}
	private: System::Windows::Forms::Button^  stopButton;
	private: System::Windows::Forms::Label^  songLength;
		/************************************************************************/
		/* My class declarations for Form1										*/
		/************************************************************************/
	private: audioClass ^player;
	private: array<song ^> ^relationToListbox;
			 /************************************************************************/
			 /* End my Declarations                                                  */
			 /************************************************************************/
	private: System::Windows::Forms::ListBox^  songListDb;
	private: System::Windows::Forms::Button^  removeSongButton;
	private: System::Windows::Forms::Button^  exportXMLButton;
	private: System::Windows::Forms::SaveFileDialog^  saveFileDialog1;
	private: System::Windows::Forms::TextBox^  filterTextBox;
	private: System::Windows::Forms::Button^  addSongButton;

	protected:
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		~Form1()
		{
			if (components)
			{
				delete components;
			}
		}
	private: System::Windows::Forms::Button^  playButton;
	private:
		/// <summary>
		/// Required designer variable.
		/// </summary>
		System::ComponentModel::Container ^components;

#pragma 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>
		void InitializeComponent(void)
		{
			this->playButton = (gcnew System::Windows::Forms::Button());
			this->stopButton = (gcnew System::Windows::Forms::Button());
			this->songLength = (gcnew System::Windows::Forms::Label());
			this->songListDb = (gcnew System::Windows::Forms::ListBox());
			this->addSongButton = (gcnew System::Windows::Forms::Button());
			this->removeSongButton = (gcnew System::Windows::Forms::Button());
			this->exportXMLButton = (gcnew System::Windows::Forms::Button());
			this->saveFileDialog1 = (gcnew System::Windows::Forms::SaveFileDialog());
			this->filterTextBox = (gcnew System::Windows::Forms::TextBox());
			this->SuspendLayout();
			// 
			// playButton
			// 
			this->playButton->Location = System::Drawing::Point(12, 8);
			this->playButton->Name = L"playButton";
			this->playButton->RightToLeft = System::Windows::Forms::RightToLeft::Yes;
			this->playButton->Size = System::Drawing::Size(75, 23);
			this->playButton->TabIndex = 0;
			this->playButton->Text = L"Play";
			this->playButton->UseVisualStyleBackColor = true;
			this->playButton->Click += gcnew System::EventHandler(this, &Form1::playButton_Click);
			// 
			// stopButton
			// 
			this->stopButton->Location = System::Drawing::Point(93, 8);
			this->stopButton->Name = L"stopButton";
			this->stopButton->Size = System::Drawing::Size(75, 23);
			this->stopButton->TabIndex = 1;
			this->stopButton->Text = L"Stop";
			this->stopButton->UseVisualStyleBackColor = true;
			this->stopButton->Click += gcnew System::EventHandler(this, &Form1::stopButton_Click);
			// 
			// songLength
			// 
			this->songLength->AutoSize = true;
			this->songLength->Location = System::Drawing::Point(13, 13);
			this->songLength->Name = L"songLength";
			this->songLength->Size = System::Drawing::Size(0, 13);
			this->songLength->TabIndex = 2;
			// 
			// songListDb
			// 
			this->songListDb->FormattingEnabled = true;
			this->songListDb->Location = System::Drawing::Point(12, 37);
			this->songListDb->Name = L"songListDb";
			this->songListDb->Size = System::Drawing::Size(318, 160);
			this->songListDb->TabIndex = 3;
			// 
			// addSongButton
			// 
			this->addSongButton->Location = System::Drawing::Point(174, 8);
			this->addSongButton->Name = L"addSongButton";
			this->addSongButton->Size = System::Drawing::Size(75, 23);
			this->addSongButton->TabIndex = 4;
			this->addSongButton->Text = L"Add Song";
			this->addSongButton->UseVisualStyleBackColor = true;
			this->addSongButton->Click += gcnew System::EventHandler(this, &Form1::addSongButton_Click);
			// 
			// removeSongButton
			// 
			this->removeSongButton->Location = System::Drawing::Point(256, 8);
			this->removeSongButton->Name = L"removeSongButton";
			this->removeSongButton->Size = System::Drawing::Size(75, 23);
			this->removeSongButton->TabIndex = 5;
			this->removeSongButton->Text = L"Remove";
			this->removeSongButton->UseVisualStyleBackColor = true;
			this->removeSongButton->Click += gcnew System::EventHandler(this, &Form1::removeSongButton_Click);
			// 
			// exportXMLButton
			// 
			this->exportXMLButton->Location = System::Drawing::Point(12, 204);
			this->exportXMLButton->Name = L"exportXMLButton";
			this->exportXMLButton->Size = System::Drawing::Size(156, 23);
			this->exportXMLButton->TabIndex = 6;
			this->exportXMLButton->Text = L"Export Songs";
			this->exportXMLButton->UseVisualStyleBackColor = true;
			this->exportXMLButton->Click += gcnew System::EventHandler(this, &Form1::exportXMLButton_Click);
			// 
			// filterTextBox
			// 
			this->filterTextBox->Location = System::Drawing::Point(174, 204);
			this->filterTextBox->Name = L"filterTextBox";
			this->filterTextBox->Size = System::Drawing::Size(156, 20);
			this->filterTextBox->TabIndex = 7;
			this->filterTextBox->TextChanged += gcnew System::EventHandler(this, &Form1::filterTextBox_TextChanged);
			// 
			// Form1
			// 
			this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
			this->ClientSize = System::Drawing::Size(347, 237);
			this->Controls->Add(this->filterTextBox);
			this->Controls->Add(this->exportXMLButton);
			this->Controls->Add(this->removeSongButton);
			this->Controls->Add(this->addSongButton);
			this->Controls->Add(this->songListDb);
			this->Controls->Add(this->songLength);
			this->Controls->Add(this->stopButton);
			this->Controls->Add(this->playButton);
			this->Name = L"Form1";
			this->Text = L"inconveniAMP";
			this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load);
			this->ResumeLayout(false);
			this->PerformLayout();

		}
#pragma endregion
	private: System::Void playButton_Click(System::Object^  sender, System::EventArgs^  e) 
			 {
				// Verify that a song is selected.
				 if( songListDb->SelectedIndex > -1 ) {
					 // Stop any playing song
					 player->stopSong();
					 // Create a standard string to send to the loadSong method of the file path from the song object relational array
					 player->loadSong( new string( systemToStdString( relationToListbox[ songListDb->SelectedIndex ]->getFilePath() ) ) );
					 // Play the track
					 player->playSong();
					 relationToListbox[ songListDb->SelectedIndex ]->updatePlays();
				 } else {
					 // Kick out an error if they have not selected a valid song in the queue
					 MessageBox::Show("Please select a song from the queue.");
				 }
			 }
	private: System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e) 
			 {
				 // Build our audio player class to handle the song files.
				 player = gcnew audioClass();

				 // Populate the listBox and relational array
				 this->refreshListBox();
			 }
	private: System::Void stopButton_Click(System::Object^  sender, System::EventArgs^  e) 
			 {
				 player->stopSong();
			 }
	private: System::Void addSongButton_Click(System::Object^  sender, System::EventArgs^  e) 
			 {
				 // Build the add song dialog
				 addSong ^add = gcnew addSong();

				 // Show the user the dialog
				 add->ShowDialog();

				 // When the dialog returns, refresh the elements in the queue list box.
				 this->refreshListBox();

			 }
	private: System::Void refreshListBox() 
			 {
				 // First we need to determine how many elements are going to be in the relational array to the list box.
				 // Build an array in relation to the listBox we have, to keep track of song IDs.
				 relationToListbox = gcnew array<song ^>( song::totalSongs() );

				 // Clears the listbox to prevent duplicate entries
				 songListDb->Items->Clear();

				 // Create database class
				 dbClass ^db = gcnew dbClass();

				 // Create empty query string
				 String ^query = gcnew String("");

				 // Select song list from database, check for filter
				 if( filterTextBox->Text == "" ) {
					 query += " SELECT * FROM songs ORDER BY ID ASC ";
				 } else {
					 query += " SELECT * FROM songs ";
					 query += " WHERE ";
					 query += " Ucase( title ) Like '%" + db->escape( filterTextBox->Text->ToUpper()  ) + "%' OR ";
					 query += " Ucase( artist ) Like '%" + db->escape( filterTextBox->Text->ToUpper() ) + "%' ";
					 query += " ORDER BY ID ASC ";
				 }

				 // Run select query
				 db->openConnection();
				 db->select( query );

				 // Counter we use to keep track of the relational array
				 int counter = 0;

				 // While there are rows to be read
				 while( db->getReader()->Read() ) {
					 // Build the song object for this part of the loop
					 // Add the song to the relational array
					 relationToListbox[counter] = gcnew song( db->getReader()->GetInt32( db->getReader()->GetOrdinal( "ID" ) ) );

					 // Start the string we are going to append to
					 String ^itemLabel = gcnew String("");

					 // Pull the information from the song object for the label
					 itemLabel += relationToListbox[counter]->getArtist();
					 itemLabel += " - ";
					 itemLabel += relationToListbox[counter]->getTitle();

					 // Add the label to the song listbox
					 songListDb->Items->Add( itemLabel );

					 // Increment Counter
					 counter++;
				 }
				 db->closeConnection();
			 }
private: System::Void removeSongButton_Click(System::Object^  sender, System::EventArgs^  e) 
		 {
			 // Make sure there is a song selected
			 if( songListDb->SelectedIndex > -1 ) {
				 // Prompt the user to verify they want to remove a certain song.
				 if ( MessageBox::Show("Are you sure you want to remove this song?", "Form Closing", MessageBoxButtons::YesNo, MessageBoxIcon::Question) == System::Windows::Forms::DialogResult::Yes ) {
					 // Cast the ID to a regular int.
					 int selectedIndex = (int)relationToListbox[ songListDb->SelectedIndex ]->getId();

					 // use the static function removeSong from the song class to
					 // remove the selected songId
					 song::removeSong( selectedIndex );

					 // Reload the queue
					 this->refreshListBox();
				 }
			 } else {
				 // Tell the user they need to select a song first.
				 MessageBox::Show("Please select a song from the queue.");
			 }
		 }
private: System::Void exportXMLButton_Click(System::Object^  sender, System::EventArgs^  e) 
		 {
			 // Set up filter for saving
			 saveFileDialog1->Filter = "XML (*.xml)|*.xml";
			 // Popup dialog for user
			 saveFileDialog1->ShowDialog();
			 // Convert system string to new string
			 string *fp = new string( systemToStdString( saveFileDialog1->FileName ) );
			 // Export to that location
			 song::exportXML( fp );
		 }
private: System::Void filterTextBox_TextChanged(System::Object^  sender, System::EventArgs^  e) {
			 this->refreshListBox();
		 }
};
}