using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class gameControl : MonoBehaviour
{
    /* gameControl CLASS
     * 
     * Holds and initialises various
     * static variables used by the rest
     * of the program.
     * */

    //Speaker prefab to be replicated 6x
    public GameObject speaker;

    //Spotlight prefab to be replicated
    public GameObject spotlight;

    //The background video animator
    public Animator loopingManimation;

    //Array of actual speakers
    static public GameObject[] speakers = new GameObject[6];

    //HP for each player
    static public int P1HP = 100;
    static public int P2HP = 100;

    static public float P1Stam = 0;
    static public float P2Stam = 0;

    public GameObject P1Win;
    public GameObject P2Win;

    //Time at which the game finished
    private float gameFinishTime = 0;


    // Use this for initialization
    void Start()
    {
        //Re-initialise ALL static values
        P1HP = 100;
        P2HP = 100;
        P1Stam = 0;
        P2Stam = 0;

        Time.timeScale = 1;

        //Throw some speakers around.
        //Speakers will be located as follows:
        //  [S2]            [S5]
        //  [S1]            [S4]
        //  [S0]            [S3]

        for (int i = 0; i < 6; i++)
        {
            speakers[i] = Instantiate(speaker, new Vector3((-2.3f + Mathf.Floor(i / 3) * 4.6f), (0.7f * (i % 3) - 0.3f), 0), Quaternion.identity);

            if (i < 3)
            {
                speakers[i].tag = "leftSpeaker";
                speakers[i].transform.Rotate(0, 90, 0);
            }
            else
            {
                speakers[i].tag = "rightSpeaker";
                speakers[i].transform.Rotate(0, -90, 0);
            }
        }

        //Throw some spotlights around.

        for(int i = 0; i < 5; i++)
        {
            Instantiate(spotlight, new Vector3(-3 + (i * 1.5f), 2.5f, 5.7f), Quaternion.identity);
        }
    }

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

        //If one player has run out of HP, freeze the game
        //and display the end screen.
        //(Also if 90 seconds have passed)
        if (P1HP == 0 || P2HP == 0 || Time.timeSinceLevelLoad > 90)
        {

            //Display the win screen for the person
            //with the most HP
            if (P1HP < P2HP)
            {
                P2Win.SetActive(true);
            }
            else
            {
                P1Win.SetActive(true);
            }

            Time.timeScale = 0;

            //Set the finish time if it has not already been set
            if(gameFinishTime == 0)
            {
                gameFinishTime = Time.unscaledTime;
            }

            //If someone presses a button after 1s of game pause, go back to the menu.
            if((Input.GetButton("P1Fret0") || Input.GetButton("P2Fret0")) && Time.unscaledTime - gameFinishTime > 1)
            {
                UnityEngine.SceneManagement.SceneManager.LoadScene("mainMenu");
            }
        }
        else
        {

            //Regenerate stamina over time
            //(up to the max. of 30)
            if (P1Stam < 15)
            {
                P1Stam += Time.deltaTime * 1;
                if (P1Stam > 15)
                {
                    P1Stam = 15;
                }
            }

            if (P2Stam < 15)
            {
                P2Stam += Time.deltaTime * 1;
                if (P2Stam > 15)
                {
                    P2Stam = 15;
                }
            }


            //Speed up (!) the animation in the background
            //based on player HP
            int lowestHP = P1HP;
            if(P2HP < lowestHP)
            {
                lowestHP = P2HP;
            }
            loopingManimation.speed = 1.5f + (float)(100 - lowestHP) / 40;
        }
    }
}
