#include "StrategyManager.h"

using namespace MyBot;

void StrategyManager::updateExpansionTask()
{
	if (BWAPI::Broodwar->getFrameCount() % 24 != 0) {
		return;
	}

	BWAPI::Unit targetEnemyUnit = nullptr;
	BWAPI::Position targetEnemyUnitPosition = BWAPI::Positions::None;

	if (BWAPI::Broodwar->self()->getRace() == BWAPI::Races::Zerg) {

		// 아군 전략에 따른 멀티 확장 타스크 : 선제적 멀티 확장
		if (true) {
		}


		// 적군 및 아군 상황에 따른 멀티 확장 타스크 
		if (true) {
						
			if (isGoodTimingToStartAnotherExpansion()) {

				// 섬멀티 불가능할때
				if (BWAPI::Broodwar->self()->getUpgradeLevel(BWAPI::UpgradeTypes::Ventral_Sacs) == 0) {
					insertTask(StrategicTask(TaskGoalType::Build_Expansion_On_Ground, BWAPI::UnitTypes::None, BWAPI::TechTypes::None, BWAPI::UpgradeTypes::None, nullptr, BWAPI::Positions::None,
						InformationManager::Instance().getOccupiedBaseLocations(InformationManager::Instance().selfPlayer).size() + 1));
				}
				// 섬멀티 가능할때
				else {
					insertTask(StrategicTask(TaskGoalType::Build_Expansion_On_Island, BWAPI::UnitTypes::None, BWAPI::TechTypes::None, BWAPI::UpgradeTypes::None, nullptr, BWAPI::Positions::None,
						InformationManager::Instance().getOccupiedBaseLocations(InformationManager::Instance().selfPlayer).size() + 1));
				}
			}

		}				
	}

}


// TODO : 확장 타이밍 결정
bool StrategyManager::isGoodTimingToStartAnotherExpansion()
{
	if (myMainBaseLocation == nullptr || enemyMainBaseLocation == nullptr){
		return false;
	}

	bool isGoodTiming = false;

	bool isNeeded = false;
	bool isSafe = false;
	bool hasEnoughResource = false;

	if (BWAPI::Broodwar->self()->supplyUsed() >= 40 * 2
		&& InformationManager::Instance().getOccupiedBaseLocations(InformationManager::Instance().selfPlayer).size() <= 1) {
		isNeeded = true;
	}
	if (BWAPI::Broodwar->self()->supplyUsed() >= 60 * 2
		&& InformationManager::Instance().getOccupiedBaseLocations(InformationManager::Instance().selfPlayer).size() <= 2) {
		isNeeded = true;
	}

	// 멀티는 최대 4개까지만 한다? 그래도 일꾼수 = 4 * 15 = 60마리
	// 5개이상 하면, 전투 유닛 숫자가 너무 적다?
	if (BWAPI::Broodwar->self()->supplyUsed() >= 80 * 2
		&& InformationManager::Instance().getOccupiedBaseLocations(InformationManager::Instance().selfPlayer).size() <= 3) {
		isNeeded = true;
	}

	// TODO : 네오 사우론 저그 : 프로토스 더블 넥서스 이거나, 테란 더블 커맨드센터 이면, 가까운 다른 스타팅포인트의 앞마당에 건설
	if (BWAPI::Broodwar->getFrameCount() < 20 * 60 * 8) {
		if (InformationManager::Instance().getOccupiedBaseLocations(InformationManager::Instance().selfPlayer).size() <= 2) {
			if (
				(InformationManager::Instance().enemyRace == BWAPI::Races::Protoss && InformationManager::Instance().getOccupiedRegions(InformationManager::Instance().enemyPlayer).size() >= 2)
				|| 
				(InformationManager::Instance().enemyRace == BWAPI::Races::Terran && InformationManager::Instance().getOccupiedRegions(InformationManager::Instance().enemyPlayer).size() >= 2)
			){
				isNeeded = true;
			}
		}
	}

	if (BWAPI::Broodwar->self()->completedUnitCount(BWAPI::UnitTypes::Zerg_Drone) >= 12) 
	{
		hasEnoughResource = true;
	}

	if (isDefenseEnough()){

		/*
		if (InformationManager::Instance().getUnitData(InformationManager::Instance().enemyPlayer).getMineralsLost() +
			InformationManager::Instance().getUnitData(InformationManager::Instance().enemyPlayer).getGasLost()
			>
			InformationManager::Instance().getUnitData(InformationManager::Instance().selfPlayer).getMineralsLost() +
			InformationManager::Instance().getUnitData(InformationManager::Instance().selfPlayer).getGasLost() + 500
		){
			isSafe = true;
		}
		*/
		isSafe = true;
	}

	if (Strategy_CombatTaskManager::Instance().combatState != Strategy_CombatTaskManager::CombatState::defenseMode) {
		isSafe = true;
	}

	isGoodTiming = isNeeded && isSafe && hasEnoughResource;

	return isGoodTiming;
}
