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

public class ShielderAI : EnemyBehaviour
{
    public class Swarm : Singleton<Swarm> { }

    // [Header("Attack")]
    // public Ticker attack = new Ticker(2);
    // public float maxRandAttackDelay = 1.4f;

    [Header("Distancing")]
    public Ticker changePos = new Ticker(20);
    public float minDistance = 4f;
    public float maxDistance = 13f;

    // 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 (PlayerManager.I.unit == unit) return;

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

            var player = PlayerManager.I.unit;

            unit.moveTarget = Swarm.I.transform;
            unit.lookTarget = player.transform;
        }
    }

    // public void Attack()
    // {
    //     unit.Attack();
    //     attack.DelayTick(Random.value * maxRandAttackDelay);
    // }
    public void ChangePos()
    {
        UnitController player = PlayerManager.I.unit;

        Vector3 dir = dir = (player.lookTarget.position - player.transform.position).normalized;
        float len = Random.Range(minDistance, maxDistance);

        Swarm.I.transform.position = player.transform.position + dir * len;
    }
}
