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

public class RotateColor : MonoBehaviour
{
    Renderer rend;
    public Material material;
    public Material red, blue, green, purple, neutral;
    public string currentColor;

    public float timerDefault = 1.47f;
    public float timerUnique = 0.0f;
    private float timerActive;
    

    // Use this for initialization
    void Start ()
    {
        rend = GetComponent<Renderer>();
        rend.sharedMaterial = material;

        if (material == red)
        {
            currentColor = "red";
        }
        else if (material == blue)
            {
            currentColor = "blue";
        }
        else if(material == green)
        {
            currentColor = "green";
        }
        else if (material == purple)
        {
            currentColor = "purple";
        }
        else if (material == neutral)
        {
            currentColor = "neutral";
        }
       // Debug.Log(this.name + " starts at " + currentColor);

        TimerSet();

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

        if (timerActive <= 0)
        {
            if (currentColor == "red")
            {
                this.GetComponent<Renderer>().material = blue;
                currentColor = "blue";
                TimerSet();
            }
            else if (currentColor == "blue")
            {
                this.GetComponent<Renderer>().material = green;
                currentColor = "green";
                TimerSet();
            }
            else if (currentColor == "green")
            {
                this.GetComponent<Renderer>().material = purple;
                currentColor = "purple";
                TimerSet();
            }
            else if (currentColor == "purple")
            {
                this.GetComponent<Renderer>().material = red;
                currentColor = "red";
                TimerSet();
            }
            else if (currentColor == "neutral")
            {
                currentColor = "neutral";
            }

            if (currentColor == "neutral")
            {
               // Debug.Log(this.name + " remains " + currentColor);
            }
            else
            {
                //Debug.Log(this.name + " changes to " + currentColor);
            }

        }
    }

    void TimerSet()
    {
        if (timerUnique > 0)
        {
            timerActive = timerUnique;
        }
        else
        {
            timerActive = timerDefault;
        }
    }
}
