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

public class ShooterAI : EnemyBehaviour
{
    [Header("Attack")]
    public Ticker attack = new Ticker(2);
    public float maxRandAttackDelay = 1.4f;

    [Header("Targeting")]
    public float smoothDamp = 0.4f;
    public Vector3 velocity;

    [Header("Distancing")]
    public Ticker changePos = new Ticker(5);
    public float distance = 5;
    public Vector3 delta;

    // private bool isPlayerAlive = true;

    // void Awake() {
    //     PlayerManager.I.playerDeath += OnPlayerDeath;
    // }

    // protected override void OnDestroy() {
    //     PlayerManager.I.playerDeath -= OnPlayerDeath;
    // }

    // private void OnPlayerDeath() {
    //     isPlayerAlive = false;
    // }

    private void Start()
    {
        attack.onTick.AddListener(Attack);
        changePos.onTick.AddListener(ChangePos);
    }
    private void Update()
    {
        if (/*isPlayerAlive && */PlayerManager.I.unit != null)
        {
            if (isPlayerControlled) return;

            attack.Tick(Time.deltaTime);
            changePos.Tick(Time.deltaTime);

            var player = PlayerManager.I.unit;

            unit.lookTarget = look;
            unit.moveTarget = follow;

            look.transform.position = Vector3.SmoothDamp(look.transform.position, player.transform.position, ref velocity, smoothDamp);
        }
    }

    public void Attack()
    {
        unit.Attack();
        attack.DelayTick(Random.value * maxRandAttackDelay);
    }
    public void ChangePos()
    {
        delta = new Vector2();
        delta.x = Random.value;
        delta.z = Mathf.Sqrt(1 - delta.x * delta.x);
        delta *= distance;

        var player = PlayerManager.I.unit;
        follow.position = player.transform.position + delta;
    }
}
