using UnityEngine;
using System.Collections;

public class PlayerSummonController : PlayerController {

	public PlayerMainController player;
	public float baseJump, incJump;
	public float baseMove, incMove;
	public GameObject deathAnimation;

	public float extraJumpMult;
	public float dashDuration, dashSpeed; private float dashTimer;

	private int[] BaseStats;	

	public struct SpCommand{ 
		public int sequenceLength;
		public int[] commSequence;
		public bool activated;

		public SpCommand(int a, int[] b){
			sequenceLength = a;
			commSequence = b;
			activated = true; //replace false later
		}
	};

	private int spCommandAmt;			//no of special commands
	public SpCommand[] spCommandList;	//data of special commands
	public int[] spCommandCharges; 		//how many times has this spcommand been invoked during summon

	private bool jumptried;
	private int extraJumpCnt, dashCnt;

	// Use this for initialization
	void Start () {
		jumptried = false;
		BaseStats = new int[5];

		spCommandAmt = 2;
		spCommandList = new SpCommand[spCommandAmt];
		spCommandCharges = new int[spCommandAmt];

		spCommandList[0] = new SpCommand(2, new int[]{1,2} ); //extra jump: Air, Earth
		spCommandList[1] = new SpCommand(2, new int[]{2,3} ); //dash: Earth, Fire,  

		this.gameObject.SetActive(false);
	}

	// Update is called once per frame
	void Update () {
		if (Input.GetKeyDown(KeyCode.Space) || Input.GetKeyDown(KeyCode.UpArrow)){
			jumptried = true;
		}  
		if (Input.GetKeyDown(KeyCode.Tab)){
			Kill ();
		}  
		if (Input.GetKeyDown(KeyCode.D)){
			Dash ();
		}  
		GenieAnimScript.bOnGround = CheckGrounded();
		GenieAnimScript.velocity = rigidBody.velocity;
		
		if (GenieAnimScript.bOnGround){
			extraJumpCnt = 0;
			dashCnt = 0;
		}
	} 
	
	// Update is called once per frame
	void FixedUpdate () {    
		if (Time.time <= dashTimer){
			rigidBody.velocity = new Vector2(dashSpeed*facedir, 0);
			return;
		}

		rigidBody.velocity = (new Vector2(rigidBody.velocity.x,rigidBody.velocity.y - gravity));
		
		if (Input.GetKey(KeyCode.RightArrow)){
			SetHorizontalVelocity(moveSpeed);
			//rigidBody.AddForce(new Vector2(movespeed,rigidBody.velocity.y));
		} 
		
		if (Input.GetKey(KeyCode.LeftArrow)){
			SetHorizontalVelocity(-moveSpeed);
			//rigidBody.AddForce(new Vector2(-movespeed,rigidBody.velocity.y));
		} 
		
		if(jumptried){
			jumptried = false;
			Jump(jumpSpeed);
		}

		if (rigidBody.velocity.x>0) facedir = 1;
		if (rigidBody.velocity.x<0) facedir = -1;
	}  

	public void SetJump(int inp){
		BaseStats[1]=inp;
		jumpSpeed = baseJump + inp*incJump;
	}

	public void SetLift(int inp){
		BaseStats[2]=inp; 
	}
	
	public void SetMove(int inp){ 
		BaseStats[3]=inp;
		moveSpeed = baseMove + inp*incMove;
	}

	public void GainFocus(){
		CameraController.SetFollowTarget(this);
		DestroyByFall.SetFollowTarget(this);
		CameraController.SetOffset(new Vector2(0,0.1f));
		CameraController.SetZoom(1.5f);
		WizzardAnimScript.bCasting = true; 
	}
	
	public override void Jump(float strength){
		if (CheckGrounded()){
			SetVerticalVelocity(strength);  
		} else {
			if (extraJumpCnt < spCommandCharges[0]){
				SetVerticalVelocity(extraJumpMult*strength);  
				++extraJumpCnt;
			}
		}
	} 

	public void Dash(){
		if (dashCnt < spCommandCharges[1]){
			dashTimer = Time.time + dashDuration; 
			++dashCnt;
		} 
	}

	public override void Kill(){
		if (this.gameObject.activeSelf) Instantiate(deathAnimation).transform.position = this.transform.position;
		player.GainFocus();
		player.SetPlayerFrozen(false);
		player.gameObject.GetComponent<SpellController>().ClearRunes(); 
		this.gameObject.SetActive(false); 
	}

	public int GetSpCommandAmt(){
		return spCommandAmt;
	}
}
