using System.Collections.Generic;
using TMPro;
using UnityEngine;
using System;
using UnityEngine.UI;
using UniRx;
using asap.core;
using game;
using GameCore;
public class EventGatherCoreEntranceBtn : EventButtonResource
{
[SerializeField] private TMP_Text textTimer;
[SerializeField] private Button button;
[SerializeField] private Image icon;
private ILoadResourceService _loadResourceService;
///
/// This is sloppy work. Need to refactor.
///
private EventBingoEntranceData _data;
private TimeSpan RemainingTime => _data.ExpiryTime - ZZTimeHelper.UtcNow();
private bool IsActive => _data.ExpiryTime > ZZTimeHelper.UtcNow() && ZZTimeHelper.UtcNow() > _data.StartTime;
private const string RedPointKey = "event_gather_core.entrance";
private void Awake()
{
var model = GContext.container.Resolve();
if (model == null)
{
gameObject.SetActive(false);
return;
}
_data = model.ToEntranceData();
UpdateTimer();
Observable.Interval(TimeSpan.FromSeconds(1f)).Subscribe(UpdateTimer).AddTo(this);
_loadResourceService = GContext.container.Resolve();
button.onClick.AddListener(EnterActAsync);
RedPointManager.Instance.SetRedPointState(RedPointKey, _data.NeedRedPoint, _data.RedPointIndex);
CheckResource(new List() { UITypes.EventGatherCorePanel.Path, EventGatherCoreAct.ActAddressable, _data.Icon });
}
private void UpdateTimer(long _ = 0L)
{
textTimer.text = ConvertTools.ConvertTime2(RemainingTime);
if (!IsActive)
gameObject.SetActive(false);
}
private async void EnterActAsync()
{
try
{
bool isReady = await _loadResourceService.Loads(
new List() { UITypes.EventGatherCorePanel.Path, EventGatherCoreAct.ActAddressable });
if (isReady)
{
GContext.Publish(new UnloadActToNextAct { actId = EventGatherCoreAct.ActAddressable, TransitionPanel = UITypes.CloudTransitionPanel });
}
else
{
var panel = await UIManager.Instance.ShowUI(UITypes.CloudTransitionPanel);
panel.GetComponent()
.SetBtn(true, () => GContext.Publish(new UnloadActToNextAct(EventGatherCoreAct.ActAddressable)));
}
}
catch (Exception e)
{
Debug.Log($"[EventGatherCore] EnterActError: {e.Message}\n{e.StackTrace}");
throw;
}
}
protected override void OnLoadEventResource()
{
if (IsActive)
{
GContext.container.Resolve().SetImageSprite(icon, _data.Icon);
gameObject.SetActive(true);
}
}
}