using System;
using System.Collections.Generic;
using Unicorn.Hitbox;
using Unity.Collections;
using Unity.Collections.LowLevel.Unsafe;
using UnityEngine;

namespace Unicorn.Boss
{
    public class AttackManager: MonoBehaviour
    {
        public static Dictionary<EntityId, float> _projMap;
        public static Dictionary<EntityId, UnicornHitbox> _projHitboxMap;
        private const float Lifetime = 5f;

        private void Start()
        {
            _projMap = new Dictionary<EntityId, float>();
            _projHitboxMap = new Dictionary<EntityId, UnicornHitbox>();
        }

        private void FixedUpdate()
        {
            foreach (Transform o in transform)
            {
                var entityId = o.GetEntityId();
                if (_projMap.TryAdd(entityId, 0f))
                {
                    if (o.TryGetComponent(out UnicornHitbox hitbox))
                        _projHitboxMap.Add(entityId, hitbox);
                }

                if (o.gameObject.activeInHierarchy)
                    _projMap[entityId] += Time.fixedDeltaTime;
                else continue;

                if (_projMap[entityId] > Lifetime)
                {
                    o.gameObject.SetActive(false);
                    _projMap[entityId] = 0f;
                    continue;
                }

                if (!_projHitboxMap.TryGetValue(entityId, out var unicornHitbox)) continue;
                o.transform.Translate(o.right * (unicornHitbox.hitboxData.hitboxSpeed * Time.fixedDeltaTime), Space.World);
            }
        }
    }
}