package player;

import animations.PlayerAnimationUtils;
import animations.PlayerAnimations;
import engine.Game;
import hud.inventory.InvenHelper;
import interfaces.ItemIDs;
import interfaces.PlayerAnimationIDs;
import utils.Utils;

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

public final class PlayerTick implements PlayerAnimationIDs, ItemIDs {
	
	public static final void tick() {
		//*********** DEBUG STUFF **************//
		if(Game.godMode) Player.health = 100;
		//*************************************//
		
		// CALC COLLISION
		if (!Game.noClip) Player.collided = PlayerCollision.collision();

		// ------ CLAMP PLAYER HEALTH -----------------------------------------------//
		Player.health = Utils.clamp(Player.health, 0, Player.maximumHealth);
		// ----- RUN DEATH IF DEAD -------------------------------------------------//
		if(Player.health <= 0) { if(!PlayerDeath.RUNNING) PlayerDeath.START(); PlayerDeath.tick(); return; }
		
		// ---- POSITION UPDATING ---------------------------//
		if (!Player.collided) MouseMovement.runMouseMovement();
		else Player.keepWalkingX = Player.keepWalkingY = false;
		PlayerPosition.update();
		// ----- END POSITION UPDATING ---------------------//
		
		// ----------- ATTACKING --------------------- //
		if(Player.attacking) { PlayerAttackSequence.tick(); return; }
		if(PlayerAttackSequence.ATTACK_COOLDOWN_TIMER > 0) PlayerAttackSequence.ATTACK_COOLDOWN_TIMER--;
		// ---------- END ATTACKING -------------------//
		
		// --- CALC WHICH ANIMATION TO USE, THEN RUN THEM --- //
		if(Player.animate) {
			PlayerAnimationUtils.calc();
			runAnimation();
			runEquipmentAnimations();
		}
		// ------- END ANIMATION CALC / RUN ---------------- //
		
		// ----------- reset animations --------------//
		if(Player.velX == 0 && Player.velY == 0) PlayerAnimationUtils.resetWalking(); //TODO fix this, it will grow way too resource heavy
		if (!(Player.treeCollided && Player.using && InvenHelper.isAxe(Player.equipmentSlots[HAND_SLOT].itemId))) PlayerAnimationUtils.resetAxeChops();
		if (!(Player.boulderCollided && Player.using && InvenHelper.isPickaxe(Player.equipmentSlots[HAND_SLOT].itemId))) PlayerAnimationUtils.resetPickaxeMines();
		// ---------- end animation resetting ---------//
	}

	private static final void runAnimation() {
		switch (Player.currentAnimation) {
			case IDLE:			 PlayerAnimations.base.runIdle(); break;
			case WALK: 			 PlayerAnimations.base.runWalk(); break;
			case AXE_CHOP: 		 PlayerAnimations.base.runChop(); break;
			case MINE_ROCK:		 PlayerAnimations.base.runMine(); break;
		}
	}
	
	private static final void runEquipmentAnimations() {
			switch (Player.currentAnimation) {
				case IDLE: { switch(Player.equipmentSlots[HAND_SLOT].itemId){
						case CRUDE_AXE: 	PlayerAnimations.crudeAxe.runIdle(); break;
						case CRUDE_PICKAXE: PlayerAnimations.crudePickaxe.runIdle(); break;
						case TORCH: 		PlayerAnimations.torch.runIdle(); break;
						case GOBLIN_SPEAR: 	PlayerAnimations.goblinSpear.runIdle(); break;
					}} break;
				
				case WALK: { switch(Player.equipmentSlots[HAND_SLOT].itemId){
						case CRUDE_AXE: 	PlayerAnimations.crudeAxe.runWalk(); break;
						case CRUDE_PICKAXE: PlayerAnimations.crudePickaxe.runWalk(); break;
						case TORCH: 		PlayerAnimations.torch.runWalk(); break;
						case GOBLIN_SPEAR:  PlayerAnimations.goblinSpear.runWalk(); break;
					}} break;
				
				case AXE_CHOP: { switch(Player.equipmentSlots[HAND_SLOT].itemId){
						case CRUDE_AXE: 	PlayerAnimations.crudeAxe.runChop(); break;
				}} break;
				
				case MINE_ROCK: { switch(Player.equipmentSlots[HAND_SLOT].itemId){
						case CRUDE_PICKAXE: PlayerAnimations.crudePickaxe.runMine(); break;
				}} break;
			}
	}
}
