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

public class TileFader : MonoBehaviour {

    [HideInInspector]
    public bool stable = false;
    public float refreshCd= 5f;

    private TilePropertys tileProperty;
    private AudioSource audio;
    //private GameManager gM;
    //private ColisionChecker cC;
    private bool enableFade;
    private float timer = 0f;

    // Use this for initialization
    void Start () {
        tileProperty = GetComponent<TilePropertys>();
        audio = GetComponent<AudioSource>();
        //gM = GameObject.FindGameObjectWithTag("GameManager").GetComponent<GameManager>();
        //cC = GameObject.FindGameObjectWithTag("Player").GetComponent<ColisionChecker>();
    }
	
	// Update is called once per frame
	void Update () {
        if (enableFade)
        {
            Renderer renderer = GetComponent<Renderer>();
            Material mat = renderer.material;

            timer += Time.deltaTime;

            Color lerpedColor = Color.LerpUnclamped(Color.black, tileProperty.color, Mathf.PingPong(timer, 1));

            mat.SetColor("_EmissionColor", lerpedColor);
            if (timer >= 2f)
            {
                timer = 0f;
                enableFade = false;
                mat.SetColor("_EmissionColor", Color.black);
            }
        }
    }

    void OnTriggerEnter(Collider other)
    {
        if (other.CompareTag("Wave"))
        {
            if (other.GetComponent<WavePropertys>().colorPick == tileProperty.colorPicker)
            {
                enableFade = true;
                stable = true;
                Invoke("refreshStable", refreshCd);
            }
        }

        if (other.CompareTag("Collider"))
        {
            audio.Play();
        }

    }

    void refreshStable()
    {
        stable = false;
    }

}
