/*
Solar Conquest
Copyright (C) 2006 Greg Beaman

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include <SDL_mixer.h>

#define NO_SOUND

class CSoundListNode
{
private:
	int id;
	Mix_Chunk* sound;

public:
	CSoundListNode* prevNode;
	CSoundListNode* nextNode;

	CSoundListNode(int nid, char* fileName);
	~CSoundListNode();

	int GetId();
	Mix_Chunk* GetSound();
};

CSoundListNode::CSoundListNode(int nid, char* fileName)
{
	printf("Loading sound ");
	printf(fileName);
	printf("\n");
	
	nextNode = NULL;
	prevNode = NULL;
	id = nid;

	sound = Mix_LoadWAV(fileName);
	if (!sound)
		printf("-Error on loading sound\n");
}

CSoundListNode::~CSoundListNode()
{
	if (sound)
		Mix_FreeChunk(sound);
}

int CSoundListNode::GetId() { return id; }
Mix_Chunk* CSoundListNode::GetSound() { return sound; }

class CSoundList
{
private:
	CSoundListNode* firstNode;
	bool audioEnabled;

public:
	CSoundList();
	~CSoundList();

	CSoundListNode* AddSound(int id, char* fileName);
	CSoundListNode* FindNode(int id);
	void DeleteSound(int id);
	void PlaySound(CSoundListNode* soundNode);
};

CSoundList::CSoundList()
{
	firstNode = NULL;
	
#ifndef NO_SOUND
//	if (Mix_OpenAudio(44100,AUDIO_S16SYS,2,1024) < 0)
	if (Mix_OpenAudio(22050,AUDIO_S16SYS,2,1024) < 0)
	{
		printf("Can not open Audio device - %s\n", Mix_GetError());
		audioEnabled = false;
	} else { audioEnabled = true; }
#else
	audioEnabled = false;
#endif
}

CSoundList::~CSoundList()
{
	if (!audioEnabled)
		return;
	
	CSoundListNode* cNode;
	CSoundListNode* tNode;
	cNode = firstNode;
	while (cNode)
	{
		tNode = cNode->nextNode;
		delete cNode;
		cNode = tNode;
	}
	firstNode = NULL;
	
	Mix_CloseAudio();
}

CSoundListNode* CSoundList::AddSound(int id, char* fileName)
{
	if (!audioEnabled)
		return NULL;
		
	CSoundListNode* newNode;
	if (!firstNode)
		newNode = new CSoundListNode(id,fileName);
	else
		newNode = new CSoundListNode(id,fileName);
	if (firstNode)
	{
		newNode->nextNode = firstNode;
		firstNode->prevNode = newNode;
	}
	firstNode = newNode;
	return newNode;
}

CSoundListNode* CSoundList::FindNode(int id)
{
	if (!audioEnabled)
		return NULL;
		
	CSoundListNode* cNode = firstNode;
	while (cNode)
	{
		if (cNode->GetId() == id)
			return cNode;
		cNode = cNode->nextNode;
	}
	return NULL;
}

void CSoundList::DeleteSound(int id)
{
	if (!audioEnabled)
		return;
		
	CSoundListNode* delNode = FindNode(id);
	if (!delNode)
		return;
	if (delNode->prevNode)
		delNode->prevNode->nextNode = delNode->nextNode;
	else
		firstNode = delNode->nextNode;
	if (delNode->nextNode)
		delNode->nextNode->prevNode = delNode->prevNode;
	delete delNode;
}

void CSoundList::PlaySound(CSoundListNode* soundNode)
{
	if (!audioEnabled || !soundNode || !g_soundEnabled)
		return;
	Mix_Chunk* sound = soundNode->GetSound();
	if (!sound)
		return;
	Mix_PlayChannel(-1,sound,1);
}

CSoundList* g_soundList = NULL;

