using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class Movements : MonoBehaviour {

	public enum e_pawn_type {MONSTER, MINION};
	public enum e_movement {UP, RIGHT, DOWN, LEFT, NONE};
	
	[SerializeField] public MinionSprites					minionSpritesManager;
	[SerializeField] public MonsterAnimations		monsterSpritesManager;
	public e_pawn_type											pawnType;
	public float 														speed = 1.0f;
	public float 														toleranceArrival = 0.01f;
	
	List<GameObject>												listGameObjects = new List<GameObject>();
	
	[HideInInspector]
	public Vector3 													posDestination;
	[HideInInspector]
	public GameObject											checkpoint = null;

	Detectors															detectors;
	e_movement														dirMovement;
	bool																		isMoving = false;
	bool																		isleavingCheckpoint = false;
	bool																		isCheckpointLocked = false;
	bool																		isOnCheckpoint = false;
	
	void Awake(){
		detectors = GetComponent<Detectors>();
	}
	
	void Start(){
		if (pawnType == e_pawn_type.MONSTER)
			GameObject.Find("Main Camera").GetComponent<SoundDispenser>().PlaySound(SoundDispenser.e_music.MONSTER_SPAWN);
	}
	
	void Update(){
		LerpMovements();
		QuitCheckpoint();
	}
	
	public void Action(){
		if (pawnType == e_pawn_type.MONSTER){
			monsterSpritesManager.Attack();
			GameObject.Find("Main Camera").GetComponent<SoundDispenser>().PlaySound(SoundDispenser.e_music.MONSTER_EATING);
			GameObject target = detectors.currentSlotObject.GetComponent<FloorSlot>().whoIsHere;
			Checkpoint checkpointScript = detectors.currentSlotObject.GetComponent<Checkpoint>();
			if (checkpointScript == null && target != null && target.GetComponent<Movements>().pawnType == Movements.e_pawn_type.MINION){
				GameObject.Find("Main Camera").GetComponent<SoundDispenser>().PlaySound(SoundDispenser.e_music.DYING);
				GameObject.Find("MinionManager").GetComponent<MinionManager>().RemoveMinion(target);
				Destroy(target);
			}
			else if (checkpointScript != null ){
				checkpointScript.KillAllMinions();
			}
		}
	}
	
	public void NewInputAction(e_movement newMovement){
		if (isOnCheckpoint == true){
			checkpoint.GetComponent<Checkpoint>().RemoveMinion(transform.gameObject);
			isleavingCheckpoint = true;
			isCheckpointLocked = true;
			return ;
		}
		isCheckpointLocked = false;
		if (newMovement == e_movement.UP && detectors.canWalkUp){
			if ((pawnType == e_pawn_type.MINION && !isMoving) || pawnType == e_pawn_type.MONSTER)
				MoveNow(e_movement.UP);
		}
		if (newMovement == e_movement.DOWN  && detectors.canWalkDown){
			if ((pawnType == e_pawn_type.MINION && !isMoving) || pawnType == e_pawn_type.MONSTER)
				MoveNow(e_movement.DOWN);
		}
		if (newMovement == e_movement.RIGHT  && detectors.canWalkRight){
			if ((pawnType == e_pawn_type.MINION && !isMoving) || pawnType == e_pawn_type.MONSTER)
				MoveNow(e_movement.RIGHT);
		}
		if (newMovement == e_movement.LEFT  && detectors.canWalkLeft){
			if ((pawnType == e_pawn_type.MINION && !isMoving) || pawnType == e_pawn_type.MONSTER)
				MoveNow(e_movement.LEFT);
		}
	}
	
	void MoveNow(e_movement newDir){
		dirMovement = newDir;
		if (dirMovement == e_movement.NONE)
			return ;
		isMoving = true;
		switch (dirMovement){
			case e_movement.UP :
				posDestination = detectors.slotObjectUp.transform.position;
				if (pawnType == e_pawn_type.MONSTER)
					monsterSpritesManager.AnimUp();
				else
					minionSpritesManager.AnimUp();
				break;
			case e_movement.RIGHT :
				posDestination = detectors.slotObjectRight.transform.position;
				if (pawnType == e_pawn_type.MONSTER)
					monsterSpritesManager.AnimRight();
				else
					minionSpritesManager.AnimRight();
				break;
			case e_movement.LEFT :
				posDestination = detectors.slotObjectLeft.transform.position;
				if (pawnType == e_pawn_type.MONSTER)
					monsterSpritesManager.AnimLeft();
				else
					minionSpritesManager.AnimLeft();
				break;
			case e_movement.DOWN :
				posDestination = detectors.slotObjectDown.transform.position;
				if (pawnType == e_pawn_type.MONSTER)
					monsterSpritesManager.AnimDown();
				else
					minionSpritesManager.AnimDown();
				break;
		}
		posDestination.z = -1f;
	}
	
	void LerpMovements(){
		if (isMoving){
			transform.position = Vector3.Lerp(transform.position, posDestination, (Time.deltaTime * speed) / (Vector3.Distance(transform.position, posDestination) / 1));
			if (Vector3.Distance(transform.position, posDestination) <= toleranceArrival && ((pawnType == e_pawn_type.MINION && !canContinueToWalk()) || pawnType == e_pawn_type.MONSTER)){
					isMoving = false;
					if (pawnType == e_pawn_type.MONSTER)
						monsterSpritesManager.AnimStop();
					else{
						minionSpritesManager.AnimStop();
						//juj
						if (detectors.currentSlot == "Checkpoint" && !isCheckpointLocked){
							isOnCheckpoint = true;
							checkpoint = detectors.currentSlotObject;
							detectors.currentSlotObject.GetComponent<Checkpoint>().AddMinion(transform.gameObject);
						}
					}
			}
		}
	}
	
	void QuitCheckpoint(){
		if (isleavingCheckpoint){
			Vector3 tmpPos = checkpoint.transform.position;
			tmpPos.z = transform.position.z;
			transform.position = Vector3.Lerp(transform.position, tmpPos, (Time.deltaTime * speed) / (Vector3.Distance(transform.position, tmpPos) / 1));
			if (Vector3.Distance(transform.position, tmpPos) <= toleranceArrival){
				checkpoint = null;
				isOnCheckpoint = false;
				isleavingCheckpoint = false;
			}
		}
	}
	
	bool canContinueToWalk(){
		switch (dirMovement){
			case e_movement.UP :
				if (detectors.canWalkUp && !detectors.canWalkRight && !detectors.canWalkLeft){
					MoveNow(e_movement.UP);
					return true;
				}
				if (!detectors.canWalkUp && detectors.canWalkRight && !detectors.canWalkLeft){
					MoveNow(e_movement.RIGHT);
					return true;
				}
				if (!detectors.canWalkUp && !detectors.canWalkRight && detectors.canWalkLeft){
					MoveNow(e_movement.LEFT);
					return true;
				}
				break;
			case e_movement.RIGHT :
				if (detectors.canWalkUp && !detectors.canWalkRight && !detectors.canWalkDown){
					MoveNow(e_movement.UP);
					return true;
				}
				if (!detectors.canWalkUp && detectors.canWalkRight && !detectors.canWalkDown){
					MoveNow(e_movement.RIGHT);
					return true;
				}
				if (!detectors.canWalkUp && !detectors.canWalkRight && detectors.canWalkDown){
					MoveNow(e_movement.DOWN);
					return true;
				}
				break;
			case e_movement.DOWN :
				if (detectors.canWalkLeft && !detectors.canWalkRight && !detectors.canWalkDown){
					MoveNow(e_movement.LEFT);
					return true;
				}
				if (!detectors.canWalkLeft && detectors.canWalkRight && !detectors.canWalkDown){
					MoveNow(e_movement.RIGHT);
					return true;
				}
				if (!detectors.canWalkLeft && !detectors.canWalkRight && detectors.canWalkDown){
					MoveNow(e_movement.DOWN);
					return true;
				}
				break;
			case e_movement.LEFT :
				if (detectors.canWalkLeft && !detectors.canWalkUp && !detectors.canWalkDown){
					MoveNow(e_movement.LEFT);
					return true;
				}
				if (!detectors.canWalkLeft && detectors.canWalkUp && !detectors.canWalkDown){
					MoveNow(e_movement.UP);
					return true;
				}
				if (!detectors.canWalkLeft && !detectors.canWalkUp && detectors.canWalkDown){
					MoveNow(e_movement.DOWN);
					return true;
				}
				break;
		}
		return false;
	}
}
