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

public class MainCharacter : MonoBehaviour
{
		public float runSpeed;
		
		public static MainCharacter inst;
		
		float lineOfDeath = 15;
		
		public float jumpStrength;
		public Transform groundCheck;
		public LayerMask whatIsGround;
		public int nJumps = 1;
		
		float maxFlyUpSpeed = 0.8f;
		bool grounded;
		Vector2 jumpVector;
		
		Vector3 startPos;
		
		bool running;
				
		public string[] shapeNames;	
		Dictionary<string, Shape>  shapes;
		
		string currentShapeName = "";
		Shape currentShape;
		
		Animator characterAnimator;
		
		bool shapeShiftedOnTouch = false;
		
		int hits = 0;
		
		public delegate void GetHitHandler (float amount);
		public static event GetHitHandler onCharacterGetHit;
		
		void Awake ()
		{
				inst = this;
		}
		
		void OnEnable ()
		{
			
				EasyTouch.On_TouchStart += HandleOn_TouchStart;
				EasyTouch.On_TouchUp += HandleOn_TouchUp;
				EasyTouch.On_Swipe += HandleOn_Swipe;
				EasyTouch.On_TouchDown += HandleOn_TouchDown;
		}

		void HandleOn_TouchUp (Gesture gesture)
		{
				shapeShiftedOnTouch = false;
		}

		void HandleOn_TouchDown (Gesture gesture)
		{
				if (currentShape == null)
						return;
						
				if (currentShape.canFly) {
						rigidbody2D.AddForce (new Vector2 (0, jumpStrength));
						if (rigidbody2D.velocity.y > maxFlyUpSpeed) {
								rigidbody2D.velocity = (new Vector2 (rigidbody2D.velocity.x, maxFlyUpSpeed));
						}
				}
		}
		
		
	
		void OnDisable ()
		{
				EasyTouch.On_Swipe -= HandleOn_Swipe;
				EasyTouch.On_TouchStart -= HandleOn_TouchStart;
				EasyTouch.On_TouchDown -= HandleOn_TouchDown;
		
		}

		
	
	
		void Start ()
		{
				
				shapes = new Dictionary<string, Shape> ();
				foreach (string shapeName in shapeNames) {
						Shape shape = Instantiate (Resources.Load ("Shapes/" + shapeName, typeof(Shape)))as Shape;
						shape.transform.parent = this.transform;
						shape.transform.localPosition = Vector3.zero;
						shape.transform.localScale = Vector3.one;
						shape.gameObject.SetActive (false);
						shapes.Add (shapeName, shape);
				}
				OnShapeShift ("Dude");
				//this.rigidbody2D.AddForce (new Vector2 (Global.inst.movementSpeed, 0));
				startPos = this.transform.localPosition;
				Restart ();
		}
		
		void OnShapeShift (string shapeName)
		{
				if (currentShapeName != shapeName) {
						if (shapes.ContainsKey (currentShapeName)) {
								shapes [currentShapeName].gameObject.SetActive (false);	
						}
						currentShapeName = shapeName;
						shapes [currentShapeName].gameObject.SetActive (true);
						characterAnimator = GetComponentInChildren<Animator> ();
						currentShape = shapes [currentShapeName];
						runSpeed = currentShape.speed != 0 ? currentShape.speed : runSpeed;
						jumpStrength = currentShape.jumpHeight != 0 ? currentShape.jumpHeight : jumpStrength;
						jumpVector = new Vector2 (0, jumpStrength);
						shapeShiftedOnTouch = true;
				}
		}
		
		
		
		public void Restart ()
		{
				hits = 0;
				SectionManager.inst.Restart ();
				SectionManager2.inst.Restart ();
				CameraMovement.inst.Restart ();
				this.transform.localPosition = startPos;
				rigidbody2D.velocity = Vector2.zero;
				running = false;
				Invoke ("StartRunning", 1);
		}
		
		void StartRunning ()
		{
				running = true;
				rigidbody2D.velocity = new Vector2 (runSpeed, rigidbody2D.velocity.y);
		}
	
		void HandleOn_TouchStart (Gesture gesture)
		{
				if (grounded && currentShape.canJump) {
						this.rigidbody2D.AddForce (jumpVector);
						characterAnimator.SetTrigger ("Jump");
				}
		}
		
		public void GetHit ()
		{		
				if (currentShape.canGetHitByEnemies) {
						if (onCharacterGetHit != null)
								onCharacterGetHit (0);
					
						hits++;
						if (hits == 5) {
								Restart ();
						}
				}
		  
		}
		
		void Update ()
		{
				// The player is grounded if a linecast to the groundcheck position hits anything on the ground layer.
				grounded = Physics2D.Linecast (transform.position, groundCheck.position, whatIsGround);	
				
				if (this.transform.localPosition.y < -lineOfDeath) {
						Restart ();
				}
		}
		
		void FixedUpdate ()
		{		
				if (running) {
						if (rigidbody2D.velocity.x >= 0 && rigidbody2D.velocity.x <= 0.05f) {
								
								rigidbody2D.AddForce (new Vector2 (-250, 0));
								if (currentShapeName != "Dude") {
										characterAnimator.SetTrigger ("Collision");
								}
														
						} else {
								/*
								if (rigidbody2D.velocity.x < runSpeed) {
										rigidbody2D.AddForce (new Vector2 (250, 0));
								}
						
								if (rigidbody2D.velocity.x > runSpeed) {
										rigidbody2D.velocity = new Vector2 (runSpeed, rigidbody2D.velocity.y);
								}
								*/
								
								if (grounded || currentShapeName == "Eagle") {
										rigidbody2D.velocity = new Vector2 (runSpeed, rigidbody2D.velocity.y);
								} else if (rigidbody2D.velocity.x <= runSpeed) {
										rigidbody2D.AddForce (new Vector2 (50, 0));
										
										if (rigidbody2D.velocity.x > runSpeed)
												rigidbody2D.velocity = new Vector2 (runSpeed, rigidbody2D.velocity.y);
								}
								
						}
				}
				
		}
		
		void HandleOn_Swipe (Gesture gesture)
		{
				
				switch (gesture.swipe) {
				
				case EasyTouch.SwipeType.Left:
						OnShapeShift ("Turtle");
						break;
				case EasyTouch.SwipeType.Right:
						OnShapeShift ("Lion");
						break;
				case EasyTouch.SwipeType.Up:
						OnShapeShift ("Eagle");
						break;
				case EasyTouch.SwipeType.Down:
						OnShapeShift ("Dude");
						break;
				default:
						break;
							
				}
		}
		
		
		
		
	
}
