using UnityEngine;
using System.Collections;
using UnityEngine.Events;

public class WaveReciever : MonoBehaviour 
{
    bool red = false;

    public bool constantCheck = false;

    public UnityEvent outputEvent;
    Collider2D coll;

    void Start()
    {
        if (GetComponent<Renderer>())
        {
            Color temp = GetComponent<Renderer>().material.color;
            if (GetComponent<BoxCollider2D>().enabled)
                GetComponent<Renderer>().material.color = new Color(temp.r, temp.g, temp.b, 1.0f);
            else
                GetComponent<Renderer>().material.color = new Color(temp.r, temp.g, temp.b, 0.3f);
        }
    }

    void OnTriggerEnter2D(Collider2D col)
    {
        coll = col;
        print(col);
        if (!constantCheck)
        {
            if (col.tag == "Wave")
            {
                outputEvent.Invoke();
            }
        }
    }

    void OnTriggerStay2D(Collider2D col)
    {
        coll = col;
        if (constantCheck)
        {
            if (col.tag == "Wave")
            {
                outputEvent.Invoke();
            }
        }
    }

    public void Flash()
    {
        red = !red;
        if (red)
        {
            GetComponent<Renderer>().material.color = Color.red;
        }
        else
        {
            GetComponent<Renderer>().material.color = Color.white;
        }
    }

    public void Push()
    {
        transform.position = Vector3.MoveTowards(transform.position, coll.transform.position, -5f * Time.deltaTime);
    }

    public void ToggleCollision()
    {
        GetComponent<BoxCollider2D>().enabled = !GetComponent<BoxCollider2D>().enabled;

        Color temp = GetComponent<Renderer>().material.color;

        if(GetComponent<BoxCollider2D>().enabled)
            GetComponent<Renderer>().material.color = new Color(temp.r, temp.g, temp.b, 1.0f);
        else
            GetComponent<Renderer>().material.color = new Color(temp.r, temp.g, temp.b, 0.3f);
    }

    public void ShortOutCollision()
    {
        StartCoroutine(IShortOutCollision());
    }

    public IEnumerator IShortOutCollision()
    {
        print("col");
        GetComponent<BoxCollider2D>().enabled = true;

        Color temp = GetComponent<Renderer>().material.color;
        GetComponent<Renderer>().material.color = new Color(temp.r, temp.g, temp.b, 0.3f);
        yield return new WaitForSeconds(1.0f);
        GetComponent<BoxCollider2D>().enabled = false;
        GetComponent<Renderer>().material.color = new Color(temp.r, temp.g, temp.b, 1.0f);
    }
}