using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Stressbar : MonoBehaviour
{
    [SerializeField] Image[] stressimages;

    private void Start()
    {
        Init();
    }

    private void Init() 
    {
        Array.ForEach(stressimages, x => x.gameObject.SetActive(false));
    
    }

    private void OnEnable()
    {
        EventManager.Instance.onStress += AddNewStressImage;
        EventManager.Instance.onTransformation += EmptyStressImages;
    }

    private void OnDisable()
    {
        EventManager.Instance.onStress -= AddNewStressImage;
        EventManager.Instance.onTransformation -= EmptyStressImages;
        Debug.Log("stressbar disable");
        Init();
    }

    private void AddNewStressImage(Sprite stressImage)
    {
        Image image = GetFreeStressimage();
        if (image != null)
        {
            image.sprite = stressImage;
            image.gameObject.SetActive(true);
        }
        else
        {
            Debug.Log("Transformation");
            EventManager.Instance.Transformation(Transformation.Hyde);
        }
    }

    private void EmptyStressImages(Transformation tr)
    {
        Array.ForEach(stressimages, x => x.gameObject.SetActive(false));
    }

    private Image GetFreeStressimage()
    {
        return Array.Find(stressimages, x => !x.gameObject.activeSelf);
    }

}
