package animations;

import engine.Game;
import engine.Handler;
import hud.inventory.InvenHelper;
import interfaces.Directions;
import interfaces.ItemIDs;
import interfaces.PlayerAnimationIDs;
import player.Player;
import utils.Animation;
import utils.Point2f;
import utils.Utils;
import world.Boulder;
import world.Tree;

/**  ------------- Nihilne ------------
    *** @author Mitch Weaver - 2016 ***
      ~~~~ All Rights Reserved ~~~~ */

public final class PlayerAnimationUtils implements Directions, PlayerAnimationIDs, ItemIDs {

	private static int TEMP_ANIMATION;
	
	public final static void calc() {
		
		// --------- GET HIT ------------------ //
		if(Player.getHitTimer > 0) {
			Player.getHitTimer--;
			TEMP_ANIMATION = Player.currentAnimation;
			Player.currentAnimation = GET_HIT;
			return;
		} else if(Player.getHitTimer == 0) {
			Player.getHitTimer--;
			Player.currentAnimation = TEMP_ANIMATION;
			return;
		}
		// -------- END GET HIT --------------- //
		
		if (Player.treeCollided && Player.using && InvenHelper.isAxe(Player.equipmentSlots[HAND_SLOT].itemId)) {
			Player.currentAnimation = AXE_CHOP;
			
			for(final Tree i : Handler.trees) /* find which tree is being chopped and orient player towards it */
				if(i.playerCollided){
					Player.facing = determineFacing(Player.center, i.center, Player.radius);
					break;
				}
		}
		
		else if (Player.boulderCollided && Player.using && InvenHelper.isPickaxe(Player.equipmentSlots[HAND_SLOT].itemId)) {
			Player.currentAnimation = MINE_ROCK;
			
			for(final Boulder i : Handler.boulders) /* find which tree is being chopped and orient player towards it */
				if(i.playerCollided){
					Player.facing = determineFacing(Player.center, i.center, Player.radius);
					break;
				}
		}

		else if (Player.velX != 0 || Player.velY != 0) Player.currentAnimation = WALK;
		else if (Player.velX == 0 && Player.velY == 0) Player.currentAnimation = IDLE;
	}
	
	
	// NO IDEA IF THIS NEEDS TO BE SYNCHRONIZED, I HAD IT BEFORE, DONT KNOW WHY
	// TOOK IT OFF, NOT NOTICING ANYTHING WRONG YET. MAY STILL NEED IT. BEWARE.
	public final static void resetAll(){
//		Player.animate = false;  // NO FKN IDEA WHY I HAD THIS EITHER, MAY NEED IT IDK
		resetIdles(); resetWalking(); resetAxeChops(); resetPickaxeMines();
//		Player.animate = true;
	}
	
	public final static void resetIdles(){
		for(final Animation i : PlayerAnimations.base.idles) 			i.restartAnimation();
		for(final Animation i : PlayerAnimations.crudeAxe.idles) 		i.restartAnimation();
		for(final Animation i : PlayerAnimations.crudePickaxe.idles) 	i.restartAnimation();
		for(final Animation i : PlayerAnimations.torch.idles) 			i.restartAnimation();
		for(final Animation i : PlayerAnimations.goblinSpear.idles)     i.restartAnimation();
	}
	
	public final static void resetWalking(){
		for(final Animation i : PlayerAnimations.base.walks) 			i.restartAnimation();
		for(final Animation i : PlayerAnimations.crudeAxe.walks) 		i.restartAnimation();
		for(final Animation i : PlayerAnimations.crudePickaxe.walks) 	i.restartAnimation();
		for(final Animation i : PlayerAnimations.torch.walks) 			i.restartAnimation();
		for(final Animation i : PlayerAnimations.goblinSpear.walks)     i.restartAnimation();
	}
	
	public final static void resetAxeChops(){
		for(final Animation i : PlayerAnimations.base.axeChops) 		i.restartAnimation();
		for(final Animation i : PlayerAnimations.crudeAxe.axeChops) 	i.restartAnimation();
	}
	
	public final static void resetPickaxeMines(){
		for(final Animation i : PlayerAnimations.base.mines)            i.restartAnimation();
		for(final Animation i : PlayerAnimations.crudePickaxe.mines)    i.restartAnimation();
	}
	
	public final static int determineFacing(final Point2f point1, final Point2f point2, final float p1Radius){
		final Point2f p1 = Utils.rotatePoint(new Point2f(point1.x, point1.y), Utils._45*(Game.rotation - 1));
		final Point2f p2 = Utils.rotatePoint(new Point2f(point2.x, point2.y), Utils._45*(Game.rotation - 1));
		
		if(p2.x - p1Radius > p1.x && p2.y - p1Radius > p1.y) return SOUTHEAST;
    	else if(p2.x - p1Radius > p1.x && p2.y + p1Radius < p1.y) return NORTHEAST;
		else if(p2.x - p1Radius > p1.x) return EAST;
		
		else if(p2.x + p1Radius < p1.x && p2.y + p1Radius < p1.y) return NORTHWEST;
		else if(p2.x + p1Radius < p1.x && p2.y - p1Radius > p1.y) return SOUTHWEST;
		else if(p2.x + p1Radius < p1.x) return WEST;	
		
		else if(p2.y - p1Radius > p1.y) return SOUTH;
		else if(p2.y + p1Radius < p1.y) return NORTH;
		else return NORTH;
	}
	
}
