using System.Collections.Generic;
using GameDev.CoreSystems;
using UnityEngine;

namespace AsteroidGame.Entities
{
    public class Turret : StructureBase
    {
        [Header("WeaponParameters")]
        [SerializeField] private int _damage = 1;
        [SerializeField] private float _fireRate = 1;
        [SerializeField] private float _projectileSpeed = 5;

        [Header("Stats")]
        [SerializeField] private float _kills = 0f;
        [SerializeField] private float _damageDealt = 0f;

        [field: Header("Weapon")]
        [field: SerializeField] public List<Weapon> Weapons { get; set; }

        [field: Header("Targeter")]
        [field: SerializeField] public Targeter Targeter { get; set; }

        protected override void OnEnable()
        {
            base.OnEnable();
            InitializeTargeter();
        }

        private void InitializeTargeter()
        {
            Targeter.SetParent(Targetable);
        }

        private void Update()
        {
            if (Targeter.FoundTarget())
            {
                foreach (var weapon in Weapons)
                {
                    weapon.PivotPoint.transform.LookAt(Targeter.GetTarget().GetCenterPosition());
                    weapon.FireWeapon = true;
                }
            }
            else
            {
                foreach (var weapon in Weapons)
                {
                    weapon.FireWeapon = false;
                }
            }
        }
    }
}