using UnityEngine;
using System.Collections;

public class PlayerController : MonoBehaviour {

	public static PlayerController instance;

	public float forwardSpeed = 4;
	public LayerMask astroidMask = -1;
	public float approachSpeed = 1;
	public PlayerCollision character;
	public SpriteRenderer JetFire;
	public float JetForce = 5;
	public float JetDelay = 0.5f;
	public float ShootDelay = 0.2f;
	public float ShootForce = 2;

	public GameObject spriteCollisionEffect;

	Transform _transform;
	Transform followTarget;			//the astroid indicated by pointer
	int currentGameMode=-1;
	float lastJetTime = -1;
	float lastShootTime = -1;
	Vector3 lastShootChangeDir;

	float difficultyParam = 1;

	// Use this for initialization
	void Awake () {
		instance = this;
		_transform = transform;
		followTarget = null;
	}
	
	// Update is called once per frame
	void Update () {
		if(currentGameMode!=-1){
			difficultyParam += Time.deltaTime*0.01f;
			//Debug.Log(difficultyParam);
		}


		_transform.position += _transform.right * forwardSpeed * Time.deltaTime * difficultyParam;

		switch(currentGameMode){
		case 0:
			if(Input.GetMouseButtonDown(0)){
				Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
				RaycastHit2D hit = Physics2D.Raycast(new Vector2(mousePos.x, mousePos.y), Vector2.zero, Mathf.Infinity, astroidMask.value);
				if(hit.collider!=null){
					SoundFXCtrl.instance.PlaySound(3,0.3f);
					followTarget = hit.transform;
					_transform.parent = followTarget;
					LineCtrl.instance.EnableLine(_transform, followTarget);
					GunCtrl.instance.EnableGun(followTarget);
				}
			}
			if(Input.GetMouseButtonUp(0)){
				if(followTarget!=null){
					followTarget = null;
					_transform.parent = null;
					LineCtrl.instance.DisableLine();
					GunCtrl.instance.DisableGun();
				}
			}
			if(followTarget!=null){
				Vector3 vectorToTarget = followTarget.position - _transform.position;
				vectorToTarget.z = 0;
				if(Vector3.Angle(vectorToTarget, _transform.right)>90.0f){
					Vector3 newDir = Vector3.RotateTowards(_transform.right, vectorToTarget, approachSpeed * Time.deltaTime, 0);
					_transform.right = newDir;
				}else{
					
				}
			}
			break;
		case 1:
			if(lastJetTime >= 0){
				lastJetTime -= Time.deltaTime;
				Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
				Vector3 deltaV = _transform.position - mousePos;
				deltaV.z = 0;
				deltaV = deltaV.normalized;
				Vector3 newDir = Vector3.RotateTowards(_transform.right, (deltaV + _transform.right).normalized, approachSpeed * Time.deltaTime*0.5f, 0);
				_transform.right = newDir;

				JetFire.enabled = true;
				JetFire.color = new Color(JetFire.color.r, JetFire.color.g, JetFire.color.b, lastJetTime);
			}else{
				JetFire.enabled = false;
			}

			if(Input.GetMouseButtonDown(0)&&lastJetTime<=0){
				SoundFXCtrl.instance.PlaySound(4,0.5f);
				lastJetTime = 1;
				Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
				Vector3 deltaV = _transform.position - mousePos;
				deltaV.z = 0;
				deltaV = deltaV.normalized;
				rigidbody2D.AddForce(deltaV, ForceMode2D.Impulse);
			}
			break;
		case 2:
			if(lastJetTime >= 0){
				lastJetTime -= Time.deltaTime;
				Vector3 newDir = Vector3.RotateTowards(_transform.right, lastShootChangeDir, approachSpeed * Time.deltaTime*0.2f, 0);
				_transform.right = newDir;
			}

			if(lastShootTime>=0){
				lastShootTime -= Time.deltaTime;
			}
			
			if(Input.GetMouseButtonDown(0)&&lastShootTime<=0){
				SoundFXCtrl.instance.PlaySound(5,0.6f);
				lastShootTime = ShootDelay;
				lastJetTime = 0.3f;
				Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
				Vector3 deltaV = _transform.position - mousePos;
				deltaV.z = 0;
				deltaV = deltaV.normalized;
				rigidbody2D.AddForce(deltaV, ForceMode2D.Impulse);
				lastShootChangeDir = (deltaV + _transform.right).normalized;
				GameManager.instance.ShootBullets(-deltaV);
			}

			break;
		}
	}

	public void CollisionHappen(Collision2D conn){
		_transform.right = Vector3.Reflect(_transform.right, conn.contacts[0].normal);
		Instantiate (spriteCollisionEffect, conn.contacts [0].point, Quaternion.identity);
		SoundFXCtrl.instance.PlaySound (6, 1.5f);
	}

	public void GameOver(){
		currentGameMode = -1;
		GunCtrl.instance.gameObject.SetActive (false);
		foreach(GameObject gunType in GunCtrl.instance.guns){
			gunType.SetActive(false);
		}
		LineCtrl.instance.DisableLine ();
		this.enabled = false;
	}

	public void GameStart(int gameMode){
		this.enabled = true;
		_transform.position = Vector3.zero;
		_transform.rotation = Quaternion.identity;
		currentGameMode = gameMode;
		character.Initialize ();
		GunCtrl.instance.gameObject.SetActive (true);
		GunCtrl.instance.Initialize (gameMode);
		difficultyParam = 1;
	}

}
