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

public class MissionCompleteView : MonoBehaviour
{
    private static readonly int OpenTrigger = Animator.StringToHash("OpenTrigger");
    private static readonly int CloseTrigger = Animator.StringToHash("CloseTrigger");
    
    [SerializeField] private Animator backgroundAnimator;
    [SerializeField] private GameObject normalMissionObject;
    [SerializeField] private GameObject dailyMissionObject;
    [SerializeField] private Button closeButton;
    [SerializeField] private Text coinText;
    
    private void Start()
    {
        var (missionData, callback) = LocalCacheManager.Load< (MissionData, Action)>(MissionManager.MissionDataTag);
        LocalCacheManager.Remove(MissionManager.MissionDataTag);
        closeButton.OnClickAsObservable().Take(1).Subscribe(_ =>
        {
            callback?.Invoke();
            transform.SetLocalScale(0);
            backgroundAnimator.SetTrigger(CloseTrigger);
            this.CallWaitForSeconds(.25f, () =>
            {
                TransitionManager.Instance.UnloadScene(GameScenes.MissionAchievement);
            });
        }).AddTo(this);
        normalMissionObject.SetActive(missionData.Category == MissionCategory.Normal);
        dailyMissionObject.SetActive(missionData.Category == MissionCategory.Daily);
        coinText.text = missionData.reward.ToString();
    }
}
