
#include "MW4Headers.hpp"

#include "AI_Statistics.hpp"

#include <windows.h>
#include <buildnum\curver.h>
#include <gameos\registry.hpp>
#include <Adept\NameTable.hpp>
#include "data_client.hpp"
#include "aiutils.hpp"
#include "gameinfo.hpp"
#include <Adept\Application.hpp>
#include <Adept\DamageObject.hpp>


// NOTE: this has all been (permanently?) commented out since no
// one is using it.  -- PAULTOZ

using namespace MW4AI;
using namespace MW4AI::Statistics;

namespace MW4AI
{
	namespace Statistics
	{
		bool g_StatisticsEnabled = false;
	};
};


void Statistics::Init()
{
#ifdef LAB_ONLY
	gos_PushCurrentHeap(Heap);
	FILETIME ft;
	GetSystemTimeAsFileTime(&ft);
	SYSTEMTIME st;
	FileTimeToSystemTime(&ft,&st);

	std::string s;
	s += IntToString(st.wMonth);
	s += "/";
	s += IntToString(st.wDay);
	s += "/";
	s += IntToString(st.wYear);
	s += " ";
	s += IntToString(st.wHour);
	s += ":";
	s += IntToString(st.wMinute);

	char computer_name[200];
	DWORD name_size = 200;
	GetComputerName(computer_name,&name_size);

	char user_name[200];
	DWORD username_size = 200;
	GetUserName((char*)&user_name,&username_size);

#if 0
	if (!strcmp (user_name,"dabzug"))
		g_AbzugFlag = true;
	else
		g_AbzugFlag = false;
	if (s_NoAbzug)
		g_AbzugFlag = false;
#endif

	DataClient::Add_StartMission((float)gos_GetElapsedTime());
	DataClient::Add_BuildVersion(CURRENT_BUILD_FULL_STRING);
	DataClient::Add_CompileDate(__DATE__);
	DataClient::Add_CompileTime(__TIME__);
	DataClient::Add_UserName(user_name);
	DataClient::Add_MachineName(computer_name);
	DataClient::Add_DateTime(s.c_str());
	DataClient::Add_MissionName(MWGameInfo::g_MissionName);
	DataClient::Add_PlayerMech(MWGameInfo::g_PlayerMech);
	DataClient::Add_NetworkingFlag(Application::GetInstance()->networkingFlag);
	gos_PopCurrentHeap();
#endif
}

void Statistics::Quit()
{
#ifdef LAB_ONLY
	DataClient::Add_EndMission((float)gos_GetElapsedTime());
#endif
}

std::string LookupName(int id)
{
	if (NameTable::GetInstance() == 0)
	{
		return ("");
	}

	NameTableEntry* entry = NameTable::GetInstance()->FindEntry(id);
	if (entry == 0)
	{
		return ("");
	}

	return (entry->objectName);
}

void Statistics::NotifyDamageTaken(int receiver_id, int sender_id, float damage, int damage_type)
{
#ifdef LAB_ONLY
	std::string receiver_name(LookupName(receiver_id));
	std::string sender_name(LookupName(sender_id));

	std::string damage_type_name;
	switch (damage_type)
	{
		case Adept::BeamDamageType:
		{
			damage_type_name = "Beam Damage";
			break;
		}

		case Adept::MissileDamageType:
		{
			damage_type_name = "Missile Damage";
			break;
		}

		case Adept::ProjectileDamageType:
		{
			damage_type_name = "Projectile Damage";
			break;
		}

		case Adept::SplashDamageType:
		{
			damage_type_name = "Splash Damage";
			break;
		}

		case Adept::HeatDamageType:
		{
			damage_type_name = "Heat Damage";
			break;
		}
// MSL 5.03 Lava
		case Adept::LavaDamageType:
		{
			damage_type_name = "Lava Damage";
			break;
		}

		case Adept::AmmoFireDamageType:
		{
			damage_type_name = "Ammo Fire Damage";
			break;
		}

	}

	DataClient::Add_Damage(receiver_id,receiver_name.c_str(),sender_id,sender_name.c_str(),damage,damage_type_name.c_str(),(float)gos_GetElapsedTime());
#endif
}

void Statistics::NotifyDestroyed(int destroyed_id)
{
#ifdef LAB_ONLY
	std::string destroyed_name(LookupName(destroyed_id));

	DataClient::Add_Destroyed(destroyed_id,destroyed_name.c_str(),(float)gos_GetElapsedTime());
#endif
}

void Statistics::NotifyObjective(char* objective_name, bool succeeded)
{
#ifdef LAB_ONLY
	DataClient::Add_Objective(objective_name,succeeded,(float)gos_GetElapsedTime());
#endif
}

void Statistics::NotifyToHitRoll(int shooter_id, float base_to_hit, float modified_to_hit, float rolled)
{
#ifdef LAB_ONLY
	std::string shooter_name(LookupName(shooter_id));
	DataClient::Add_ToHitRoll(shooter_name.c_str(),shooter_id,base_to_hit,modified_to_hit,rolled,(float)gos_GetElapsedTime());
#endif
}

bool Statistics::Enabled()
{
//#ifdef LAB_ONLY
	return (g_StatisticsEnabled);
//#endif
}

void Statistics::SetEnabled(bool enabled)
{
#ifdef LAB_ONLY
	g_StatisticsEnabled = enabled;
#endif
}
