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

public class BulkOrderCancelView : MonoBehaviour
{
    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(_ =>
        {
            if (LocalCacheManager.Load<Action>(BulkOrder.CancelCallbackTag, null) is Action callback)
            {
                callback.Invoke();
            }
            if (LocalCacheManager.Load<Action>(KitchenManager.ArchivedCallbackTag, null) is Action kitchenCallback)
            {
                kitchenCallback.Invoke();
            }
            transform.parent.SetLocalScale(0);
            backgroundAnimator.SetTrigger(CloseTrigger);
            this.CallWaitForSeconds(.25f, () =>
            {
                TransitionManager.Instance.UnloadScene(GameScenes.BulkOrderCancel);
            });
        }).AddTo(this);
    }
}
