using System;
using UniRx;
using UnityEngine;
using UnityEngine.UI;

public class BulkOrderCancelView : MonoBehaviour
{
    private static readonly string CallbackTag = "BulkOrderCancelViewCallback";
    private static readonly int OpenTrigger = Animator.StringToHash("OpenTrigger");
    private static readonly int CloseTrigger = Animator.StringToHash("CloseTrigger");
    [SerializeField] private Animator backgroundAnimator;
    [SerializeField] private Button closeButton;
    
    [SerializeField] private Button cancelButton;
    [SerializeField] private Text coinText;
    [SerializeField] private Text heartText;

    private void Start()
    {
        if (LocalCacheManager.Load<BulkOrderData>(BulkOrder.DataTag, null) is BulkOrderData orderData)
        {
            coinText.text = orderData.rewardCoin.ToString();
            heartText.text = orderData.rewardHeart.ToString();
        }
        closeButton.OnClickAsObservable().Take(1).Subscribe(_ =>
        {
            transform.parent.SetLocalScale(0);
            backgroundAnimator.SetTrigger(CloseTrigger);
            this.CallWaitForSeconds(.25f, () =>
            {
                TransitionManager.Instance.UnloadScene(GameScenes.BulkOrderCancel);
            });
        }).AddTo(this);
        cancelButton.OnClickAsObservable().Take(1).Subscribe(_ =>
        {
            LocalCacheManager.Load<Action>(CallbackTag, null)?.Invoke();
            LocalCacheManager.Remove(CallbackTag);
            transform.parent.SetLocalScale(0);
            backgroundAnimator.SetTrigger(CloseTrigger);
            this.CallWaitForSeconds(.25f, () =>
            {
                TransitionManager.Instance.UnloadScene(GameScenes.BulkOrderCancel);
            });
        }).AddTo(this);
    }

    public static void ShowDialog(Action onComplete = null)
    {
        LocalCacheManager.Save(CallbackTag, onComplete);
        TransitionManager.Instance.LoadSceneAdditive(GameScenes.BulkOrderCancel);
    }
}
