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

public class Radio : MonoBehaviour {

    public Transform Icon;

    Vector3 iconStartPosition;
    bool HasTransmision = false;
    int currentClip = 0;

	// Use this for initialization
	void Start () {
        DayCycle.OnNewDay += OnNewDay;
        Player.LocatedNearAnItem += LocatedNearAnItem;
        iconStartPosition = Icon.position;
        SoundManager.instance.PlayOnce(SoundManager.instance.radioClips[1]);
    }
	
	// Update is called once per frame
	void Update () {
        if(HasTransmision)
        {
            Icon.gameObject.SetActive(true);
            Icon.position = iconStartPosition + new Vector3(0.0f, Mathf.Sin(Time.time) * 0.5f, 0.0f);
        } else
        {
            Icon.gameObject.SetActive(false);
        }
        
    }

    void OnNewDay()
    {
        HasTransmision = true;
    }

    void LocatedNearAnItem(GameObject go)
    {
        Debug.Log("HEY!!!");
        if(HasTransmision && go.GetComponent<Radio>() != null)
        {
            PlayToday();
        }
    }

    void PlayToday()
    {
        HasTransmision = false;
        SoundManager.instance.PlayOnce(SoundManager.instance.radioClips[currentClip]);
        currentClip++;
        if(currentClip >= SoundManager.instance.radioClips.Count)
        {
            currentClip = 0;
        }
    }
}
