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

public class InformShowScript : MonoBehaviour, InformShow
{
    public float lastinformed;
    public float status;
    public bool active;
    float gamestarttime;
    SpriteRenderer[] renderers;
    Color[] originalColors;

    public void Awake()
    {
        gamestarttime = Time.time;
        lastinformed = Time.time;
        if (active) status = 1;
        else status = 0;

        this.renderers = transform.parent.GetComponentsInChildren<SpriteRenderer>();
        this.originalColors = new Color[this.renderers.Length];
        for (int i = 0; i < this.renderers.Length; i++)
        {
            this.originalColors[i] = this.renderers[i].color;
        }
    }

    public void Show()
    {
        //print("show" + Time.time.ToString());
        lastinformed = Time.time;

        //if (!active) active = true;
        if (Time.time - gamestarttime < 0.1f) { active = true; status = 1; }
    }

    public void FixedUpdate()
    {
        if (Time.time - lastinformed < 0.05f) status = Mathf.MoveTowards(status, 1, 0.05f);
        else status = Mathf.MoveTowards(status, 0, 0.015f);

        if (active && status <= 0)
        {
            active = false;
        }
        if (!active && status >= 1) active = true;

        for (int i = 0; i < this.renderers.Length; i++)
        {
            this.renderers[i].color = new Color(this.originalColors[i].r, this.originalColors[i].g, this.originalColors[i].b, status);
        }


        if (active) GetComponent<Collider2D>().enabled = true;
        else GetComponent<Collider2D>().enabled = false;
    }

}
