/*
* Tyr is an AI for StarCraft: Broodwar, 
* 
* Please visit https://github.com/SimonPrins/Tyr for further information.
* 
* Copyright 2015 Simon Prins
*
* This file is part of Tyr.
* Tyr is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
* Tyr is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with Tyr.  If not, see http://www.gnu.org/licenses/.
*/


package com.tyr.builds;

import java.util.ArrayList;

import bwapi.Game;
import bwapi.Player;
import bwapi.Position;
import bwapi.Race;
import bwapi.UnitType;
import bwapi.UpgradeType;
import bwta.BWTA;

import com.tyr.EnemyManager;
import com.tyr.EnemyPosition;
import com.tyr.Settings;
import com.tyr.Tyr;
import com.tyr.UnitTracker;
import com.tyr.agents.Agent;
import com.tyr.agents.Attack;
import com.tyr.buildingplacement.SpaceManager;
import com.tyr.requirements.ConjRequirement;
import com.tyr.requirements.CostRequirement;
import com.tyr.requirements.UnitRequirement;
import com.tyr.tasks.BuildAtLocationTask;
import com.tyr.tasks.ConstantPushSolution;
import com.tyr.tasks.ConstantPushTask;
import com.tyr.tasks.LocalDefenseTask;


public class DragoonPush extends CompositeBuildOrder
{
	private ConstantPushTask constantPushTask;
	private LocalDefenseTask localDefenseTask;
	private LocalDefenseTask thirdDefenseTask;
	private boolean cannonDefense;
	private boolean earlyZealots;
	private boolean takeExpands;
	private boolean defensesInitialized;
	private boolean expandFirst = true;
	private boolean detectionNeeded;
	private boolean attackStarted;
	private boolean upgradeLegSpeed;

	private int armySize = 25;
	private int retreatSize = 10;
	
	public DragoonPush(boolean cannonDefense, boolean detectionNeeded, boolean earlyZealots, boolean takeExpands)
	{
		this.cannonDefense = cannonDefense;
		defensesInitialized = !cannonDefense;
		this.detectionNeeded = detectionNeeded;
		this.earlyZealots = earlyZealots;
		this.takeExpands = takeExpands;
	}
	
	@Override
	public void initialize(Game game, Player self, Tyr bot)
	{
		Settings.setRequiredSize(100);
		Settings.setMaximumSize(100);
		Settings.setMaximumWorkers(40);
		Settings.setSmallInvasionDist(480);
		Settings.setLargeInvasionDist(768);
		Settings.setWorkersPerGas(2);
		
		Attack.dontWaitAtCannon = true;
		
		this.add(new ExpandPart(true));
		this.add(new WorkerScoutPart(1600));
		ExpandPart.maximumCcs = 2;
		
		localDefenseTask = new LocalDefenseTask(BWTA.getRegion(self.getStartLocation()), 0);
		bot.taskManager.potentialTasks.add(localDefenseTask);
		
		thirdDefenseTask = new LocalDefenseTask(BWTA.getRegion(self.getStartLocation()), 0);
		bot.taskManager.potentialTasks.add(thirdDefenseTask);
		constantPushTask = new ConstantPushTask(null, ConstantPushSolution.ATTACK);
		constantPushTask.stop = true;
		bot.taskManager.potentialTasks.add(constantPushTask);
		
		ConstantPushTask.prioritizeExpands = true;
		
		super.initialize(game, self, bot);
	}
	
	private Position getDefensivePosition()
	{
		Position location = SpaceManager.getNatural();
	
		ArrayList<Position> minerals = new ArrayList<Position>();
		// Find all the mineral patches close to the base.
		for (EnemyPosition neutralUnit : EnemyManager.getManager().neutralBuildingMemory)
			if (neutralUnit.type.isMineralField() && neutralUnit.pos.getDistance(location) <= 270)
	        	minerals.add(neutralUnit.pos);
	        
		return SpaceManager.getDefensePosPositions(location, minerals);
	}
	
	@Override
	public void onFrame(Game game, Player self, Tyr bot) 
	{
		super.onFrame(game, self, bot);
		
		if (game.enemy().getRace() == Race.Zerg
				&& UnitTracker.count(UnitType.Protoss_Zealot) < 6)
		{
			Settings.setWorkersPerGas(3);
			
			//if we're running out of supply and have enough minerals ...
			if ((self.supplyTotal() + UnitTracker.getSupplyConstructing() - self.supplyUsed() 
						<= UnitTracker.count(UnitType.Protoss_Gateway)*3 + UnitTracker.getCcCount() * 3)
					&& (bot.getAvailableMinerals() >= 100))
			{
				bot.spaceManager.build(UnitType.Protoss_Pylon);
			}
			
			//if we've the resources to build a Gateway ...
			if (bot.getAvailableMinerals() >= 150
					&& UnitTracker.count(UnitType.Protoss_Gateway) == 0
					&& self.completedUnitCount(UnitType.Protoss_Pylon) > 0)
			{
				bot.spaceManager.build(UnitType.Protoss_Gateway);
			}
			
			//if we've the resources to build a Gateway ...
			if (bot.getAvailableMinerals() >= 150
					&& UnitTracker.count(UnitType.Protoss_Gateway) < 2)
			{
				bot.spaceManager.build(UnitType.Protoss_Gateway);
			}
			
			return;
		}
		
		if (!defensesInitialized && game.getFrameCount() >= 100)
		{
			defensesInitialized = true;
			
			Position defensivePosition = getDefensivePosition();
				
			BuildAtLocationTask buildTask = new BuildAtLocationTask(defensivePosition, true);
			buildTask.addBuilding(UnitType.Protoss_Pylon, new UnitRequirement(UnitType.Protoss_Probe, 7));
			buildTask.addBuilding(UnitType.Protoss_Forge, new ConjRequirement()
				.addRequirement(new UnitRequirement(UnitType.Protoss_Pylon, 1, true))
				.addRequirement(new CostRequirement(100, 0)));
			buildTask.addBuilding(UnitType.Protoss_Photon_Cannon, new ConjRequirement()
			.addRequirement(new UnitRequirement(UnitType.Protoss_Forge, 1, true))
			.addRequirement(new CostRequirement(150, 0)));
			if (earlyZealots)
			{
				buildTask.addBuilding(UnitType.Protoss_Gateway, new ConjRequirement()
				.addRequirement(new CostRequirement(150, 0)));
			}
			else
			{
				buildTask.addBuilding(UnitType.Protoss_Gateway, new ConjRequirement()
				.addRequirement(new CostRequirement(150, 0))
				.addRequirement(new UnitRequirement(UnitType.Protoss_Nexus, 2, true)));
			}
			bot.taskManager.potentialTasks.add(buildTask);
		}
		
		if (UnitTracker.count(UnitType.Protoss_Gateway) >= 12)
			Settings.setWorkersPerGas(3);
		
		if (game.enemy().getRace() != Race.Zerg
				&& (self.getUpgradeLevel(UpgradeType.Leg_Enhancements) > 0 
						|| EnemyManager.getManager().getEnemyTypes().contains(UnitType.Protoss_Scarab)))
			armySize = 20;

		if (constantPushTask != null && self.completedUnitCount(UnitType.Protoss_Zealot) + self.completedUnitCount(UnitType.Protoss_Dragoon) <= retreatSize)
			constantPushTask.stop = true;
		if (constantPushTask != null && self.completedUnitCount(UnitType.Protoss_Zealot) + self.completedUnitCount(UnitType.Protoss_Dragoon) >= armySize)
		{
			constantPushTask.stop = false;
			attackStarted = true;
			localDefenseTask.setDefendersNeeded(3);
			if (takeExpands)
				ExpandPart.maximumCcs = 100;
			if (game.enemy().getRace() == Race.Zerg)
				detectionNeeded = true;
		}
		
		if (thirdDefenseTask.getDefendersNeeded() == 0 && Tyr.bot.workForce.mineralWorkers.size() >= 3)
		{
			thirdDefenseTask.setDefendersNeeded(3);
			thirdDefenseTask.setDefendedPosition(Tyr.bot.workForce.mineralWorkers.get(2).resourceDepot.getPosition());
		}
		
		if (self.completedUnitCount(UnitType.Protoss_Zealot) + self.completedUnitCount(UnitType.Protoss_Dragoon) >= 9)
			upgradeLegSpeed = true;
			
		if (EnemyManager.getManager().getEnemyTypes().contains(UnitType.Protoss_Dark_Templar) ||EnemyManager.getManager().getEnemyTypes().contains(UnitType.Zerg_Lurker))
			detectionNeeded = true;
		
		//if we're running out of supply and have enough minerals ...
		if ((self.supplyTotal() + UnitTracker.getSupplyConstructing() - self.supplyUsed() 
					<= UnitTracker.count(UnitType.Protoss_Gateway)*3 + UnitTracker.getCcCount() * 3)
				&& (bot.getAvailableMinerals() >= 100)
				&& self.supplyTotal() + UnitTracker.getSupplyConstructing() < 400
				&& (!cannonDefense || UnitTracker.count(UnitType.Protoss_Pylon) > 0))
		{
			bot.spaceManager.build(UnitType.Protoss_Pylon);
		}
		
		//if we've the resources to build a Gateway ...
		if ((UnitTracker.count(UnitType.Protoss_Nexus) >= 2 || !expandFirst)
				&& bot.getAvailableMinerals() >= 150
				&& UnitTracker.count(UnitType.Protoss_Gateway) == 0
				&& self.completedUnitCount(UnitType.Protoss_Pylon) > 0
				&& UnitTracker.count(UnitType.Protoss_Pylon) > 0)
		{
			bot.spaceManager.build(UnitType.Protoss_Gateway);
		}
		
		//if we've the resources to build a Gateway ...
		if ((UnitTracker.count(UnitType.Protoss_Nexus) >= 2 || !expandFirst)
				&& (UnitTracker.count(UnitType.Protoss_Nexus) >= 2 || bot.getAvailableMinerals() >= 500 || !expandFirst)
				&& bot.getAvailableMinerals() >= 150
				&& UnitTracker.count(UnitType.Protoss_Gateway) < 2
				&& UnitTracker.count(UnitType.Protoss_Cybernetics_Core) > 0)
		{
			bot.spaceManager.build(UnitType.Protoss_Gateway);
		}
		
		if (!pauseProduction())
		{
			if ((UnitTracker.count(UnitType.Protoss_Nexus) >= 2 || !expandFirst)
					&& bot.getAvailableMinerals() >= 250
					&& UnitTracker.count(UnitType.Protoss_Gateway) < 4
					&& (UnitTracker.count(UnitType.Protoss_Cybernetics_Core) > 0))
			{
				bot.spaceManager.build(UnitType.Protoss_Gateway);
			}
			
			if (UnitTracker.count(UnitType.Protoss_Nexus) >= 2
					&& bot.getAvailableMinerals() >= 250
					&& (UnitTracker.count(UnitType.Protoss_Cybernetics_Core) > 0)
					&& UnitTracker.count(UnitType.Protoss_Gateway) < 8)
			{
				bot.spaceManager.build(UnitType.Protoss_Gateway);
			}
			
			if (UnitTracker.count(UnitType.Protoss_Nexus) >= 2
					&& bot.getAvailableMinerals() >= 150
					&& self.completedUnitCount(UnitType.Protoss_Gateway) >= 8
					&& UnitTracker.count(UnitType.Protoss_Gateway) < 12)
			{
				bot.spaceManager.build(UnitType.Protoss_Gateway);
			}
		}
		
		if(bot.getAvailableMinerals() >= 100 
				&& UnitTracker.getGeyserCount() > 0
				&& UnitTracker.count(UnitType.Protoss_Gateway) > 0
				&& (UnitTracker.count(UnitType.Protoss_Nexus) >= 2 || !expandFirst))
		{
			bot.spaceManager.build(UnitType.Protoss_Assimilator);
		}
		
		if (bot.getAvailableMinerals() >= 200 
				&& UnitTracker.count(UnitType.Protoss_Cybernetics_Core) < 1
				&& self.completedUnitCount(UnitType.Protoss_Gateway) > 0
				&& (UnitTracker.count(UnitType.Protoss_Nexus) >= 2 || !expandFirst))
		{
			bot.spaceManager.build(UnitType.Protoss_Cybernetics_Core);
		}
		
		if (bot.getAvailableGas() >= 200 && bot.getAvailableMinerals() >= 200
				&& (UnitTracker.count(UnitType.Protoss_Gateway) >= 8 || detectionNeeded)
				&& UnitTracker.count(UnitType.Protoss_Robotics_Facility) == 0
				&& UnitTracker.count(UnitType.Protoss_Dragoon) >= 5)
		{
			bot.spaceManager.build(UnitType.Protoss_Robotics_Facility);
		}
		
		if (bot.getAvailableGas() >= 50 && bot.getAvailableMinerals() >= 100
				&& self.completedUnitCount(UnitType.Protoss_Robotics_Facility) > 0
				&& UnitTracker.count(UnitType.Protoss_Observatory) == 0)
		{
			bot.spaceManager.build(UnitType.Protoss_Observatory);
		}
		
		if (bot.getAvailableMinerals() >= 150
				&& UnitTracker.count(UnitType.Protoss_Forge) < 2
				&& UnitTracker.count(UnitType.Protoss_Dragoon) >= 2)
		{
			bot.spaceManager.build(UnitType.Protoss_Forge);
		}
		
		if (upgradeLegSpeed 
				&& bot.getAvailableMinerals() >= 150
				&& bot.getAvailableGas() >= 100
				&& UnitTracker.count(UnitType.Protoss_Citadel_of_Adun) == 0
				&& self.completedUnitCount(UnitType.Protoss_Cybernetics_Core) > 0
				&& UnitTracker.count(UnitType.Protoss_Nexus) >= 2)
		{
			bot.spaceManager.build(UnitType.Protoss_Citadel_of_Adun);
		}
		
		/*
		if (bot.getAvailableMinerals() >= 400
				&& bot.getAvailableGas() >= 400
				&& UnitTracker.count(UnitType.Protoss_Templar_Archives) < 1
				&& self.completedUnitCount(UnitType.Protoss_Citadel_of_Adun) > 0
				&& UnitTracker.count(UnitType.Protoss_Nexus) >= 3)
		{
			bot.spaceManager.build(UnitType.Protoss_Templar_Archives);
		}*/
	}
	
	private boolean pauseProduction()
	{
		if (Tyr.bot.getAvailableGas() > 250 && Tyr.bot.getAvailableMinerals() > 400)
			return false;

		if (upgradeLegSpeed && UnitTracker.count(UnitType.Protoss_Citadel_of_Adun) == 0)
			return true;
		if (Tyr.self.completedUnitCount(UnitType.Protoss_Citadel_of_Adun) > 0 
				&& !Tyr.self.isUpgrading(UpgradeType.Leg_Enhancements)
				&& Tyr.self.getUpgradeLevel(UpgradeType.Leg_Enhancements) == 0)
			return true;
		if (detectionNeeded && UnitTracker.count(UnitType.Protoss_Dragoon) >= 5
				&& UnitTracker.count(UnitType.Protoss_Robotics_Facility) == 0)
			return true;
		if (detectionNeeded && UnitTracker.count(UnitType.Protoss_Dragoon) >= 5
				&& Tyr.self.completedUnitCount(UnitType.Protoss_Robotics_Facility) == 1
				&& UnitTracker.count(UnitType.Protoss_Observatory) == 0)
			return true;
		if (detectionNeeded && UnitTracker.count(UnitType.Protoss_Dragoon) >= 5
				&& Tyr.self.completedUnitCount(UnitType.Protoss_Observatory) == 1
				&& UnitTracker.count(UnitType.Protoss_Observer) == 0)
			return true;
		if (takeExpands && attackStarted && UnitTracker.count(UnitType.Protoss_Nexus) < 3)
			return true;
		return false;
	}
	
	@Override
	public boolean overrideStructureOrder(Game game, Player self, Tyr bot, Agent agent)
	{
		if (agent.unit.getType() == UnitType.Protoss_Gateway && !agent.unit.isTraining())
		{
			if (game.enemy().getRace() == Race.Zerg && UnitTracker.count(UnitType.Protoss_Zealot) < 6
					&& bot.getAvailableMinerals() >= 100)
				agent.unit.build(UnitType.Protoss_Zealot);
			boolean pause = pauseProduction();
			if (earlyZealots
					&& self.completedUnitCount(UnitType.Protoss_Cybernetics_Core) == 0
					&& bot.getAvailableMinerals() >= 100)
				agent.unit.build(UnitType.Protoss_Zealot);
			if (UnitTracker.count(UnitType.Protoss_Zealot) < UnitTracker.count(UnitType.Protoss_Dragoon) - 5
					&& bot.getAvailableMinerals() >= 100
					&& (bot.getAvailableMinerals() >= 400 || !pause)
					)
				agent.unit.build(UnitType.Protoss_Zealot);
			else if (bot.getAvailableMinerals() >= 125 && bot.getAvailableGas() >= 50
					&& ((bot.getAvailableMinerals() >= 400 && bot.getAvailableGas() > 200) || !pause))
				agent.unit.build(UnitType.Protoss_Dragoon);
			return true;
		}
		else if (agent.unit.getType() == UnitType.Protoss_Forge && !agent.unit.isUpgrading())
		{
			if(bot.getAvailableMinerals() >= UpgradeType.Protoss_Ground_Weapons.mineralPrice()
					&& bot.getAvailableGas() >= UpgradeType.Protoss_Ground_Weapons.gasPrice())
				agent.unit.upgrade(UpgradeType.Protoss_Ground_Weapons);
			
			if(bot.getAvailableMinerals() >= UpgradeType.Protoss_Ground_Armor.mineralPrice()
					&& bot.getAvailableGas() >= UpgradeType.Protoss_Ground_Armor.gasPrice())
				agent.unit.upgrade(UpgradeType.Protoss_Ground_Armor);
		}
		else if (agent.unit.getType() == UnitType.Protoss_Cybernetics_Core && !agent.unit.isUpgrading())
		{
			if(bot.getAvailableMinerals() >= UpgradeType.Singularity_Charge.mineralPrice()
					&& bot.getAvailableGas() >= UpgradeType.Singularity_Charge.gasPrice())
				agent.unit.upgrade(UpgradeType.Singularity_Charge);
		}
		else if (agent.unit.getType() == UnitType.Protoss_Robotics_Facility)
		{
			if ((UnitTracker.count(UnitType.Protoss_Observer) < 2)
					&& bot.getAvailableMinerals() >= 25
					&& bot.getAvailableGas() >= 75
					&& !agent.unit.isTraining())
				agent.unit.build(UnitType.Protoss_Observer);
			return true;
		}
		else if (agent.unit.getType() == UnitType.Protoss_Citadel_of_Adun && !agent.unit.isUpgrading())
		{
			if (bot.getAvailableGas() >= UpgradeType.Leg_Enhancements.gasPrice()
					&& bot.getAvailableMinerals() >= UpgradeType.Leg_Enhancements.mineralPrice())
				agent.unit.upgrade(UpgradeType.Leg_Enhancements);
		}
		return false;
	}
}
