using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System;

public class PropManager : MonoBehaviour
{

    public Image[] props;

    public float delay = 1.0f;
    public float minFadeDuration = 0.25f;
    public float maxFadeDuration = 0.5f;

    float lastFlashed = 0;
    
    int seed = 0;
    bool idle = true;

    // Use this for initialization
    void Start()
    {

    }

    void generateSeed()
    {
        seed = (int)DateTime.Now.Ticks % int.MaxValue;
        UnityEngine.Random.seed = seed;
    }

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

        if (idle && lastFlashed > delay)
        {
            idle = false;
            lastFlashed = 0;

            var propIndex = UnityEngine.Random.Range(0, props.Length);
            var duration = UnityEngine.Random.Range(minFadeDuration, maxFadeDuration);

            for (int i = 0; i < props.Length; i++)
            {
                if (i != propIndex)
                    props[i].gameObject.SetActive(false);
                else
                    props[i].gameObject.SetActive(true);
            }

            StartCoroutine(FadeIt(duration));

        }



    }

    IEnumerator FadeIt(float duration)
    {
        var canvasGroup = GetComponent<CanvasGroup>();
        while (canvasGroup.alpha < 1.0f)
        {
            canvasGroup.alpha += Time.deltaTime / duration;
            yield return new WaitForEndOfFrame();
        }

        while (canvasGroup.alpha > 0.0f)
        {
            canvasGroup.alpha -= Time.deltaTime / duration;
            yield return new WaitForEndOfFrame();
        }

        idle = true;
        lastFlashed = 0;
    }

}
