using UnityEngine;
using System.Collections;

public class IsMouseSpinning : MonoBehaviour {

    public float spinZoneDistance;

    public int sequenceAmount = 3;
    public float minAngle = 10;

    public Vector2 mousePos;

    public Vector2 mouseDirectionToPoint;
    public Vector2 oldMouseDirectionToPoint;
    public  float rotateSin;
    public float minSin;

    public int[] leftRightSequences;

    CircleCollider2D circleColliderRef;
    bool inSpinZone = true;

	// Use this for initialization
	void Start () {
        leftRightSequences = new int[sequenceAmount];

        //in the near impossible event that the leftRightSequence has an even number and half of them are positive and the other half are negative, shit will break. 
        //this can be fixed by simply having an odd number of sequences
        if (sequenceAmount % 2 == 0) {
            Debug.LogError("The sequence amount should be an odd number. Check objects with the IsMouseSpinning script and change the sequence amount");
        }

        //get collider for limiting mouse rotation position
        circleColliderRef = GetComponent<CircleCollider2D>();
	}

    //OnMouseEnter and OnMouseExit are being dumb. were just going to caculate the distance between the mouse and the instance of the catfish
    /*
    //using OnMouseEnter and OnMouseExit is a bit fickle. Were going to switch to using OnMouseDown and OnMouseUp and force the player to click to make the wave
    //enable spinning for this fishcat when mouse is over the collider
    void OnMouseEnter() {
        inSpinZone = true;
    }

    //disable spinning for this fishcat when mouse is over the collider
    void OnMouseExit() {
        inSpinZone = false;
        //if the player leaves the spin zone then the sequence is reset to 0 to prevent infinite wave growth
        updateSequence(0);
    }
    */

	// Update is called once per frame
	void Update () {

        //get mouse position
        mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
        mouseDirectionToPoint = mousePos - new Vector2(transform.position.x, transform.position.y);

        //if the distance between the mouse and the object is less than the specified value then the mouse is inTheSpinZone 
        if (Vector3.Magnitude(mouseDirectionToPoint) < spinZoneDistance)
        {
            if (Input.GetMouseButton(0)) {
                inSpinZone = true;
            }

        }
        else {
            inSpinZone = false;
        }

        if (inSpinZone)
        {
            DetectMouseSpin();
        }
        else {
            updateSequence(0);
        }
    }

    Vector3 TwoToThree (Vector2 vector)
    {
        return new Vector3(vector.x, vector.y, 0);
    }

    void DetectMouseSpin() {

        //calculate the cross product between the mouse's current position and the mouse's previous position
        minSin = Mathf.Sin(minAngle - 90);
        if (oldMouseDirectionToPoint != Vector2.zero)
        {
            rotateSin = -(Vector3.Cross(TwoToThree(oldMouseDirectionToPoint), TwoToThree(mouseDirectionToPoint))).z;
        }
        else
        {
            rotateSin = 0;
        }

        //update the mouse's previous position
        oldMouseDirectionToPoint = mouseDirectionToPoint;

        //if the cross product is greater than the minimum sin, then the mouse is spinning clickwise, if the cross product is less than the minimum sin then the mouse is spinning counter clockwise 
        if (rotateSin > minSin)
        {
            updateSequence(1);
        }
        else if (rotateSin < -minSin)
        {
            updateSequence(-1);
        }
        else
        {
            updateSequence(0);

        }
    }

    void updateSequence(int sequence)
    {
        for (int i = leftRightSequences.Length - 1; i > 0; i--)
        {
            leftRightSequences[i] = leftRightSequences[i - 1];
        }
        leftRightSequences[0] = sequence;
    }
}
