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 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 curentValue)
    {
        coinText.text = missionData.reward.ToString();
        missionText.text = missionData.text;
        progressBar.value = Mathf.InverseLerp(0, missionData.count, curentValue);
        progressText.text = string.Format(progressFormat, curentValue, missionData.count);
    }

    public void ChangeProgress(int goalValue, int curentValue)
    {
        progressBar.value = Mathf.InverseLerp(0, goalValue, curentValue);
        progressText.text = string.Format(progressFormat, curentValue, 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);
    }
}