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

public class MissionView : MonoBehaviour
{
    private static readonly string progressFormat = "{0} / {1}";
    [SerializeField] private Button rewardButton;
    [SerializeField] private GameObject disableObject;
    [SerializeField] private GameObject rewardedObject;
    [SerializeField] private GameObject coinFrame;
    [SerializeField] private GameObject coinObject;
    [SerializeField] private GameObject shopCustomizeCoinObject;
    [SerializeField] private Text coinText;
    [SerializeField] private Text missionText;
    [SerializeField] private TextMeshProUGUI progressText;
    [SerializeField] private Slider progressBar;
    public IObservable<Unit> RewardButton => rewardButton.OnClickAsObservable().TakeUntilDestroy(this);

    public void SetData(MissionData missionData, int currentValue, string specialText = null)
    {
        coinText.text = missionData.reward.ToString();
        missionText.text = specialText ?? missionData.Text;
        progressBar.value = Mathf.InverseLerp(0, missionData.count, currentValue);
        progressText.text = string.Format(progressFormat, currentValue, missionData.count);
        coinObject.SetActive(missionData.RewardType == MissionRewardType.Coin);
        shopCustomizeCoinObject.SetActive(missionData.RewardType == MissionRewardType.ShopCustomizeCoin);
    }

    public void ChangeProgress(int goalValue, int currentValue)
    {
        progressBar.value = Mathf.InverseLerp(0, goalValue, currentValue);
        progressText.text = string.Format(progressFormat, currentValue, goalValue);
    }

    public void SetButtonActive(bool active)
    {
        rewardButton.gameObject.SetActive(active);
        disableObject.SetActive(!active);
        rewardedObject.SetActive(false);
    }

    public void SetRewarded()
    {
        coinFrame.SetActive(false);
        rewardButton.gameObject.SetActive(false);
        disableObject.SetActive(false);
        rewardedObject.SetActive(true);
    }
}