using UnityEngine;
using System.Collections;

public class ranged_foe_ai : MonoBehaviour {
	
	enum v_state{up,down};
	enum h_state{left,right,stationary};
	
	v_state vert_move;
	h_state horz_move;
	
	bool ai_can_jump=true;
	bool ai_enabled=false;
	
	private Animator animator;

	public Rigidbody2D ArrowFab;
	
	int ai_health=3;
	
	private GameObject player;
	private BoxCollider2D own_collider;
	private GameObject Camera;
	
	private global_variables variable_script;
	
	private GameObject gameManager;

	private float arrow_position_x;
	
	float foe_speed=2;

	bool arrow_fire=false;

	float currentvelocity_y;

	bool firing;
	
	Vector3 screenPosition;
	float screenX;
	float screenY;
	
	// Use this for initialization
	void Start () {
		
		gameManager = GameObject.FindGameObjectWithTag ("GameController").gameObject;
		variable_script = gameManager.GetComponent<global_variables> ();
		player = GameObject.FindGameObjectWithTag ("Player").gameObject;
		own_collider = GetComponent<BoxCollider2D> ();
		Camera = GameObject.FindGameObjectWithTag ("MainCamera").gameObject;
		animator = this.GetComponent<Animator> ();
		
	}

	void FireArrow(){

		Rigidbody2D ArrowInstance;

		if (player.transform.position.x>transform.position.x){
			transform.localScale = new Vector3 (-1, 1, 1);
			 arrow_position_x=transform.position.x+(own_collider.size.x/2)+(float)0.5;
		}
		else if (player.transform.position.x<transform.position.x){
			transform.localScale = new Vector3 (1, 1, 1);
			arrow_position_x=transform.position.x-(own_collider.size.x/2)-(float)0.5;
		}

		float relativeProjectileX = ((player.transform.position.x - transform.position.x)*100);
		float relativeProjectileY=((player.transform.position.y - transform.position.y)*200);


		Debug.Log (arrow_position_x);
		Debug.Log (transform.position.x);

		variable_script.arrowProjection.Set (relativeProjectileX, relativeProjectileY);
		ArrowInstance=Instantiate (ArrowFab,new Vector3(arrow_position_x, transform.position.y+(own_collider.size.x/4),0),Quaternion.identity) as Rigidbody2D;

		}

	// Update is called once per frame
	void Update () {
		
		currentvelocity_y = rigidbody2D.velocity.y;
		
		screenPosition=Camera.camera.WorldToScreenPoint (gameObject.transform.position);
		
		
		screenX = screenPosition.x;
		screenY = screenPosition.y;
		
		if (ai_health <= 0) {
			Destroy (gameObject, (float)0.5);
		}
		
		if (screenX >= 0 && screenY >= 0) {
			ai_enabled = true;
			
		}
		
		if (ai_enabled == true) {
			
			if (transform.position.x < player.transform.position.x-(float)2.5&&!firing) {
				horz_move = h_state.right;
			} else if (transform.position.x>player.transform.position.x+(float)2.5&&!firing){
				horz_move = h_state.left;
			}
			else {
				horz_move=h_state.stationary;
				if (!arrow_fire){
					arrow_fire=true;
					firing=true;
					animator.SetBool("enemy_firing",true);
					FireArrow ();
					Invoke ("ResetFire",2);
				}
			}



			
			if (transform.position.y < (player.transform.position.y - (own_collider.size.y / 2))) {
				vert_move = v_state.up;
			} else {
				vert_move = v_state.down;
			}
			
			if (horz_move == h_state.right) {
				
				transform.localScale = new Vector3 (1, 1, 1);
				rigidbody2D.velocity = new Vector2 (foe_speed, currentvelocity_y);

			} else {
				
				transform.localScale = new Vector3 (-1, 1, 1);
				rigidbody2D.velocity = new Vector2 (-foe_speed, currentvelocity_y);

			}
			
			if (vert_move == v_state.up && ai_can_jump) {
				rigidbody2D.AddForce (Vector2.up * 5, ForceMode2D.Impulse);
				ai_can_jump = false;

			}
			
		}
		
	}

		void ResetFire(){
			arrow_fire=false;
		}
	
	void ChangeColor (){
		gameObject.GetComponent<SpriteRenderer> ().color = Color.white;
	}
	
	void OnCollisionEnter2D(Collision2D collision){
		if (collision.gameObject.tag == "platform") {
			ai_can_jump = true;
			//animator.SetBool("Jumping",false);
		} else if (collision.gameObject.tag == "projectile") {
			ai_health-=variable_script.rock_damage;
			if(collision.gameObject.transform.position.x<transform.position.x){
				rigidbody2D.AddForce (new Vector2 (5,3), ForceMode2D.Impulse);
			}
			else{
				rigidbody2D.AddForce (new Vector2 (-5,3), ForceMode2D.Impulse);
			}
			gameObject.GetComponent<SpriteRenderer>().color = Color.red;
			
			Invoke ("ChangeColor",(float)0.1);
		}
		
	}


}
