
#include "MW4Headers.hpp"

#include "AI_LancemateAudio.hpp"

#include <Adept\AudioRenderer.hpp>
#include "LancemateDefines.hpp"
#include "Mech.hpp"
#include "CombatAI.hpp"
#include <Adept\AudioCommand.hpp>
#include <Adept\Application.hpp>



using namespace MW4AI;
using namespace MW4AI::LancemateAudio;

const Stuff::Scalar audio_update_frequency = 1.0f;
const Stuff::Scalar min_audio_replay_time = 60.0f;
const Stuff::Scalar min_mandatory_audio_replay_time = 1.0f;
const Stuff::Time min_idle_time = 65.0f;


AudioManager::AudioManager()
	: m_LastTimeAnyAudioPlayed(0)
	, m_LastAudioUpdate(0)
	, m_PissedOff(false)
{
	m_LastTimeNotInactive = 0;
}

AudioManager::~AudioManager()
{
}

void AudioManager::Update(MechWarrior4::Group& group, MechWarrior4::Mech& leader)
{
	if ((m_LastAudioUpdate != 0) &&
		(m_LastAudioUpdate + audio_update_frequency > gos_GetElapsedTime()))
	{
		return;
	}

	if (group.GetElements().size() > 1)
	{
		std::vector<MWObject*> members;
		group.GetMembers(members);

		if (members.size() > 1)
		{
			members.erase(members.begin());

			{for (std::vector<MWObject*>::iterator i = members.begin();
				  i != members.end();
				  ++i)
			{
				if (((*i)->GetAI() != 0) &&
					((*i)->GetAI()->CanAct() == true) &&
					((*i)->GetAI()->IsDerivedFrom(CombatAI::DefaultData) == true))
				{
					CombatAI* combat_ai = Cast_Object(CombatAI*,(*i)->GetAI());

					if ((combat_ai->MoveDone() == false) ||
						(combat_ai->GetAttackState() == CombatAI::ATTACKING))
					{
						m_LastTimeNotInactive = gos_GetElapsedTime();
						break;
					}
				}
			}}
		}
		else
		{
			m_LastTimeNotInactive = gos_GetElapsedTime();
		}
	}

	if ((m_LastTimeNotInactive != 0) &&
		(m_LastTimeNotInactive + min_idle_time < gos_GetElapsedTime()) &&
		(Adept::Application::GetInstance()->networkingFlag == false))
	{
		PlayAudio(AUDIO_IDLE,"");
		m_LastTimeNotInactive = gos_GetElapsedTime();
	}

	m_LastAudioUpdate = gos_GetElapsedTime();
}

void AudioManager::PlayAudio(Category category, const Stuff::MString& talker_suffix)
{
	MString sound_name("VO\\Generic\\");
	bool apply_talker_suffix = true;

// MSL 5.03 Modified wav file called for check fire messages
	switch (category)
	{
		case AUDIO_FF1:
		{

//			sound_name += "CheckFire1";
//			break;
			sound_name += "FriendlyTarget_Bet.wav{handle}";
			apply_talker_suffix = false;
			break;
		}
		case AUDIO_FF2:
		{
//			sound_name += "CheckFire2";
//			break;
			sound_name += "FriendlyTarget_Bet.wav{handle}";
			apply_talker_suffix = false;
			break;
		}
		case AUDIO_FF3:
		{
//			sound_name += "CheckFire1";
//			break;
			sound_name += "FriendlyTarget_Bet.wav{handle}";
			apply_talker_suffix = false;
			break;
		}
		case AUDIO_FF4:
		{
			sound_name += "KillIan1_RAT.wav{handle}";
			apply_talker_suffix = false;
			break;
		}
		case AUDIO_OK:
		{
			if (m_PissedOff == true)
			{
				sound_name += "RogerBummed";
			}
			else
			{
				switch (gos_rand() & 0x03)
				{
					case 0:
					{
						sound_name += "Roger1";
						break;
					}
					case 1:
					{
						sound_name += "Roger2";
						break;
					}
					case 2:
					{
						sound_name += "RogerHarried";
						break;
					}
					case 3:
					{
						sound_name += "Copy";
						break;
					}
				}
			}

			break;
		}
		case AUDIO_OKX:
		{
			sound_name += "RogerBummed";
			break;
		}
		case AUDIO_NO:
		{
			switch (gos_rand() & 0x01)
			{
				case 0:
				{
					sound_name += "NoComply1";
					break;
				}
				case 1:
				{
					sound_name += "NoComply2";
					break;
				}
			}

			break;
		}
		case AUDIO_TARGETDESTROYED:
		{
			switch (gos_rand() & 0x01)
			{
				case 0:
				{
					sound_name += "TargetDestroyed1";
					break;
				}
				case 1:
				{
					sound_name += "TargetDestroyed2";
					break;
				}
			}

			break;
		}
		case AUDIO_ATNAV:
		{
			sound_name += "AtNav";
			break;
		}
		case AUDIO_EJECT:
		{
			sound_name += "Ejecting";

			switch (gos_rand() % 3)
			{
				case 0:		{	sound_name += "1";	break;	}
				case 1:		{	sound_name += "2";	break;	}
				default:	break;
			}
			break;
		}

		case AUDIO_IDLE:
		{
			sound_name += "Figit";

			switch (gos_rand() % 3)
			{
				case 0:		{	sound_name += "1";	break;	}
				case 1:		{	sound_name += "2";	break;	}
				case 2:		{	sound_name += "3";	break;	}
			}

			sound_name += "_RAT.wav{handle}";
			apply_talker_suffix = false;
			break;
		}
	}

	if (apply_talker_suffix == true)
	{
		sound_name += talker_suffix;
		sound_name += ".wav{handle}";
	}

	AudioCommand *command = AudioCommand::Create(AudioRenderer::VOType, sound_name, 1.0f, 1.0f);
	if (command)
	{
		Check_Object(command);
		command->Play();
	}

//	PlayAudio(*randomly_chosen);

	m_LastTimeAnyAudioPlayed = gos_GetElapsedTime();
	m_LastTimeAudioPlayed[category] = gos_GetElapsedTime();
}

void AudioManager::PlayAudio(Category category, const MechWarrior4::AI& ai)
{
    if (&ai) {
        PlayAudio(category, ai.GetTalkerSuffix());
    }
}

void AudioManager::AddAudioCue(Category category, int sound_id)
{
	Verify(category >= 0);
	Verify(category < MAX_AUDIO_CATEGORIES);

// todo
}

void AudioManager::SetPissedOff(bool pissed_off)
{
	m_PissedOff = pissed_off;
}
