using UnityEngine;
using System.Collections;

public class PlayerController : MonoBehaviour {
	
	public float maxPanic = 1.0f;
	public float panicUpRate = 0.1f;
	public float panicRecoverRate = 0.05f;
	public float panicTotal = 0.0f;
	public float panicBackupTime = 0.3f;
	public float fowardSpeed = 1.0f;
	public float backwardsSpeed = 0.5f;
	public float panicSpeed = 1.5f;
	public bool panic = false;
	public bool startIdle = true;

	public GameObject GameControllerObject;
	public GameObject LaserTopHalf;
	public GameObject LaserBottomHalf;
	private bool dead = false;
	
	
	public enum WalkingStates {idle, walk_forward, pause, walk_backwards, panic, surrender, surrender_moist, surrender_laser}; 
	public WalkingStates state = WalkingStates.idle;
	
	private Animator animator;
	
	//GameController gc;
	// Use this for initialization
	void Start () {
		animator = gameObject.GetComponent<Animator>();
		animator.SetInteger("State", 0);
		state = WalkingStates.idle;
		panic = false;
	}
	
	// Update is called once per frame
	void Update () {
		if(panic == true && dead == false)
			state = WalkingStates.panic;
			
		switch(state)
		{
		
		case WalkingStates.idle:
			SetHorizontalVelocity(0);
			animator.SetInteger("State", 0);
			if(AnyInput() || startIdle == false)
				state = WalkingStates.walk_forward;
		break;
		
		case WalkingStates.walk_forward:
			SetHorizontalVelocity(fowardSpeed);
			animator.SetInteger("State", 1);
			if(CheckPause())
				state = WalkingStates.pause;
			else
				state = WalkingStates.walk_forward;
		break;
		
		case WalkingStates.pause:
			SetHorizontalVelocity(0);
			animator.SetInteger("State", 2);
			panicTotal += panicUpRate * Time.deltaTime;
			if(panicTotal > panicBackupTime)
				state = WalkingStates.walk_backwards;
			if(CheckPause() == false)
				state = WalkingStates.walk_forward;
			
		break;
		
		case WalkingStates.walk_backwards:
			animator.SetInteger("State", 3);
			SetHorizontalVelocity(backwardsSpeed);
			panicTotal += panicUpRate * Time.deltaTime;
			if(panicTotal >= maxPanic)
			{
				panicTotal = maxPanic;
				state = WalkingStates.panic;
			}
			if(CheckPause() == false)
				state = WalkingStates.walk_forward;
		break;
		
		case WalkingStates.panic:
			animator.SetInteger("State", 4);
			panic = true;
			SetHorizontalVelocity(panicSpeed);
			if(panicTotal <= 0)
			{
				panic = false;
				state = WalkingStates.walk_forward;
			}
			
		break;
		case WalkingStates.surrender:
			animator.SetInteger("State", 5);
			SetHorizontalVelocity(0);
		break;
		case WalkingStates.surrender_moist:
			animator.SetInteger("State", 6);
			SetHorizontalVelocity(0);
			break;
		case WalkingStates.surrender_laser:
			animator.SetInteger("State", 7);
			SetHorizontalVelocity(0);
			GameObject g1 = (GameObject)Instantiate(LaserTopHalf, gameObject.transform.position + Vector3.up * 5 + Vector3.right, gameObject.transform.rotation);
			GameObject g2 = (GameObject)Instantiate(LaserBottomHalf, gameObject.transform.position + Vector3.up * 3, gameObject.transform.rotation);
			gameObject.SetActive(false);
			break;
		default:
		break;
		}
		
		panicTotal -= panicRecoverRate * Time.deltaTime;
		if(panicTotal < 0)
			panicTotal = 0;
	}
	
	bool AnyInput()
	{
		if(Mathf.Abs(Input.GetAxis("Horizontal")) > 0.1f || Mathf.Abs(Input.GetAxis("Vertical")) > 0.1f || Input.GetButton("Fire1"))
			return true;
		return false;
	}

	bool CheckPause ()
	{
		return Input.GetButton("Fire1") || Input.GetButton("Jump");
	}

	void SetHorizontalVelocity (float speed)
	{
		Vector2 v = rigidbody2D.velocity;
		v.x = speed;
		rigidbody2D.velocity = v;
	}	
	
	void OnTriggerEnter2D(Collider2D other)
	{
		if(other.tag.Contains("Obstacle"))
		{
			dead = true;
			if(other.tag.Contains("Laser"))
				state = WalkingStates.surrender_laser;
			else if(Random.Range(0,1.0f) < 0.5f)
				state = WalkingStates.surrender;
			else
				state = WalkingStates.surrender_moist;
		}
	}

	public void Surrender()
	{
		if (dead == true)
			return;
		dead = true;
		if(Random.Range(0,1.0f) < 0.5f)
			state = WalkingStates.surrender;
		else
			state = WalkingStates.surrender_moist;

	}
}
