#include "~/Game Data/scripts/server/include/empire_lib.as"

import void init_remnant_ai() from "remnant_ai";
import void prep_remnant_ai_defaults(Empire@ emp) from "remnant_ai";

bool initialized = false;

/* Called for each empire to register any AI or defaults it might need */
void registerEmpireData(Empire@ emp) {
	if(isClient())
		return;

	if(!initialized) {
		loadDefaults(false);
		init_remnant_ai();
		initialized = true;
	}
	
	if(emp.isValid()) {
		// Prepare empire settings
		if (emp.ID == 1) {
			emp.setName(localize("#terran_empire"));
			emp.color = Color(0xff3388ff);
			emp.portrait = max(getPortraitID("Human"), 0);
			emp.background = getBackgroundID(emp.color);
			declareWar(emp, getEmpireByID(-2));
		}
		else if (emp.ID == 2) {
			emp.setName(localize("#good_chaps"));
			emp.color = Color(0xffffec33);
			emp.portrait = max(getPortraitID("Slug"), 0);
			emp.background = getBackgroundID(emp.color);
			emp.regScriptData("empire_ai", "TreatyDeciderAI");
		}
		else if (emp.ID == 3) {
			emp.setName(localize("#commies"));
			emp.color = Color(0xffff4600);
			declareWar(emp, getEmpireByID(1));
			emp.portrait = max(getPortraitID("Cyborg"), 0);
			emp.background = getBackgroundID(emp.color);
			emp.regScriptData("empire_ai", "TreatyDeciderAI");
		}
		else if (emp.ID == -3) {
			emp.regScriptData("remnant_ai", "RemnantAIData");
			prep_remnant_ai_defaults(emp);
		}

		if (emp.ID > 0 || emp.ID == -2) {
			if(emp.getShipLayoutCnt() == 0) {
				ResearchWeb web;
				web.prepare(emp);

				int designCount = defaultDesigns.length();
				for(int i = 0; i < designCount; ++i) {
					if (emp.isAI()) {
						if (!defaultDesigns[i].forAI)
							continue;
					}
					else {
						if (!defaultDesigns[i].forHuman)
							continue;
					}
					if (!defaultDesigns[i].canCreate(emp, web))
						continue;
					defaultDesigns[i].generateForEmpire(emp);
				}
			}
		}
	}
}

class TreatyDeciderAI {
	TreatyDeciderAI(Empire@ emp) {
	}

	void tick(Empire@ emp, float time) {
		Empire@ player = getEmpireByID(1);
		TreatyList treaties;
		treaties.prepare(emp);

		Treaty@ proposed = treaties.getProposedTreaty(player);
		if (proposed !is null)
			treaties.setAcceptance(player, emp.ID == 2);
	}

	void onDiplomaticMessage(Empire@ emp, Empire@ from, DiploMsg@ msg) {
	}
};

void registerTeam(Empire@ from, Empire@ to, int team) {
}

void registerHostilities(Empire@ from, Empire@ to) {
}

void registerSpecialEmpires() {
	// The pirates raid resources from any undefended systems
	Empire@ pirates = addEmpire(-2);
	pirates.setReserved(true);
	pirates.setName(localize("#NM_Pirates"));
	pirates.color = Color(0xff8c0000);

	// The remnants are leftover guardians of a fallen empire
	Empire@ remnants = addEmpire(-3);
	remnants.setReserved(true);
	remnants.setName(localize("#NM_Remnants"));
	remnants.color = Color(0xffccc5ab);
}
