using UnityEngine;
using System.Collections;

public class pacmanMove : MonoBehaviour {

    // Set characters speed
    public float speed = 0.4f;
    public float superSpeed = 0.05f;
    float originalSpeed;

	Vector2 dest = Vector2.zero;
	Vector3 rotatorTemp;
	int direction, NORTH=1,EAST=2,SOUTH=3,WEST=4;
	SpriteRenderer sr, srChild;
	Sprite spriteOriginal, spriteCape;
	public Sprite spriteUp;
	public Sprite spriteDown;
    public Cape cape;

    //godmode on respawn stuff
    bool godMode = false;
    float godtime, godDuration = 3;

	void Start() {
		dest = transform.position;
		rotatorTemp = transform.rotation.eulerAngles;
		sr = GetComponent<SpriteRenderer>();
		spriteOriginal = sr.sprite;
        originalSpeed = speed;
		if (cape != null) {
			srChild = cape.GetComponent<SpriteRenderer> ();
			spriteCape = srChild.sprite;
		}
	}

	void FixedUpdate() {

        //check for godMode (on respawn)
        if (godMode)
        {
            //check if it's time to disable godmode
            if (Time.fixedTime - godtime > godDuration)
            {
                resetSpeed();
            }
        }

		// Move closer to Destination
		Vector2 p = Vector2.MoveTowards(transform.position, dest, speed);
		GetComponent<Rigidbody2D>().MovePosition(p);
		float vertical = Input.GetAxis ("Pac UpDown");
		float horizontal = Input.GetAxis ("Pac Leftright");

		//on "up" move up and rotate sprite up
		if ((Input.GetKey(KeyCode.UpArrow) || vertical < -0.1f)  && 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 ((Input.GetKey(KeyCode.RightArrow) || horizontal > 0.1f) && valid(Vector2.right))
		{
			//set destination to "East"
			direction = EAST;

			//CHANGE TO "RIGHT" Sprite
			sr.sprite = spriteOriginal;
			if (cape != null) {
				srChild.sprite = spriteCape;
			}
		}

		//on "down" move down and rotate sprite down
		if ((Input.GetKey(KeyCode.DownArrow)|| vertical > 0.1f) && 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 ((Input.GetKey(KeyCode.LeftArrow)|| horizontal < -0.1f) && valid(-Vector2.right))
		{
			//set destination to "West"
			direction = WEST;

			//CHANGE TO "LEFT" Sprite
			sr.sprite = spriteOriginal;
			if (cape != null) {
				srChild.sprite = spriteCape;
			}
		}

		if (direction == WEST)
		{
			sr.flipX = true;
			if (cape != null) {
				srChild.flipX = true;
			}
		}
		else if (direction == EAST)
		{
			sr.flipX = false;
			if (cape != null) {
				srChild.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>());
		*/
	}

    public void resetDest()
    {
        dest = transform.position;
        direction = 5;
    }
    void respawnAmp()
    {
        //Debug.Log("PacVan is back!");
        speed = superSpeed;
        //move to PacGhost layer
        gameObject.layer = 14;
        gameObject.tag = "NotPlayer";

        //colour PacVan white
        sr.color = Color.black;
		if (cape != null) {
			srChild.color = Color.black;
		}

        //get timestamp of when we started godmode
        godtime = Time.fixedTime;
        godMode = true;
    }

    void resetSpeed()
    {
        speed = originalSpeed;

        //colour PacVan normal
        sr.color = Color.white;
		if (cape != null) {
			srChild.color = Color.white;
		}

        //move to PacVan layer
        gameObject.layer = 10;
        gameObject.tag = "Player";
        godMode = false;
    }
}