using System.Collections;
using System.Collections.Generic;
using Cysharp.Threading.Tasks;
using UnityEngine;
using UnityEngine.UI;
///
/// 内政 - 基本用于淘金
///
public class AffairBaseWin : UIBase
{
[SerializeField] Button bagBtn;
//淘金相关
[SerializeField] GameObject fullGoldRush;
[SerializeField] Image goldRushIcon;
[SerializeField] Text goldRushCountText;
[SerializeField] Button goldRushItemBtn;
[SerializeField] Image goldRushItemProcess;
[SerializeField] Text autoText;
[SerializeField] GameObject flowAutoEffect;
[SerializeField] Button autoBtn;
//NPC对话相关
[SerializeField] HeroSkinModel[] funcNPCs;
[SerializeField] Transform[] talkRects;
[SerializeField] Text[] talkTexts;
protected override void InitComponent()
{
bagBtn.AddListener(() =>
{
UIManager.Instance.OpenWindow();
}
);
goldRushIcon.SetIconWithMoneyType(52);
goldRushItemBtn.AddListener(() =>
{
ItemTipUtility.ShowMoneyTip(52, false);
}
);
autoBtn.AddListener(() =>
{
if (FuncOpen.Instance.IsFuncOpen((int)FuncOpenEnum.GoldRush, true))
{
UIManager.Instance.OpenWindow();
}
}
);
}
protected override void OnPreOpen()
{
GoldRushManager.Instance.OnGoldRushInfoEvent += OnGoldRushInfoEvent;
GoldRushManager.Instance.OnAutoWorkingEvent += OnAutoWorkingEvent;
GlobalTimeEvent.Instance.secondEvent += OnSecondEvent;
PlayerDatas.Instance.playerDataRefreshEvent += PlayerDataRefreshEvent;
GlobalTimeEvent.Instance.fiveSecondEvent += OnFiveSecondEvent;
Display();
if (GoldRushManager.Instance.openAutoGoldRush)
{
GoldRushManager.Instance.GetAllAward();
}
}
protected override void OnPreClose()
{
GoldRushManager.Instance.OnGoldRushInfoEvent -= OnGoldRushInfoEvent;
GoldRushManager.Instance.OnAutoWorkingEvent -= OnAutoWorkingEvent;
GlobalTimeEvent.Instance.secondEvent -= OnSecondEvent;
PlayerDatas.Instance.playerDataRefreshEvent -= PlayerDataRefreshEvent;
GlobalTimeEvent.Instance.fiveSecondEvent -= OnFiveSecondEvent;
}
protected override void OnOpen()
{
GoldRushManager.Instance.ResumeAutoWorking();
}
void Display()
{
fullGoldRush.SetActive(GoldRushManager.Instance.GetFinishGoldRushCount() >= GoldRushManager.Instance.warehouseMaxCnt);
RefreshGoldRushMoney();
autoText.text = Language.Get(GoldRushManager.Instance.isAutoWorking ? "GoldRush34" : "GoldRush24");
if (FuncOpen.Instance.IsFuncOpen((int)FuncOpenEnum.GoldRush))
{
autoBtn.SetActive(true);
goldRushItemBtn.SetActive(true);
}
else
{
autoBtn.SetActive(false);
goldRushItemBtn.SetActive(false);
}
}
void OnGoldRushInfoEvent()
{
fullGoldRush.SetActive(GoldRushManager.Instance.GetFinishGoldRushCount() >= GoldRushManager.Instance.warehouseMaxCnt);
}
void OnSecondEvent()
{
RefreshGoldRushMoney();
}
void OnAutoWorkingEvent()
{
if (GoldRushManager.Instance.isAutoWorking)
{
autoText.text = Language.Get("GoldRush34");
flowAutoEffect.SetActive(true);
}
else
{
autoText.text = Language.Get("GoldRush24");
flowAutoEffect.SetActive(false);
}
}
void RefreshGoldRushMoney()
{
var count = UIHelper.GetMoneyCnt(52);
if (count > 0)
{
goldRushCountText.text = count + "/" + GoldRushManager.Instance.goldRushMissionMaxCnt;
}
else
{
//倒计时
goldRushCountText.text = TimeUtility.SecondsToMS(GoldRushManager.Instance.restoreMissionSeconds - (TimeUtility.AllSeconds - GoldRushManager.Instance.lastRecoverTime));
}
goldRushItemProcess.fillAmount = (float)count / GoldRushManager.Instance.goldRushMissionMaxCnt;
}
void PlayerDataRefreshEvent(PlayerDataType type)
{
if (type == PlayerDataType.GoldRush)
{
RefreshGoldRushMoney();
}
}
void OnFiveSecondEvent()
{
var index = FuncNPCManager.Instance.GetRandomAffairNpcTalk();
var talk = FuncNPCManager.Instance.GetAffairTalk(funcNPCs[index].heroSkinID);
if (talk != null)
{
talkTexts[index].text = Language.Get(talk);
talkRects[index].SetActive(true);
}
var npc = funcNPCs[index].GetModel();
npc.PlayAnimation("hanhua", true);
Talk(index).Forget();
}
async UniTask Talk(int index)
{
await UniTask.Delay(5000);
talkRects[index].SetActive(false);
var npc = funcNPCs[index].GetModel();
npc.PlayAnimation("idle", true);
}
}