using UnityEngine;

public class EnemyScript : MonoBehaviour
{
    [Header("Main parameters")]
    [SerializeField] bool isBoss;
    [SerializeField] bool hasHealthBar;
    [SerializeField] bool goOutOfScreen;
    [SerializeField] bool needToBeDefeated;
    [SerializeField] int healthBarType;
    [SerializeField] bool patternType;
    [SerializeField] bool isDisabled;


    [Header("Main options")]
    [SerializeField] int maxHealth;
    [SerializeField] int health;
    [SerializeField] int damage;
    [SerializeField] int speed;


    [Header("Ai")]
    public float movementX;
    public float movementY;
    [SerializeField] private Rigidbody rb;

    [Header("Sounds effects")]
    public AudioSource audioSource;
    public AudioClip shootSound;
    public AudioClip damageSound;
    public AudioClip deathSoundA;
    public AudioClip deathSoundB;

    [Header("Animations")]
    public const string IDLE = "idle";
    public const string DEATH = "death";
    public const string RIGHT = "rightStay";
    public const string ATTACK2 = "Attack2";

    string currentAnimationState;

    [Header("Particles")]
    public ParticleSystem ParticleDamage;
    public ParticleSystem ParticleExplosion;
    public ParticleSystem ParticleSmoke;

    [Header("Misc.")]
    [SerializeField] UI_HealthBar healthBar;
    [SerializeField] BoxCollider boxcollider;
    public Animator animator;
    // public GameObject myObject;

    // -==============================- DEFAULT SCRIPT'S LOOPS -=============================-
    private void Awake()
    {
        health = maxHealth;
        healthBar = GetComponentInChildren<UI_HealthBar>();
        healthBar.UpdateHealthBar(health, maxHealth);
    }

    // Start is called once before the first execution of Update after the MonoBehaviour is created
    void Start()
    {
        
    }

    private void FixedUpdate()
    {
        if(!isDisabled)
        {
            MoveApply();
            MoveReduce();
        }
        SetAnimations();
    }
    // Update is called once per frame
    void Update()
    {
        healthBar.UpdateHealthBar(health, maxHealth);
    }

    // -==============================- FUNCTIONS -=============================-

    private void MoveApply()
    {
        print("MoveApply est appelé");
        rb.linearVelocity = new Vector3(movementX, 0, movementY);
    }

    private void MoveReduce()
    {
        movementX = movementX * 0.98f;
        movementY = movementY * 0.98f;
    }

    private void SetAnimations()
    {
        if (health > 0)
            ChangeAnimationState(IDLE);

        else if (health < 0)
            ChangeAnimationState(DEATH);

    }

    public void ChangeAnimationState(string newState)
    {
        // Prevent the same animation from interrupting with itself
        if (currentAnimationState == newState) return;

        // Play the animation 
        currentAnimationState = newState;
        animator.CrossFadeInFixedTime(currentAnimationState, 0.2f);
    }

    // -==============================- EXTERNAL -=============================-

    private void OnTriggerEnter(Collider other)
    {
        print("Une collision a été détéctée");
        if (other.gameObject.tag == "PlayerProjectile")
        {
            // apply damage
            health -= 10;
            audioSource.PlayOneShot(damageSound);
            healthBar.UpdateHealthBar(health, maxHealth);
            ParticleDamage.Play();

            // If death
            if (health < 0)
            {
                // myObject.GetComponent<BoxCollider>().enabled = false;
                ParticleExplosion.Play();
                ParticleSmoke.Play();
                Destroy(boxcollider);
                audioSource.PlayOneShot(deathSoundA);
                audioSource.PlayOneShot(deathSoundB);
                print("Mort assurée");
                isDisabled = true;
            }
            Destroy(other.gameObject);
        }
    }
}