#include "music_test.h"

#include "scenenode.h"

#include <glm/gtx/transform.hpp>
// glm::perspective
#include <glm/gtc/matrix_projection.hpp>
// glm::translate, glm::rotate, glm::scale
#include <glm/gtc/matrix_transform.hpp>
// glm::value_ptr
#include <glm/gtc/type_ptr.hpp> 

#include "transformation_node.h"

using namespace std;

music_test::music_test()
{}


music_test::~music_test()
{}


void music_test::errcheck(FMOD_RESULT result)
{
	if(result != FMOD_OK)
	{
		printf("FMOD error! (%d) %s\n", result, FMOD_ErrorString(result));
		exit(-1);
	}
}


bool music_test::init()
{

	// Create a system object and initialize it
	result = FMOD::System_Create(&system);
	errcheck(result);
	
	result = system->getVersion(&version);
	errcheck(result);

	if(version < FMOD_VERSION)
	{
		printf("Error! You are using an old version of FMOD %08x. This program requires %08x\n", version, FMOD_VERSION);
		return 0;
	}

	result = system->init(1, FMOD_INIT_NORMAL, 0);
	errcheck(result);

	result = system->createStream("../../media/medley.mp3", FMOD_HARDWARE | FMOD_LOOP_NORMAL | FMOD_2D, 0, &sound);
	errcheck(result);

	cout << "============================" << endl;
	cout << "Play medley" << endl;
	cout << endl;
	cout << "Press space to pause, Esc to quit" << endl;
	cout << endl;

	//Now play the sound
	result = system->playSound(FMOD_CHANNEL_FREE, sound, false, &channel);
	errcheck(result);

	//main loop
	do
    {
        if (kbhit())
        {
            key = getch();

            switch (key)
            {
                case ' ' :
                {
                    bool paused;
                    channel->getPaused(&paused);
                    channel->setPaused(!paused);
                    break;
                }
            }
        }

        system->update();

        if (channel)
        {
            unsigned int ms;
            unsigned int lenms;
            bool         playing;
            bool         paused;

            channel->isPlaying(&playing);
            if ((result != FMOD_OK) && (result != FMOD_ERR_INVALID_HANDLE))
            {
                errcheck(result);
            }

            result = channel->getPaused(&paused);
            if ((result != FMOD_OK) && (result != FMOD_ERR_INVALID_HANDLE))
            {
                errcheck(result);
            }

            result = channel->getPosition(&ms, FMOD_TIMEUNIT_MS);
            if ((result != FMOD_OK) && (result != FMOD_ERR_INVALID_HANDLE))
            {
                errcheck(result);
            }

            result = sound->getLength(&lenms, FMOD_TIMEUNIT_MS);
            if ((result != FMOD_OK) && (result != FMOD_ERR_INVALID_HANDLE))
            {
                errcheck(result);
            }

            printf("Time %02d:%02d:%02d/%02d:%02d:%02d : %s\r", ms / 1000 / 60, ms / 1000 % 60, ms / 10 % 100, lenms / 1000 / 60, lenms / 1000 % 60, lenms / 10 % 100, paused ? "Paused " : playing ? "Playing" : "Stopped");
        }

        Sleep(10);

    } while (key != 27);



	return true;
}


void music_test::cleanup()
{
	result = sound->release();
	errcheck(result);
	result = system->close();
	errcheck(result);
	result = system->release();
	errcheck(result);
}