#include "cAudio.hpp"

/*** Custom Header Files ***/
#include "../UtilityEngine/cUtility.hpp"

using AudioEngine::cAudio;
using UtilityEngine::cUtility;

/*static*/ cAudio* cAudio::sp_inst = nullptr;

cAudio::cAudio()
: m_rate(22050), m_format(AUDIO_S16), m_channels(2), m_buffers(4096)
{}

cAudio::~cAudio()
{
	CleanUp();
}

//Functions
/*static*/ cAudio& cAudio::Inst()
{
	if (sp_inst == nullptr)
	    sp_inst = new cAudio();
	return *sp_inst;
}

/*static*/ void cAudio::Delete()
{
	delete sp_inst;
	sp_inst = nullptr;
}

const bool cAudio::Initialize() const
{
	bool rtn = IsInit();

	if (rtn == false) {
		if(SDL_InitSubSystem(SDL_INIT_AUDIO) == 0) {
			cUtility::Inst().Message("Audio initialized.");
			rtn = true;
		} else
			cUtility::Inst().Message("Could not initialize Audio. SDL_InitSubSystem(SDL_INIT_AUDIO):", __AT__, cUtility::eTypeSDL::SDL);
	}

	return rtn;
}

void cAudio::Setup()
{
	CleanUp();

	if (Mix_OpenAudio(m_rate, Uint16(m_format), m_channels, m_buffers) < 0)
		cUtility::Inst().Message("Could not setup mixer. Mix_OpenAudio():", __AT__, cUtility::eTypeSDL::MIXER);

	//Mix_AllocateChannels(number_of_mixing_channels); 
}

void cAudio::CleanUp()
{
	Mix_CloseAudio();
}

void cAudio::SoundPlay()
{
	
}
void cAudio::MusicPlay()
{

}

///Sets
void cAudio::setRate( const unsigned long int rate /*= 22050*/ )
{
	m_rate = rate;
}

void cAudio::setFormat( const unsigned long int format /*= AUDIO_S16*/ )
{
	m_format = format;
}

void cAudio::setChannels( const unsigned long int channels /*= 2*/ )
{
	m_channels = channels;
}

void cAudio::setBuffers( const unsigned long int buffers /*= 4096*/ )
{
	m_buffers = buffers;
}

///Gets
const bool cAudio::IsInit() const
{
	bool rtn = false;

	if (SDL_WasInit(SDL_INIT_AUDIO) != 0) {
		cUtility::Inst().Message("Audio is initialized.");
		rtn = true;
	} else
		cUtility::Inst().Message("Audio is not initialized.");

	return rtn;
}

const unsigned long int cAudio::getRate() const
{
	return m_rate;
}

const unsigned long int cAudio::getFormat() const
{
	return m_format;
}

const unsigned long int cAudio::getChannels() const
{
	return m_channels;
}

const unsigned long int cAudio::getBuffers() const
{
	return m_buffers;
}