using UnityEngine;
using System.Collections;

public class WaveSpinManipulation : MonoBehaviour {

    public GameObject waveObject;
    public float waveYSpawn;
    public float waveSpinHeightDecay = 10;

    IsMouseSpinning spinRef;
    int spinDirection;
    int spinSpeed;

    Vector2 waveSpawnPosition;

    GameObject spawnedWaveObject;
    Wave spawnedWaveScript;

    //we only want them to spawn one wave at a time, so after a wave is spawned we flip this bool to prevent further waves from spawning untill the current wave is released, or what's probably a better idea, when the wave has crashed
    public bool spawnedWave = false;

    void Start () {

        spinRef = GetComponent<IsMouseSpinning>();

	}
	
	void Update () {

        waveSpawnPosition = new Vector2(transform.position.x, waveYSpawn);

        //calculate the direction of the spin. spinDirection is negative or positive depending on the direction
        spinDirection =  CalculateDirection(spinRef.leftRightSequences);

        //calculate how fast the player is spinning around the catcatfish. the direction is needed to determine which values to take into account when calculating the speed
        spinSpeed = CalculateSpeed(spinRef.leftRightSequences, spinDirection);

        SpawnWave();

	}

    void SpawnWave() {


        //if the player is spinning around the catfish and the spin direction is positive, instantiate a wave as a child 
        //the movement of the wave is also going to have to effect the transform of the catfish(?)
        if (spinDirection == -1 || spinDirection == 1)
        {
            if (!spawnedWave)
            {
                //spawn the wave. this is going to be tricky beacuse its difficult to keep the spin direcion value consistant
                spawnedWaveObject = Instantiate(waveObject, waveSpawnPosition, Quaternion.identity) as GameObject;
                spawnedWaveScript = spawnedWaveObject.GetComponent<Wave>();
                spawnedWave = true;
            }

            //while the spin direction is still 1 (player is still spinning the mouse) increase the height of the wave
            //beacuse the max height is something like 5, if we added the raw spin speed to the height of the wave it would get real fast real quick. by dividing it by 10 we can make it more reaonsable
            spawnedWaveScript.height += spinSpeed / waveSpinHeightDecay;

        }
        else {
            try
            {
                //if the spinDirection is ever 0 or -1 then the wave is given speed corrpsponding to its height
                spawnedWaveScript.speed = spawnedWaveScript.height;
                if (spawnedWaveScript.speed < 1) {
                    spawnedWaveScript.speed = 1;
                }

                //tempor
                spawnedWave = false;
            }
            catch {
                Debug.Log("No player wave currently exists");
            }

        }




    }

    int CalculateDirection(int[] spinDirectionArray) {
        //loop through the array and get the number of positive and negative numbers notating each direction
        int positiveTicks = 0;
        int negativeTicks = 0;

        for (int i = 0; i < spinDirectionArray.Length; i++) {
            if (spinDirectionArray[i] == 1) {
                positiveTicks++;
            }
            if (spinDirectionArray[i] == -1) {
                negativeTicks++;
            }
        }

        // if theres more positive ticks than negative then the player is spinning clockwise. the same is true vice versa
        if (positiveTicks > negativeTicks) {
            return 1;
        }
        if (negativeTicks > positiveTicks) {
            return -1;
        }

        return 0;
    }

    //take note that the average max speed a player can reach with a 2.5 sensitvity is about 5
    int CalculateSpeed(int[] spinSpeedArray, int spinDirectionValue) {

        int speedCalculation = 0;

        //depending on the spin direcion, loop through the spin speed aray and total the number of relevant values
        if (spinDirectionValue == 1) {
            for (int i = 0; i < spinSpeedArray.Length; i++) {
                if (spinSpeedArray[i] == 1) {
                    speedCalculation++;
                }
            }
        }

        if (spinDirectionValue == -1)
        {
            for (int i = 0; i < spinSpeedArray.Length; i++)
            {
                if (spinSpeedArray[i] == -1)
                {
                    speedCalculation++;
                }
            }
        }

        return speedCalculation;
    }
}
