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

public class Broadcaster : MonoBehaviour {

    public float broadcastRadius = 3f;

    public class broadcastReceiver
    {
        public Transform transform;
        public float distance;
    }
    List<broadcastReceiver> Receivers = new List<broadcastReceiver>();

    private float timer = 0f;
    private float broadcastTimer = .25f;
    public List<Transform> contactedBroadcasters = new List<Transform>();
    public bool connectionPaired = false;
    public WorldController WC;

    private void Start()
    {
        GetComponentInChildren<SpriteRenderer>().size = new Vector2(broadcastRadius, broadcastRadius);
    }

    // Update is called once per frame
    void Update () {
        timer += Time.deltaTime;
        if(timer > broadcastTimer)
        {
            connectionPaired = TryConnection(transform, broadcastRadius);
            if (connectionPaired)
            {
                WC.OpenTransmission(transform);
                List<Vector3> Vectors = new List<Vector3>();
                Vectors.Add(transform.position);
                foreach (Transform tr in contactedBroadcasters)
                {
                    Vectors.Add(tr.transform.position);
                }
                GetComponent<LineRenderer>().positionCount = Vectors.Count;
                GetComponent<LineRenderer>().SetPositions(Vectors.ToArray());
            }
            else
            {
                WC.CloseTransmission(transform);
                GetComponent<LineRenderer>().positionCount = 0;
            }
        }
	}

    public bool TryConnection(Transform broadcaster, float broadcastRadius)
    {
        contactedBroadcasters = new List<Transform>();
        Collider2D[] colliders = Physics2D.OverlapCircleAll(broadcaster.position, broadcastRadius);
        foreach (Collider2D col in colliders)
        {
            if (col.transform.tag == "connection" && col.transform != transform)
            {
                if (!contactedBroadcasters.Contains(col.transform))
                {
                    contactedBroadcasters.Add(col.transform);
                }
            }
        }
        for(int i = 0; i < contactedBroadcasters.Count; i++)
        {
            if (contactedBroadcasters[i].name == "Satellite")
            {
                Debug.Log("Satellite found");
                return true;
            }
            if (contactedBroadcasters[i].GetComponent<Pillar>())
            {
                if(CheckBroadcaster(contactedBroadcasters[i], contactedBroadcasters[i].GetComponent<Pillar>().broadcastRadius))
                {
                    return true;
                }
            }
        }
        return false;
    }

    public bool CheckBroadcaster(Transform broadcaster, float broadcastRadius)
    {
        Collider2D[] colliders = Physics2D.OverlapCircleAll(broadcaster.position, broadcastRadius);
        foreach (Collider2D col in colliders)
        {
            if (col.transform.tag == "connection" && col.transform != transform)
            {
                if (col.transform.name == "Satellite")
                {
                    contactedBroadcasters.Add(col.transform);
                    return true;
                }
                if (!contactedBroadcasters.Contains(col.transform))
                {
                    contactedBroadcasters.Add(col.transform);
                }
            }
        }
        return false;
    }
}
