using AsteroidGame.Entities.Enemies.Scripts;
using AsteroidGame.Entities.Structures.Scripts;
using AsteroidGame.Interfaces;
using UnityEngine;

namespace AsteroidGame.Entities.Structures.Tower
{
    public class Turret : StructureBase
    {
        [Header("WeaponParameters")] [SerializeField]
        private float weaponRange = 40f;

        [SerializeField] private int damage = 1;
        [SerializeField] private float fireRate = 1;
        [SerializeField] private float projectileSpeed = 5;
        [SerializeField] private TargetStrategy targetStrategy = TargetStrategy.LowestHealth;
        
        [Header("Stats")] 
        [SerializeField]
        private float score = 0f;


        [Header("RigConnections")] 
        [SerializeField] private Transform turretHead;
        [SerializeField] private Transform barrel;

        #region Privates
        
        
        [SerializeField]
        enum TargetStrategy
        {
            ClosestEnemy,
            LowestHealth
        };

        [SerializeField] private EnemyBase targetEnemy;
        [SerializeField] private Transform[] buildingParts;


        #endregion

        #region Publics

        public float MaxDistance { get; set; }

        public int Damage
        {
            get => damage;
            set => damage = value;
        }

        public int FireRate { get; set; }
        
        #endregion

        private void Awake()
        {
            targetEnemy = FindObjectOfType<EnemyBase>();
        }

        private void Start()
        {
            // enemyHandler = FindObjectOfType<EnemyHandler>();
            // scoreHandler = FindObjectOfType<ScoreHandler>();
            UpdateWeaponParameters(fireRate, projectileSpeed);
        }

        private void Update()
        {
            turretHead.transform.LookAt(targetEnemy.GetCenterPosition());
        }
        
        private void UpdateWeaponParameters(float _fireRate, float _projectileSpeed)
        {
            // var main = _projectile.main;
            // main.startSpeed = _projectileSpeed;
            //
            // var emission = _projectile.emission;
            // emission.rateOverTime = _fireRate;
        }

        // Update is called once per frame

        // private void FindAndShootEnemy()
        // {
        //     float bestValue = Mathf.Infinity;
        //     bool targetFound = false;
        //     List<GameObject> enemies = enemyHandler.ReturnAllEnemies();
        //
        //     foreach (GameObject enemy in enemies)
        //     {
        //         float distanceToTarget = Vector3.Magnitude(enemy.transform.position - transform.position);
        //
        //         bool withinRange = distanceToTarget < weaponRange;
        //         if (withinRange)
        //         {
        //             if (targetStrategy == TargetStrategy.ClosestEnemy)
        //             {
        //                 bool isClosest = distanceToTarget < bestValue;
        //                 if (isClosest)
        //                 {
        //                     targetFound = true;
        //                     bestValue = distanceToTarget;
        //                     targetEnemy = enemy;
        //                 }
        //             }
        //             if (targetStrategy == TargetStrategy.LowestHealth)
        //             {
        //                 float enemyHealth = enemy.GetComponent<EnemyHealth>().Health;
        //
        //                 bool isLowestHealth = enemyHealth < bestValue;
        //                 if (isLowestHealth)
        //                 {
        //                     targetFound = true;
        //                     bestValue = enemyHealth;
        //                     targetEnemy = enemy;
        //                 }
        //             }
        //         }
        //     }
        //     
        //     if (targetFound)
        //     {
        //         weapon.transform.LookAt(targetEnemy.transform.position);
        //         ShootProjectile(true);
        //     }
        // }
        // private void OnTriggerEnter(Collider other)
        // {
        //     print($"TriggerEnter: {other.name}");
        //     // if(other.name == "BuildCollider")
        //     // {
        //     //     _buildPlacementBlocked = true;
        //     // }
        // }
        //
        // private void OnTriggerExit(Collider other)
        // {
        //     print($"TriggerExit: {other.name}");
        //     // if(other.name == "BuildCollider")
        //     // {
        //     //     _buildPlacementBlocked = false;
        //     // }
        // }
    }
}
