//
//  Wumbo-Sound.cpp
//  Wumbo
//
//  Created by Wade McGillis on 12/13/13.
//  Copyright (c) 2013 Wade McGillis. All rights reserved.
//

#include "Wumbo-Sound.h"

#if PLATFORM_IOS
#include <fstream>
#include <sstream>
#include <UIKit/UIKit.h>
#include <AudioToolbox/AudioServices.h>
#include <Foundation/Foundation.h>
#include "Wumbo-Functions.h"
#endif

namespace Wumbo
{
	Sound::Sound()
	{
	#if PLATFORM_IOS
		volume = 15;
	#else
		volume = 24;
	#endif
	}

	Sound::~Sound()
	{
	#if PLATFORM_IOS
		//for(int i=0;i<soundbuffer.size();i++)
		//	[soundbuffer[i] release];
		soundbuffer.clear();
	#endif
	}


#if PLATFORM_APPLE
void Sound::load(NSString *fname, int buffercount = 1)
#else
void Sound::load(std::string fname, int buffercount = 1)
#endif
{
#if PLATFORM_IOS
	for(int i=0;i<buffercount;i++)
    {
		//NSURL *filePath = [NSURL fileURLWithPath:path isDirectory:NO];
		//AVAudioPlayer* avaudplay = [[AVAudioPlayer alloc] initWithContentsOfURL:filePath error:NULL];
		FinchSound* snd = new FinchSound();
		snd->initWithFile(fname);
		soundbuffer.push_back(snd);
    }
#elif PLATFORM_PC
#if PLATFORM_OSX
	if (!buffer.loadFromFile([fname UTF8String]))
	{
		printf("Error in Wumbo::Sound::load(): Could not find file named %s\n",[fname UTF8String]);
#else
		if (!buffer.loadFromFile(fname))
		{
			printf("Error in Wumbo::Sound::load(): Could not find file named %s\n",fname.c_str());
#endif
			return;
		}
		if (buffercount < 1)
			buffercount = 1;
		for(int i=0;i<buffercount;i++)
			soundbuffer.push_back(sf::Sound(buffer));
#endif
	}
	
	void Sound::loop()
	{
#if !PLATFORM_ANDROID
		for(int i=0;i<soundbuffer.size();i++)
		{
#if PLATFORM_IOS
			FinchSound* s = soundbuffer[i];
			if (s->isPlaying() == false)//(s.isPlaying == NO)
			{
				s->setGain((float)volume/100.f);
				s->setLooping(true);
				break;
			}
#elif PLATFORM_PC
			//printf("PLAY THE SOUND!");
			if (soundbuffer[i].getStatus() != sf::Sound::Playing)
			{
				soundbuffer[i].setLoop(true);
				soundbuffer[i].setVolume(volume);
				soundbuffer[i].play();
				break;
			}
#endif
		}
#endif
		//#endif
	}
	
	void Sound::play()
	{
#if !PLATFORM_ANDROID
		for(int i=0;i<soundbuffer.size();i++)
		{
#if PLATFORM_IOS
			FinchSound* s = soundbuffer[i];
			if (s->isPlaying() == false)//(s.isPlaying == NO)
			{
				s->setGain((float)volume/100.f);
				s->play();
				break;
			}
#elif PLATFORM_PC
			//printf("PLAY THE SOUND!");
			if (soundbuffer[i].getStatus() != sf::Sound::Playing)
			{
				soundbuffer[i].setLoop(false);
				soundbuffer[i].setVolume(volume);
				soundbuffer[i].play();
				break;
			}
#endif
		}
#endif
	}
	
	void Sound::stopAll()
	{
#if !PLATFORM_ANDROID
		for(int i=0;i<soundbuffer.size();i++)
		{
#if PLATFORM_IOS
			FinchSound* s = soundbuffer[i];
			s->stop();
#elif PLATFORM_PC
			soundbuffer[i].stop();
#endif
		}
#endif
	}
	
	
	
	int Sound::length()
	{
#if PLATFORM_IOS
		return soundbuffer[0]->getDuration()*1000;
#elif PLATFORM_PC
		printf("Sound duration: %i\n",buffer.getDuration().asMilliseconds());
		return buffer.getDuration().asMilliseconds();
#endif
	}
	
	void Sound::setVolume(int vol)
	{
		volume = vol;
	}
};