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

public enum RoadStatus
{
    EXITS_CLOSED,
    EXIT_OPEN
};

public class TrafficCircle : MonoBehaviour
{
    public SpriteRenderer roadSignRenderer;
    public float speed;
    public ExitBarrier[] exitBarriers;

    private float cw_rotation;
    private bool doRotation;

    void Awake()
    {
        Messenger<string>.AddListener("GameOverFailure", OnGameOverFailure);
        Messenger.AddListener("GameOverGoalReached", OnGameOverGoalReached);
    }

    void OnDestroy()
    {
        Messenger<string>.RemoveListener("GameOverFailure", OnGameOverFailure);
        Messenger.RemoveListener("GameOverGoalReached", OnGameOverGoalReached);
    }

    // Start is called before the first frame update
    void Start()
    {
        cw_rotation = -1.0f;
        doRotation = true;
    }

    // Update is called once per frame
    void Update()
    {
        if(doRotation)
        {
            transform.Rotate(Vector3.forward * cw_rotation * speed);
        }
    }

    void OnGameOverFailure(string message)
    {
        StopRotation();
    }

    void OnGameOverGoalReached()
    {
        StopRotation();
    }

    void StopRotation()
    {
        doRotation = false;
    }

    public void SetSignSprite(Sprite signSprite)
    {
        roadSignRenderer.sprite = signSprite;
    }

    public void CLoseExits()
    {
        foreach(var barrier in exitBarriers)
        {
            barrier.Lower();
        }
    }

    public void OpenExits()
    {
        foreach (var barrier in exitBarriers)
        {
            barrier.Raise();
        }

    }
}
