using UnityEngine;
using System.Collections;

public class Catfish : MonoBehaviour {

    WaveSpinManipulation catfishScript;
    public float moveSpeed;
    float slowSpeed;
    public float slowdownRate;

    float defaultSpeed;

    //boost after wave remains unused
    float fastSpeed;
    public float fastSpeedMultiplier;

    public float fastSpeedTimer;

    public float wiskerDistance;
    Vector2 directionToMoveIn;

    bool boostAfterWave;

	void Start () {

        //pick a random direction to move in and orient yourself in that direction
        directionToMoveIn = new Vector2(Random.Range(-1f,1f), Random.Range(-1f,1f)).normalized;
        transform.right = directionToMoveIn;

        catfishScript = GetComponentInChildren<WaveSpinManipulation>();
        defaultSpeed = moveSpeed;
        slowSpeed = moveSpeed / slowdownRate;
        fastSpeed = moveSpeed * fastSpeedMultiplier;
    }

	void Update () {
        //if the player is making a wave then slow down that catfish
        if (catfishScript.spawnedWave)
        {
            //slow down the catfish
            moveSpeed = slowSpeed;
        }
        else {
            //return the catfish to default speed
            moveSpeed = defaultSpeed;
        }

        Move();

        
    }


    void ResetSpeed() {
        moveSpeed = defaultSpeed;
    }

    void Move() {

        transform.position += (transform.right * moveSpeed) * Time.deltaTime;

        //have a short set of wiskers in front of you. if they detect anything, recalculate the random direction you move in

        if (Physics2D.Raycast(transform.position, transform.right, wiskerDistance))
        {
            directionToMoveIn = new Vector2(Random.Range(-1f, 1f), Random.Range(-1f, 1f)).normalized;
            transform.right = directionToMoveIn;
        }

        if (Physics2D.Raycast(transform.position, Quaternion.Euler(0, 45, 0) * transform.right, wiskerDistance))
        {
            directionToMoveIn = new Vector2(Random.Range(-1f, 1f), Random.Range(-1f, 1f)).normalized;
            transform.right = directionToMoveIn;
        }
        if (Physics2D.Raycast(transform.position, Quaternion.Euler(0, -45, 0) * transform.right, wiskerDistance))
        {
            directionToMoveIn = new Vector2(Random.Range(-1f, 1f), Random.Range(-1f, 1f)).normalized;
            transform.right = directionToMoveIn;
        }

    }

}
