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

public class TeslaTower : MonoBehaviour
{
    public float Cooldown;
    public GameObject linePrefab;
    public Light light;
    private bool powered;
    private RopeAttachPoint me;
    private LinkedList<GameObject> kidQueue;
    private float cdTimer;
    public Transform firePoint;
    public GameObject decal;
    public GameObject lightning;
    public GameObject ZAP;
    public ParticleSystem particles;

    [Header("Sounds")]
    public AudioClip attackSound;
    public AudioClip poweredUp;

    

    private void OnTriggerEnter(Collider other)
    {
        if (other.transform.CompareTag("kid"))
        {
            kidQueue.AddLast(other.gameObject);
        }
    }

    private void OnTriggerExit(Collider other)
    {
        if (other.transform.CompareTag("kid"))
        {
            kidQueue.Remove(other.gameObject);
        }
    }

    private void Update()
    {
        powered = false;
        light.enabled = false;
        foreach( GameObject obj in me.GetConnections())
        {
            Generator gen = obj.GetComponent<Generator>();
            if(gen != null)
            {
                if(gen.power > 0)
                {
                    gen.power -= 0;
                    light.enabled = true;
                    powered = true;
                    if (particles.isStopped)
                    {
                        particles.Play(true);

                        if(poweredUp != null)
                            AudioSource.PlayClipAtPoint(poweredUp, transform.position);
                    }
                    
                    break;
                }
            }
        }

        if (!powered)
        {
            if (particles.isPlaying)
            {
                particles.Stop(true);
            }
        }

        if(cdTimer >= Cooldown)
        {
            if(kidQueue.Count != 0 && powered)
            {
                if(kidQueue.First == null)
                {
                    kidQueue.RemoveFirst();
                }
                else if(kidQueue.First.Value.GetComponent<KidController>().state == KidController.KidState.Stunned)
                {
                    kidQueue.RemoveFirst();
                }
                else
                {
                    Attack();
                    cdTimer = 0.0f;
                }     
            }
        }
        else
        {
            cdTimer += Time.deltaTime;
        }
    }

    public void Start()
    {
        kidQueue = new LinkedList<GameObject>();
        cdTimer = 0.0f;
        me = GetComponent<RopeAttachPoint>();
    }

    public void Attack()
    {
        //Tell top kid to die.
        GameObject kid = kidQueue.First.Value;
        LineRenderer line = Instantiate(linePrefab).GetComponent<LineRenderer>();
        Vector3 from = line.transform.InverseTransformPoint(firePoint.position);
        Vector3 to = line.transform.InverseTransformPoint(kid.transform.position);

        GameObject zap = Instantiate(ZAP, firePoint.position, Quaternion.identity);
        zap.transform.up = -Vector3.Normalize(kid.transform.position-firePoint.position);

        Instantiate(decal,  kid.transform.position + (Vector3.down), kid.transform.rotation);

        AudioSource audio = GetComponent<AudioSource>();
        
        audio.clip = attackSound;
        audio.Play();

        line.SetPosition(0, from);
        line.SetPosition(1, to);
        kid.SendMessage("Stun");
        GameManager.instance.KillKid();
        kidQueue.RemoveFirst();
    }
}
