using System;
using UnityEngine;
using UnityEngine.UI;
using Random = UnityEngine.Random;

public class BossAttackElements : MonoBehaviour
{
    public static Action TakeDamage;
    [SerializeField] private Transform cucumbers;
    [SerializeField] private Animator bossAnimator;
    [SerializeField] private Image healthBar;

    [SerializeField] private int health = 100;
    [SerializeField] private int damageValue = 1;
    private int _maxHealth;

    private void Start()
    {
        _maxHealth = health;
        TakeDamage += HealthSystem;
    }

    private void OnDestroy()
    {
        TakeDamage -= HealthSystem;
    }

    public void DisableRandomCucumbers()
    {
        foreach (Transform cucumber in cucumbers)
        {
            cucumber.GetChild(0).gameObject.SetActive(true);
        }
        int randomCucumber = Random.Range(5, cucumbers.childCount - 5);
        cucumbers.GetChild(randomCucumber).GetChild(0).gameObject.SetActive(false);
        cucumbers.GetChild(randomCucumber + 1).GetChild(0).gameObject.SetActive(false);
        bossAnimator.SetTrigger("Jump");
    }

    private void HealthSystem()
    {
        health -= damageValue;
        healthBar.fillAmount = (float)_maxHealth / health;
        if (health <= 0)
        {
            PlayerController.Win?.Invoke();
        }
    }
}
