using UnityEngine;
using System.Collections;

public class Monster3_Move : MonoBehaviour
{

    // Set characters speed
    public float speed = 0.4f;
    Vector2 dest = Vector2.zero;
    Vector3 rotatorTemp;
    int direction, NORTH = 1, EAST = 2, SOUTH = 3, WEST = 4;

    //Stuff for sprites
    SpriteRenderer sr;
    Sprite spriteOriginal;
    public Sprite spriteUp;
    public Sprite spriteDown;

	bool goToJail;
	public Transform jail;
    public AudioClip Death_sfx;
	bool blue;

    void Start()
    {
        dest = transform.position;
        rotatorTemp = transform.rotation.eulerAngles;

        //Sprite stuff
        sr = GetComponent<SpriteRenderer>();
        spriteOriginal = sr.sprite;

		goToJail = false;
		blue = false;
    }

    void FixedUpdate()
    {
		if (goToJail) {
			Vector2 p = Vector2.MoveTowards (transform.position, jail.position, speed);
			GetComponent<Rigidbody2D> ().MovePosition (p);
            if (Vector2.Distance(transform.position, jail.position) < 0.1)
            {
				goToJail = false;
				dest = transform.position;
				GetComponent<CircleCollider2D> ().enabled = true;
				if (blue) {
					ChangeColorBlue ();
				} else {
					ChangeColorNormal ();
				}
			}
		} else {
			// Move closer to Destination
			Vector2 p = Vector2.MoveTowards (transform.position, dest, speed);
			GetComponent<Rigidbody2D> ().MovePosition (p);

			//get AXIS position on controller
			float vertical_monster3 = Input.GetAxis ("Monster3 UpDown");
			float horizontal_monster3 = Input.GetAxis ("Monster3 Leftright");

			//on "up" move up and rotate sprite up
			if (vertical_monster3 < -0.05f && valid (Vector2.up)) {
				//set destination to "North"
				direction = NORTH;

				//CHANGE TO "UP" Sprite
				sr.sprite = spriteUp;
			}

			//on "right" move up and rotate sprite right
			if (horizontal_monster3 > 0.05f && valid (Vector2.right)) {
				//set destination to "East"
				direction = EAST;

				//CHANGE TO "RIGHT" Sprite
				sr.sprite = spriteOriginal;
			}

			//on "down" move down and rotate sprite down
			if (vertical_monster3 > 0.05f && valid (-Vector2.up)) {
				//set destination to "South"
				direction = SOUTH;

				//CHANGE TO "DOWN" Sprite
				sr.sprite = spriteDown;
			}

			//on "left" move left and rotate sprite left
			if (horizontal_monster3 < -0.05f && valid (-Vector2.right)) {
				//set destination to "West"
				direction = WEST;

				//CHANGE TO "LEFT" Sprite
				sr.sprite = spriteOriginal;
			}

			//Sprite Stuff (facing left or right)
			if (direction == WEST) {
				sr.flipX = true;
			} else if (direction == EAST) {
				sr.flipX = false;
			}

			//continue moving towards destination forever, until another button is pressed to change the destination
			switch (direction) {
			case 1:
                //set destination to "North"
				dest = (Vector2)transform.position + Vector2.up;
				break;
			case 2:
                //set destination to "East"
				dest = (Vector2)transform.position + Vector2.right;
				break;
			case 3:
                //set destination to "South"
				dest = (Vector2)transform.position - Vector2.up;
				break;
			case 4:
                //set destination to "West"
				dest = (Vector2)transform.position - Vector2.right;
				break;
			}
		}

    }

    //check if PacVan can move in this direction without hitting a wall
    bool valid(Vector2 dir)
    {
        //TODO: could fix this to prevent "cheating" and stuff, but it was causing bad things
        return true;

        /*

        // Cast Line from 'next to Pac-Man' to 'Pac-Man

        //get pacVan's current position
        Vector2 pos = transform.position;

        //draw a line starting from pacVan's position, out by a distance of pos (his radius), in the direction of dis (his facing)
        RaycastHit2D hit = Physics2D.Linecast(pos + dir, pos);

        //returns TRUE if there is a collision, false if there is not
        return (hit.collider == GetComponent<Collider2D>());
         */
    }

	void ChangeColorNormal(){
		blue = false;
		if (!goToJail) {
			GetComponent<SpriteRenderer> ().color = new Color (1.0f, 1.0f,1.0f,1.0f); 

		}
	}

	void ChangeColorBlue(){
		blue = true;
		if (!goToJail) {
			GetComponent<SpriteRenderer> ().color = new Color (0.11f, 0.306f, 0.875f); 

		}
	}

	void Eaten(){
        if (Death_sfx != null)
        {
            AudioSource.PlayClipAtPoint(Death_sfx, transform.position);
        }

		goToJail = true;
		GetComponent<CircleCollider2D> ().enabled = false;
		GetComponent<SpriteRenderer> ().color = new Color (1.0f, 1.0f, 1.0f, 0.5f);  
	}
}