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

public class AssignPlayer : MonoBehaviour
{

    int randomNumberP1 = 0, randomNumberP2 = 0, randomNumberP3 = 0;
    float timeDelay = 2.25f;
    float timeReset = 2.25f;
    float timeDelay2 = 2.0f;
    public Material red, blue, green, purple;
    bool goP1 = true;
    bool goP2 = false;
    bool goP3 = false;
    bool start = false;
    bool loopBreaker1 = false;
    bool loopBreaker2 = false;
    int loopBreaker3 = 0;
    public string assignedColor1 = null;
    public string assignedColor2 = null;
    public string assignedColor3 = null;

    public GameObject[] players;

    public AudioSource p1Assigned, p2Assigned, p3Assigned;

    // Use this for initialization
    void Start()
    {

        randomNumberP1 = Random.Range(1, 5);

        while (loopBreaker1 == false)
        {
            randomNumberP2 = Random.Range(1, 5);

            if (randomNumberP2 != randomNumberP1)
            {
                loopBreaker1 = true;
            }
        }

        while (loopBreaker2 == false)
        {
            randomNumberP3 = Random.Range(1, 5);

            if (randomNumberP3 != randomNumberP1)
            {
                if (randomNumberP3 != randomNumberP2)
                {
                    loopBreaker2 = true;
                }
            }
        }

        Debug.Log(randomNumberP1 + " " + randomNumberP2 + " " + randomNumberP3);

    }

    // Update is called once per frame
    void Update()
    {
        timeDelay -= Time.deltaTime;

        if (timeDelay <= 0 && goP1 == true && start == false)
        {
            if (randomNumberP1 == 1)
            {
                players[0].GetComponent<Renderer>().material = red;
                assignedColor1 = "red";
                p1Assigned.Play();
                timeDelay = timeDelay + timeReset;
                goP1 = false;
                goP2 = true;
            }
            else if (randomNumberP1 == 2)
            {
                players[0].GetComponent<Renderer>().material = blue;
                assignedColor1 = "blue";
                p1Assigned.Play();
                timeDelay = timeDelay + timeReset;
                goP1 = false;
                goP2 = true;
            }
            else if (randomNumberP1 == 3)
            {
                players[0].GetComponent<Renderer>().material = green;
                assignedColor1 = "green";
                p1Assigned.Play();
                timeDelay = timeDelay + timeReset;
                goP1 = false;
                goP2 = true;
            }
            else if (randomNumberP1 == 4)
            {
                players[0].GetComponent<Renderer>().material = purple;
                assignedColor1 = "purple";
                p1Assigned.Play();
                timeDelay = timeDelay + timeReset;
                goP1 = false;
                goP2 = true;
            }
        }

        if (timeDelay <= 0 && goP2 == true && start == false)
        {
            if (randomNumberP2 == 1)
            {
                players[1].GetComponent<Renderer>().material = red;
                assignedColor2 = "red";
                p2Assigned.Play();
                timeDelay = timeDelay + timeReset;
                goP2 = false;
                goP3 = true;
            }
            else if (randomNumberP2 == 2)
            {
                players[1].GetComponent<Renderer>().material = blue;
                assignedColor2 = "blue";
                p2Assigned.Play();
                timeDelay = timeDelay + timeReset;
                goP2 = false;
                goP3 = true;
            }
            else if (randomNumberP2 == 3)
            {
                players[1].GetComponent<Renderer>().material = green;
                assignedColor2 = "green";
                p2Assigned.Play();
                timeDelay = timeDelay + timeReset;
                goP2 = false;
                goP3 = true;
            }
            else if (randomNumberP2 == 4)
            {
                players[1].GetComponent<Renderer>().material = purple;
                assignedColor2 = "purple";
                p2Assigned.Play();
                timeDelay = timeDelay + timeReset;
                goP2 = false;
                goP3 = true;
            }
        }

        if (timeDelay <= 0 && goP3 == true && start == false)
        {
            if (randomNumberP3 == 1)
            {
                players[2].GetComponent<Renderer>().material = red;
                assignedColor3 = "red";
                p3Assigned.Play();
                goP3 = false;
                start = true;
            }
            else if (randomNumberP3 == 2)
            {
                players[2].GetComponent<Renderer>().material = blue;
                assignedColor3 = "blue";
                p3Assigned.Play();
                goP1 = false;
                start = true;
            }
            else if (randomNumberP3 == 3)
            {
                players[2].GetComponent<Renderer>().material = green;
                assignedColor3 = "green";
                p3Assigned.Play();
                goP1 = false;
                start = true;
            }
            else if (randomNumberP3 == 4)
            {
                players[2].GetComponent<Renderer>().material = purple;
                assignedColor3 = "purple";
                p3Assigned.Play();
                goP1 = false;
                start = true;
            }
        }

        if (start == true && loopBreaker3 == 0)
        {
            players[0].GetComponent<P1control>().start = true;
            start = false;
            loopBreaker3++;
        }
    }

}
