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

[RequireComponent(typeof(Animator))]

public class LittleController : MonoBehaviour {

	public float moveSpeed = 5f;
	public float jumpForce = 3f;
	public float lightRotationSpeed = 2f;

	public GameObject darkLightQuad;
	private Material DLQuadReference;
	public Texture darkLight;
	public Texture flippedDarkLight;

	public GameObject darkLightPivot;
	private Transform pivotObj;

	public Transform lightCollider;

	private float h;
	private float v;
	private bool facingRight = true;
	private Transform groundCheck;
	//private Transform ceilingCheck;
	private float groundedRadius = .1f;
	//private float ceilingRadius = .1f;
	public bool grounded = false;
	public LayerMask whatIsGround;

	private float armRotation;

	private Animator anim;

	private bool jump = false;

	private bool playerCanControl = true;
	public float deathForce = 1000f;
	public float twerq = 5f;

	private bool restartTimer = false;
	private float restartCountdown = 1.5f;
	

	
	void Start () {

		// Setting up references.
		groundCheck = transform.Find("GroundCheck");
		//ceilingCheck = transform.Find("CeilingCheck");

		anim = this.gameObject.GetComponent<Animator>();

		DLQuadReference = darkLightQuad.GetComponent<MeshRenderer>().material;



	}

	void Update () {

		pivotObj = GameObject.FindGameObjectWithTag("light pivot").transform;

		if(restartTimer){

			restartCountdown -= Time.deltaTime;

			if(restartCountdown <= 0){
				Application.LoadLevel(Application.loadedLevel);
			}

		}
	
		if(!playerCanControl){
			return;

		}

		if(Input.GetKeyDown(KeyCode.R)){
			Application.LoadLevel(Application.loadedLevel);
		}


		v = Input.GetAxis("Vertical");

		armRotation += v*lightRotationSpeed;

		if(Input.GetKeyDown(KeyCode.Space) && grounded ){
			jump = true;
		}

		//Normalize my rotation to stay between 80 and 270 degrees
		if(armRotation > 80f){
			armRotation = 79.9f;
		}else if(armRotation < -80f){
			armRotation = -79.9f;
		}

		//Actually rotate arm
		if(facingRight){
			pivotObj.rotation = Quaternion.Euler(0, 0, armRotation);
		}else{
			pivotObj.rotation = Quaternion.Euler(0, 0, -armRotation);
		}


		// The player is grounded if a circlecast to the groundcheck position hits anything designated as ground
		grounded = Physics2D.OverlapCircle(groundCheck.position, groundedRadius, whatIsGround);



	}

	void FixedUpdate(){

		if(playerCanControl){
			h = Input.GetAxis("Horizontal");
		}


		// The player is grounded if a circlecast to the groundcheck position hits anything designated as ground
		grounded = Physics2D.OverlapCircle(groundCheck.position, groundedRadius, whatIsGround);


		//Sets the direction that the player needs to face based on the movement
		if(h > 0 && !facingRight){
			FlipSprite();
		}else if(h < 0 && facingRight){
			FlipSprite();
		}

		rigidbody2D.velocity = new Vector2(h * moveSpeed, rigidbody2D.velocity.y);


		// If the player should jump...
		if (grounded && jump && playerCanControl) {
			// Add a vertical force to the player.
			rigidbody2D.AddForce(new Vector2(0f, jumpForce));
			jump = false;
		}



		//Let the animator know if I am grounded or not and give my current horizontal movespeed
		anim.SetBool("grounded", grounded);
		anim.SetFloat("moveSpeed", Mathf.Abs(h));

	}

	private void FlipSprite(){


		facingRight = !facingRight;


		Follow temp = darkLightPivot.GetComponent<Follow>();
		//Transform pivotObj = darkLightPivot.transform.FindChild("DO NOT DELETE");
		Vector3 tempScale = pivotObj.localScale;
		Vector3 tempRot = pivotObj.rotation.eulerAngles;

		Vector3 templightCollider = lightCollider.transform.localScale;

		//Change my mask to face the proper direction without distortion
		if(facingRight){
			DLQuadReference.SetTexture("_Mask", darkLight);

			temp.xOffset = -2;
			tempScale.x = 1;

			templightCollider.x = 1;

		}else{
			DLQuadReference.SetTexture("_Mask", flippedDarkLight);

			temp.xOffset = 2;
			tempScale.x = -1;

			templightCollider.x = -1;
		}

		//Apply the scale changes when flipping
		pivotObj.localScale = tempScale;

		//Apply lightCollider changes
		lightCollider.localScale = templightCollider;

		//Change my rotation.z to keep the look consistent
		tempRot.z *= -1;
		pivotObj.rotation = Quaternion.Euler(tempRot);

		// Multiply the player's x local scale by -1.
		Vector3 theScale = transform.localScale;
		theScale.x *= -1;

		transform.localScale = theScale;

	}

	void OnTriggerEnter2D(Collider2D col){

		//Check if player is hit by projectle and start his death
		if(col.gameObject.layer == LayerMask.NameToLayer("Enemy Projectile")){

			//KILL PLAYER HERE

			killPlayer();;


		}

	}


	public void killPlayer(){

		playerCanControl = false;
		
		rigidbody2D.fixedAngle = false;
		rigidbody2D.AddTorque(twerq);
		
		rigidbody2D.AddForce(Vector2.up*deathForce);
		
		restartTimer = true;

	}




















}
