using asap.core;
using DG.Tweening;
using DG.Tweening.Core;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
using UniRx;
using System;
using System.Threading.Tasks;
public class EventCanProgressTaskView : MonoBehaviour
{
[SerializeField] private Image progressBar;
[SerializeField] private TMP_Text textProgress;
[SerializeField] private RewardItemNew reward;
private EventCanProgressTaskInfo _info;
private IEventAggregator _eventAggregator;
///
/// Prevents multiple task updates
///
private bool _isUpdatingTask = false;
public void Init(EventCanProgressTaskInfo info, IEventAggregator ea)
{
float p = info.CurrentProgress / (float)info.TotalProgressRequired;
progressBar.fillAmount = p;
textProgress.text = $"{info.CurrentProgress}/{info.TotalProgressRequired}";
reward.SetData(info.Reward);
_info = info.DeepCopy();
_eventAggregator = ea;
var model = GContext.container.Resolve();
_eventAggregator.GetEvent()
.Subscribe(_ => UpdateProgressTask(model.ProgressTaskInfo))
.AddTo(this);
}
public async void UpdateProgressTask(EventCanProgressTaskInfo newInfo)
{
try
{
if (_isUpdatingTask)
return;
string[] tokens;
int currentProgress = 0;
int totalProgressRequired = 0;
TweenerCore tweenText;
TweenerCore tweenBar;
Sequence sequence;
if (_info.TaskIdx != newInfo.TaskIdx)
{
_isUpdatingTask = true;
tokens = textProgress.text.Split('/');
currentProgress = int.Parse(tokens[0]);
totalProgressRequired = int.Parse(tokens[1]);
tweenText = DOTween.To(() => currentProgress, v => currentProgress = v, totalProgressRequired, 0.5f)
.OnUpdate(() => textProgress.text = $"{currentProgress}/{totalProgressRequired}");
tweenBar = progressBar.DOFillAmount(1, 0.5f);
sequence = DOTween.Sequence();
sequence.Join(tweenText).Join(tweenBar);
await sequence.AsyncWaitForCompletion();
reward.SetReceived(true);
// _eventAggregator.Publish(new EventCanTaskRewardClaimEvent(_info.Reward, reward.gameObject.transform.position));
var request = new EventRewardFlyStashRequest(_info.Reward, reward.icon.GetComponent());
GContext.Publish(request);
await Task.Delay(TimeSpan.FromSeconds(1f));
progressBar.fillAmount = 0;
await Awaiters.NextFrame;
_info = newInfo.DeepCopy();
reward.SetData(newInfo.Reward);
textProgress.text = $"0/{newInfo.TotalProgressRequired}";
_isUpdatingTask = false;
}
tokens = textProgress.text.Split('/');
currentProgress = int.Parse(tokens[0]);
totalProgressRequired = newInfo.TotalProgressRequired;
tweenText = DOTween.To(() => currentProgress, v => currentProgress = v, newInfo.CurrentProgress, 0.5f)
.OnUpdate(() => textProgress.text = $"{currentProgress}/{totalProgressRequired}");
tweenBar = progressBar.DOFillAmount(newInfo.CurrentProgress / (float)newInfo.TotalProgressRequired, 0.5f);
sequence = DOTween.Sequence();
sequence.Join(tweenText).Join(tweenBar);
await sequence.AsyncWaitForCompletion();
}
catch (Exception e)
{
Debug.LogError($"[EventCan] {e.Message}\n{e.StackTrace}");
}
}
}