namespace Grunt_gun
{
    using System.Collections;
    using System.Collections.Generic;
    using UnityEditorInternal;
    using UnityEngine;


    public class AttackState : BaseState
    {
        private readonly Enemy_1 enemy;
        private float moveTimer;
        private float losePlayerTimer = 0;
        private float shotTimer;
        [SerializeField] private Path path;
        public AttackState(Enemy_1 enemy)
        {
           this.enemy = enemy;
        }
        
        public override void Enter()
        {
            Debug.Log("Entering AttackState");
        }
        public override void Exit()
        {

        }
        public override void Perform()
        {
            Debug.Log("AttackState Perform() method executed");
            
            
                if (enemy.CanSeePlayer())
                {
                    Debug.Log("Player detected in AttackState");
                    losePlayerTimer = 0;
                    moveTimer += Time.deltaTime;
                    shotTimer += Time.deltaTime;
                    enemy.transform.LookAt(enemy.Player.transform);

                    if (shotTimer > enemy.fireRate)
                    {
                        Shoot();
                    }
                    if (moveTimer > Random.Range(3, 7))
                    {
                        enemy.Agent.SetDestination(enemy.transform.position + (Random.insideUnitSphere * 5));
                        moveTimer = 0;
                    }
                }
                else
                {
                    Debug.Log("Player not detected in AttackState");
                    Debug.Log("Player lost in AttackState. Transitioning back to PatrolState");
                    losePlayerTimer += Time.deltaTime;
                    if (losePlayerTimer > 8)
                    {                        
                            enemy.TransitionToPatrolState();                                              
                    }
                }
            
        }

        public void Shoot()
        {
            Transform Fuzil = enemy.Fuzil;
            GameObject bullet = GameObject.Instantiate(Resources.Load("Prefabs/bullet") as GameObject, Fuzil.position, enemy.transform.rotation);
            Vector3 shootDirection = (enemy.Player.transform.position - Fuzil.transform.position).normalized;
            bullet.GetComponent<Rigidbody>().velocity = Quaternion.AngleAxis(Random.Range(-3f, 3f), Vector3.up) * shootDirection * 40;
            Debug.Log("Shoot");
            shotTimer = 0;
        }

        internal bool CanSeePlayer()
        {
            throw new System.NotImplementedException();
        }

        void Start()
        {

        }


        void Update()
        {

        }
    }
}