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

public class RangeAlly : BaseAlly, TargetWithLifeThatNotifies.IDeathNotifiable
{
    [SerializeReference] float AttackCadence = 2;
    [SerializeReference] Animator anim;

    Transform currentTarget = null;

    [Header("Projectile info")]
    [SerializeField] float ProjectileVelocity = 500;
    [SerializeField] GameObject Projectile = null;
    [SerializeField] GameObject FakeAttack = null;
    [SerializeField] Transform ShootPoint;
    [SerializeField] SpriteRenderer spr;

    Sight sight;
    float TimeToNextAttack = 0;
    GameObject CurrentFakeAttack;
    GameObject CurrentProjectile;
    bool ProjectileFired = false;

    private void Awake()
    {
        sight = GetComponent<Sight>();
    }

    void Start()
    {
       
    }

    void Update()
    {
        if(sight.collidersInSight.Count > 0)
        {
            currentTarget = sight.collidersInSight[0].transform;
        }
        if(currentTarget)
        {
            if (currentTarget.transform.position.x > this.transform.position.x)
                spr.flipX = false;
            else
                spr.flipX = true;
            TimeToNextAttack -= Time.deltaTime;
            TimeToNextAttack = TimeToNextAttack > 0f ? TimeToNextAttack : 0f;

            if (TimeToNextAttack <= 0)
            {
                TimeToNextAttack = 1f / AttackCadence;
                anim.SetTrigger("Shoot");
            }
        }

        if(ProjectileFired && CurrentProjectile == null)
        {
            Destroy(CurrentFakeAttack);
            ProjectileFired = false;
        }

    }

    public void ShootTarget()
    {
        if(currentTarget != null)
        {
            Vector3 UpdatedDirection = new Vector3(currentTarget.position.x, currentTarget.position.y + 0.5f, currentTarget.position.z);
            Vector2 ShootDirection = (UpdatedDirection - ShootPoint.position);
            Quaternion NewRotation = Quaternion.FromToRotation(Vector2.up, ShootDirection);
            Quaternion RotationForParticles = Quaternion.FromToRotation(Vector3.forward, ShootDirection);
            CurrentProjectile = Instantiate(Projectile, ShootPoint.position, NewRotation);
            CurrentFakeAttack = Instantiate(FakeAttack, ShootPoint.position, RotationForParticles);
            FMODUnity.RuntimeManager.PlayOneShot("event:/Foley/PlantShoot");

            if (CurrentProjectile)
            {
                CurrentProjectile.GetComponent<Rigidbody2D>().velocity = new Vector2(0.0f, 0.0f);
                CurrentProjectile.GetComponent<Rigidbody2D>()?.AddForce(ShootDirection.normalized * ProjectileVelocity);
                ProjectileFired = true;
            }
        }
    }

    void TargetWithLifeThatNotifies.IDeathNotifiable.NotifyDeath()
    {
        if (!Dead)
        {
            Dead = true;
            GetComponentInParent<CircleCollider2D>().enabled = false;
            Invoke("Death", 1f);
            anim.SetTrigger("Death");
        }
    }

}
