using System;
using UniRx;
using UnityEngine;

public class ActionProgress : MonoBehaviour
{
    // UI Elements:
    [SerializeField] private UltimateCircularHealthBar progressUI;
    [SerializeField] private UltimateCircularHealthBar backgroundUI;

    // Internal progress values
    private IntReactiveProperty progressAmount = new IntReactiveProperty(0);
    private BoolReactiveProperty isInProgress = new BoolReactiveProperty(false);
    private int maxProgressAmount;
    
    public static ActionProgress Instance { get; private set; }
    
    private void Awake()
    {
        if (Instance != null && Instance != this)
        {
            Destroy(this);
        }
        else
        {
            Instance = this;
        }

        progressAmount.Subscribe(progressValue =>
        {
            if (isInProgress.Value)
            {
                progressUI.RemovedSegments = maxProgressAmount - progressValue;
            }
        });
        
        isInProgress.Subscribe(progress =>
        {
            if (progress)
            {
                progressAmount.Value = 0;
                
                // activate progress UI:
                progressUI.gameObject.SetActive(true);
                backgroundUI.gameObject.SetActive(true);
                
                // set max value in the ui:
                progressUI.SegmentCount = maxProgressAmount;
                backgroundUI.SegmentCount = maxProgressAmount;
                
                progressUI.RemovedSegments = maxProgressAmount - progressAmount.Value;
            }
            else
            {
                // deactivate progress UI:
                progressUI.gameObject.SetActive(false);
                backgroundUI.gameObject.SetActive(false);
            }
        });
        
    }


    public void SetMaxProgressAmount(int maxProgressAmt)
    {
        maxProgressAmount = maxProgressAmt;
    }

    public void IncreaseProgressAmount()
    {
        Debug.Log("Increase Progress Amount");
        progressAmount.Value++;
    }

    public void ResetProgressAmount()
    {
        progressAmount.Value = 0;
    }

    public void SetInProgress(bool inProgress)
    {
        Debug.Log($"SetInProgress: {inProgress}");
        isInProgress.Value = inProgress;
    }
    
}
