#include "base.h"
#include <iostream>
#include "SoundEffect.h"

SoundEffect::SoundEffect(const std::string& path, int channel)
	: m_pMixChunk { Mix_LoadWAV(path.c_str()) }, m_Channel(channel) {
	if (m_pMixChunk == nullptr) {
		const std::string errorMsg = "SoundEffect: Failed to load " + path + ",\nSDL_mixer Error: " + Mix_GetError();
		std::cerr << errorMsg;
	}
}
SoundEffect::~SoundEffect() {
	Mix_FreeChunk(m_pMixChunk);
	m_pMixChunk = nullptr;
}

bool SoundEffect::IsLoaded() const {
	return m_pMixChunk != nullptr;
}

void SoundEffect::Play(const int loops) const {
	if (m_pMixChunk != nullptr) {
		Mix_PlayChannel(m_Channel, m_pMixChunk, loops);
	}
	else {
		std::cout << "SoundEffect::Play() failed, sound effect not loaded\n";
	}
}

void SoundEffect::SetVolume(const int value) {
	if (m_pMixChunk != nullptr) {
		Mix_VolumeChunk(m_pMixChunk, value);
	}
}

int SoundEffect::GetVolume() const {
	if (m_pMixChunk != nullptr) {
		return Mix_VolumeChunk(m_pMixChunk, -1);
	}
	else {
		return -1;
	}
}

void SoundEffect::Stop() const {
	Mix_HaltChannel(m_Channel);
}

void SoundEffect::StopAll() {
	Mix_HaltChannel(-1);
}



void SoundEffect::PauseAll() {
	Mix_Pause(-1);
}
void SoundEffect::ResumeAll() {
	Mix_Resume(-1);
}
bool SoundEffect::IsPlaying() const {
	return Mix_Playing(m_Channel);
}
