using System; 
 | 
using Cysharp.Threading.Tasks; 
 | 
using UnityEngine; 
 | 
using UnityEngine.UI; 
 | 
  
 | 
  
 | 
/// <summary> 
 | 
/// 淘金刷新界面 
 | 
/// </summary> 
 | 
public class GoldRushRefreshWin : UIBase 
 | 
{ 
 | 
    [SerializeField] ItemCell itemCell; 
 | 
    [SerializeField] Text nameText; 
 | 
    [SerializeField] CountControler countControler; 
 | 
  
 | 
    [SerializeField] Text timeText; 
 | 
    [SerializeField] Text moneyText; 
 | 
    [SerializeField] Image iconImg; 
 | 
  
 | 
    [SerializeField] Transform refreshRect; 
 | 
    [SerializeField] ButtonEx refreshBtn; 
 | 
    [SerializeField] ButtonEx workBtn; 
 | 
    [SerializeField] ButtonEx callBackBtn; 
 | 
    [SerializeField] Text callBackText; 
 | 
    [SerializeField] Text workingText; 
 | 
  
 | 
    GoldRushItemConfig config; 
 | 
    int workerCount; 
 | 
  
 | 
    protected override void InitComponent() 
 | 
    { 
 | 
        workBtn.AddListener(DoingWork); 
 | 
        refreshBtn.AddListener(RefreshItem); 
 | 
        callBackBtn.AddListener(CallBackWorker); 
 | 
    } 
 | 
  
 | 
  
 | 
  
 | 
    protected override void OnPreOpen() 
 | 
    { 
 | 
        GoldRushManager.Instance.OnGoldRushCampEvent += OnGoldRushCampEvent; 
 | 
        GlobalTimeEvent.Instance.secondEvent += OnSecondEvent; 
 | 
        var goldID = GoldRushManager.Instance.GetCampGoldID(GoldRushManager.Instance.selectCampID); 
 | 
        if (goldID == 0) 
 | 
        { 
 | 
            DelayCloseWindow().Forget(); 
 | 
            return; 
 | 
        } 
 | 
         
 | 
  
 | 
        Display(); 
 | 
    } 
 | 
  
 | 
    protected override void OnPreClose() 
 | 
    { 
 | 
        GoldRushManager.Instance.OnGoldRushCampEvent -= OnGoldRushCampEvent; 
 | 
        GlobalTimeEvent.Instance.secondEvent -= OnSecondEvent; 
 | 
    } 
 | 
  
 | 
  
 | 
    void Display() 
 | 
    { 
 | 
        var goldID = GoldRushManager.Instance.GetCampGoldID(GoldRushManager.Instance.selectCampID); 
 | 
        config = GoldRushItemConfig.Get(goldID); 
 | 
        int emptyCnt = GoldRushManager.Instance.GetEmptyWorkerCount(); 
 | 
  
 | 
        int itemID = config.ItemID; 
 | 
        itemCell.Init(new ItemCellModel(itemID, false, config.ItemCount)); 
 | 
        itemCell.button.AddListener(()=> 
 | 
        { 
 | 
            ItemTipUtility.Show(itemID); 
 | 
        }); 
 | 
        nameText.text = GoldRushManager.Instance.GetCampItemName(config); 
 | 
        var endTime = GoldRushManager.Instance.GetCampEndTime(GoldRushManager.Instance.selectCampID); 
 | 
        if (endTime == 0) 
 | 
        { 
 | 
            //未开始 
 | 
            timeText.text = TimeUtility.SecondsToMS(config.NeedSeconds); 
 | 
            refreshRect.SetActive(true); 
 | 
            callBackBtn.SetActive(false); 
 | 
  
 | 
            iconImg.SetIconWithMoneyType(GoldRushManager.Instance.refreshMoneyType); 
 | 
            moneyText.text = UIHelper.ShowUseMoney(GoldRushManager.Instance.refreshMoneyType, GetRefreshMoney()); 
 | 
            workBtn.SetInteractable(emptyCnt != 0); 
 | 
            workerCount = Math.Min(emptyCnt, 1); 
 | 
        } 
 | 
        else 
 | 
        { 
 | 
            timeText.text = TimeUtility.SecondsToMS(endTime - TimeUtility.AllSeconds); 
 | 
            refreshRect.SetActive(false); 
 | 
            callBackBtn.SetActive(true); 
 | 
            RefreshCallBackBtn(); 
 | 
            workerCount = GoldRushManager.Instance.GetCampWorkerCnt(GoldRushManager.Instance.selectCampID); 
 | 
        } 
 | 
        countControler.Init(ChangeWorkerCount, config.WorkerMax, workerCount, AddWorker, DecWorker); 
 | 
    } 
 | 
  
 | 
    void RefreshCallBackBtn() 
 | 
    { 
 | 
        var realCnt = GoldRushManager.Instance.GetCampWorkerCnt(GoldRushManager.Instance.selectCampID); 
 | 
        if (workerCount == 0) 
 | 
        { 
 | 
            callBackBtn.SetInteractable(true); 
 | 
            callBackText.text = Language.Get("GoldRush33"); //撤回监工 
 | 
        } 
 | 
        else if (realCnt == workerCount) 
 | 
        { 
 | 
            callBackBtn.SetInteractable(false); 
 | 
            callBackText.text = Language.Get("GoldRush14"); //调整监工 
 | 
        } 
 | 
        else 
 | 
        {  
 | 
            callBackBtn.SetInteractable(true); 
 | 
            callBackText.text = Language.Get("GoldRush14"); //调整监工 
 | 
        } 
 | 
    } 
 | 
  
 | 
    void OnSecondEvent() 
 | 
    {  
 | 
        var endTime = GoldRushManager.Instance.GetCampEndTime(GoldRushManager.Instance.selectCampID); 
 | 
        if (endTime != 0) 
 | 
        { 
 | 
            timeText.text = TimeUtility.SecondsToMS(endTime - TimeUtility.AllSeconds); 
 | 
  
 | 
            var addStr = new string('.', (int)Time.time % 4); 
 | 
            workingText.text = Language.Get("GoldRush37") + addStr; 
 | 
        } 
 | 
    } 
 | 
  
 | 
    int GetRefreshMoney() 
 | 
    { 
 | 
        var refreshCnt = GoldRushManager.Instance.GetCampRefreshCnt(GoldRushManager.Instance.selectCampID); 
 | 
        return GoldRushManager.Instance.refreshMoneyList[Math.Min(refreshCnt, GoldRushManager.Instance.refreshMoneyList.Length - 1)]; 
 | 
    } 
 | 
  
 | 
    void OnGoldRushCampEvent(int campID) 
 | 
    { 
 | 
        if (campID != GoldRushManager.Instance.selectCampID) 
 | 
        { 
 | 
            return; 
 | 
        } 
 | 
        var goldID = GoldRushManager.Instance.GetCampGoldID(GoldRushManager.Instance.selectCampID); 
 | 
        if (goldID == 0) 
 | 
        { 
 | 
            //已完成 
 | 
            CloseWindow(); 
 | 
            return; 
 | 
        } 
 | 
        Display(); 
 | 
    } 
 | 
  
 | 
    void ChangeWorkerCount(int count) 
 | 
    { 
 | 
        workerCount = count; 
 | 
        var endTime = GoldRushManager.Instance.GetCampEndTime(GoldRushManager.Instance.selectCampID); 
 | 
        if (endTime == 0) 
 | 
        { 
 | 
            //未开始 
 | 
            timeText.text = TimeUtility.SecondsToMS(config.NeedSeconds / Math.Max(1, count)); 
 | 
        } 
 | 
        else 
 | 
        { 
 | 
            timeText.text = TimeUtility.SecondsToMS(endTime - TimeUtility.AllSeconds); 
 | 
            RefreshCallBackBtn(); 
 | 
        } 
 | 
    } 
 | 
  
 | 
    bool AddWorker(int count) 
 | 
    { 
 | 
        if (count >= config.WorkerMax) 
 | 
        { 
 | 
            return false; 
 | 
        } 
 | 
  
 | 
        //可派遣的监工: 空闲监工数+当前监工数 
 | 
        if (count + 1 > GoldRushManager.Instance.GetEmptyWorkerCount() + GoldRushManager.Instance.GetCampWorkerCnt(GoldRushManager.Instance.selectCampID)) 
 | 
        {  
 | 
            SysNotifyMgr.Instance.ShowTip("GoldRush2"); 
 | 
            return false; 
 | 
        } 
 | 
  
 | 
        return true; 
 | 
    } 
 | 
  
 | 
    bool DecWorker(int count) 
 | 
    { 
 | 
        if (count <= 0) 
 | 
            return false; 
 | 
        return true; 
 | 
    } 
 | 
  
 | 
    void DoingWork() 
 | 
    { 
 | 
        var endTime = GoldRushManager.Instance.GetCampEndTime(GoldRushManager.Instance.selectCampID); 
 | 
        if (endTime != 0) 
 | 
            return; 
 | 
  
 | 
        int emptyCnt = GoldRushManager.Instance.GetEmptyWorkerCount(); 
 | 
        if (emptyCnt == 0 || workerCount > emptyCnt) 
 | 
        { 
 | 
            SysNotifyMgr.Instance.ShowTip("GoldRush2"); 
 | 
            return; 
 | 
        } 
 | 
  
 | 
        if (workerCount == 0) 
 | 
        { 
 | 
            SysNotifyMgr.Instance.ShowTip("GoldRush5"); 
 | 
            return; 
 | 
        } 
 | 
  
 | 
        if (workerCount > config.WorkerMax) 
 | 
        { 
 | 
            return; 
 | 
        } 
 | 
  
 | 
        //仓库容量已达上限 
 | 
        if (GoldRushManager.Instance.GetWarehouseCnt() >= GoldRushManager.Instance.warehouseMaxCnt) 
 | 
        { 
 | 
            SysNotifyMgr.Instance.ShowTip("GoldRush3"); 
 | 
            return; 
 | 
        } 
 | 
  
 | 
        GoldRushManager.Instance.SendGoldRushOP(2, GoldRushManager.Instance.selectCampID, workerCount); 
 | 
        SysNotifyMgr.Instance.ShowTip("GoldRush6"); 
 | 
        CloseWindow(); 
 | 
    } 
 | 
  
 | 
    void RefreshItem() 
 | 
    { 
 | 
        var endTime = GoldRushManager.Instance.GetCampEndTime(GoldRushManager.Instance.selectCampID); 
 | 
        if (endTime != 0) 
 | 
            return; 
 | 
  
 | 
  
 | 
        if (!UIHelper.CheckMoneyCount(GoldRushManager.Instance.refreshMoneyType, GetRefreshMoney(), 2)) 
 | 
        { 
 | 
            return; 
 | 
        } 
 | 
  
 | 
        GoldRushManager.Instance.SendGoldRushOP(1, GoldRushManager.Instance.selectCampID, 0); 
 | 
  
 | 
        SysNotifyMgr.Instance.ShowTip("GoldRush4"); 
 | 
    } 
 | 
  
 | 
    void CallBackWorker() 
 | 
    { 
 | 
         var endTime = GoldRushManager.Instance.GetCampEndTime(GoldRushManager.Instance.selectCampID); 
 | 
        if (endTime == 0) 
 | 
            return; 
 | 
  
 | 
        var realCnt = GoldRushManager.Instance.GetCampWorkerCnt(GoldRushManager.Instance.selectCampID); 
 | 
        if (workerCount == 0) 
 | 
        { 
 | 
            ConfirmCancel.ShowPopConfirm(Language.Get("Mail101"), 
 | 
            Language.Get("GoldRush38"), (bool isOK) => 
 | 
                { 
 | 
                    if (isOK) 
 | 
                    { 
 | 
                        //撤回监工 
 | 
                        GoldRushManager.Instance.SendGoldRushOP(3, GoldRushManager.Instance.selectCampID, 0); 
 | 
                        CloseWindow(); 
 | 
                    } 
 | 
                }); 
 | 
        } 
 | 
        else if (realCnt == workerCount) 
 | 
        { 
 | 
            return; 
 | 
        } 
 | 
        else 
 | 
        { 
 | 
            //调整监工 
 | 
            GoldRushManager.Instance.SendGoldRushOP(2, GoldRushManager.Instance.selectCampID, workerCount); 
 | 
            SysNotifyMgr.Instance.ShowTip("GoldRush1"); 
 | 
            CloseWindow(); 
 | 
        } 
 | 
    } 
 | 
} 
 |