using UnityEngine;
using System.Collections;

public class foe_ai : MonoBehaviour {

	enum v_state{up,down};
	enum h_state{left,right};

	v_state vert_move;
	h_state horz_move;

	bool ai_can_jump=true;
	bool ai_enabled=false;

	private Animator animator;

	int ai_health=3;

	private GameObject player;
	private BoxCollider2D own_collider;
	private GameObject Camera;

	private global_variables variable_script;
	
	private GameObject gameManager;

	float foe_speed=2;

	float currentvelocity_y;

	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> ();

	}
	
	// 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) {
								horz_move = h_state.right;
						} else {
								horz_move = h_state.left;
						}


						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);
								animator.SetFloat ("Movement",(float)foe_speed);
						} else {
							
								transform.localScale = new Vector3 (-1, 1, 1);
								rigidbody2D.velocity = new Vector2 (-foe_speed, currentvelocity_y);
								animator.SetFloat ("Movement",(float)foe_speed);
						}

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

				}

	}

	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);
				}

	}
}
