using System.Collections;
using UnityEngine;
using UI.ActivityAFuGift;
using ET;
using FairyGUI;
using System;
using UI.CommonGame;
using static GFGGame.ShopSProxy;
namespace GFGGame
{
public class ActivityAfuGiftView : BaseWindow
{
private UI_ActivityAFuGiftUI _ui;
private GList _rewardListUI;
private struct GetState
{
public int rewardID;
public int dayIndex;
public bool canGet;
public bool isVip;
}
public override void Dispose()
{
base.Dispose();
}
protected override void OnInit()
{
base.OnInit();
packageName = UI_ActivityAFuGiftUI.PACKAGE_NAME;
_ui = UI_ActivityAFuGiftUI.Create();
viewCom = _ui.target;
viewCom.Center();
modal = true;
viewAnimationType = EnumViewAnimationType.ZOOM_CENTER;
_ui.m_btnClose.onClick.Add(Hide);
_ui.m_btnGet.onClick.Add(OnClickBtnGet);
// Data
ActivityAfuGiftDataManager.Instance.InitActivityInfo();
// UI
_rewardListUI = _ui.m_panel.m_scrollPane.m_list;
_rewardListUI.itemRenderer = RenderListReward;
_rewardListUI.numItems = ActivityAfuGiftDataManager.Instance.RewardCfgs.Count;
InitUI();
}
protected override void OnShown()
{
base.OnShown();
_ui.m_panel.m_scrollPane.target.scrollPane.posX = 0;
UpdateTime(null);
Timers.inst.Add(1, 0, UpdateTime);
}
protected override void OnHide()
{
base.OnHide();
Timers.inst.Remove(UpdateTime);
}
protected override void AddEventListener()
{
base.AddEventListener();
EventAgent.AddEventListener(ConstMessage.RED_CHANGE, UpdateState);
}
protected override void RemoveEventListener()
{
base.RemoveEventListener();
EventAgent.RemoveEventListener(ConstMessage.RED_CHANGE, UpdateState);
}
private void InitUI()
{
_ui.m_c1.SetSelectedIndex(ActivityAfuGiftDataManager.Instance.GetIsBuy() ? 1 : 0);
// 使列表宽度等于子节点的总宽度
float itemWidth = _rewardListUI.GetChildAt(0).width;
float columnGap = _rewardListUI.columnGap;
_rewardListUI.width = CalculateListWidth(_rewardListUI.numItems, itemWidth, columnGap);
Timers.inst.StartCoroutine(InitProgressWidth());
}
private IEnumerator InitProgressWidth()
{
yield return new WaitForSeconds(0.5f);
// 根据列表 初始化进度条长度
float marginLeft = 149;
GProgressBar gProgressBar = _ui.m_panel.m_scrollPane.m_progress;
gProgressBar.width = marginLeft + _rewardListUI.GetChildAt(_rewardListUI.numChildren - 1).position.x - _rewardListUI.GetChildAt(0).position.x;
}
private float CalculateListWidth(int num, float itemWidth, float columnGap)
{
return num * itemWidth + (num - 1) * columnGap;
}
private void UpdateTime(object param)
{
long curTime = TimeHelper.ServerNow();
long endTime = ActivityAfuGiftDataManager.Instance.GetEndTime();
_ui.m_txtTime.text = TimeUtil.FormattingTimeTo_DDHHmm(endTime - curTime);
}
private void RenderListReward(int index, GObject item)
{
UI_reward rewardUI = UI_reward.Proxy(item);
rewardUI.m_day.m_day.SetVar("day", (index + 1).ToString()).FlushVars();
// 已经到达的日期
int passDay = ActivityAfuGiftDataManager.Instance.GetMaxPassDay();
int arrive = (index + 1) <= passDay ? 0 : 1;
rewardUI.m_day.m_light.SetSelectedIndex(arrive);
int itemNormalId = ActivityAfuGiftDataManager.Instance.RewardCfgs[index].bonusFreeArr[0][0];
int itemVipId1 = ActivityAfuGiftDataManager.Instance.RewardCfgs[index].bonusPayArr[0][0];
int itemVipId2 = ActivityAfuGiftDataManager.Instance.RewardCfgs[index].bonusPayArr[1][0];
LoadItem(itemNormalId, rewardUI.m_reward_normal, ActivityAfuGiftDataManager.Instance.RewardCfgs[index].bonusFreeArr[0][1]);
LoadItem(itemVipId1, rewardUI.m_reward_vip1, ActivityAfuGiftDataManager.Instance.RewardCfgs[index].bonusPayArr[0][1], true);
LoadItem(itemVipId2, rewardUI.m_reward_vip2, ActivityAfuGiftDataManager.Instance.RewardCfgs[index].bonusPayArr[1][1], true);
UI_reward.ProxyEnd();
}
private void LoadItem(int id, GObject obj, int num, bool isVip = false)
{
ItemCfg itemNormal = ItemCfgArray.Instance.GetCfg(id);
UI_ComItem comItem = UI_ComItem.Proxy(obj);
comItem.m_loaIcon.url = ResPathUtil.GetIconPath(itemNormal);
comItem.m_txtCount.text = num.ToString();
comItem.m_QualityType.selectedIndex = itemNormal.rarity - 1;
comItem.target.onClick.Add(OnItemClick);
int isLock = 0;
if (isVip)
{
isLock = ActivityAfuGiftDataManager.Instance.GetIsBuy() ? 0 : 1;
}
comItem.m_Lock.selectedIndex = isLock;
int dayIndex = _rewardListUI.GetChildIndex(obj) + 1;
// 已经到达的日期
int passDay = ActivityAfuGiftDataManager.Instance.GetMaxPassDay();
bool arrive = dayIndex <= passDay;
comItem.target.data = new GetState
{
rewardID = id,
canGet = arrive,
dayIndex = dayIndex,
isVip = isVip
};
if (arrive)
{
ActivityInfo activityInfo = ActivityAfuGiftDataManager.Instance.Info;
bool canGet = false;
// Vip专属
if (isVip)
{
canGet = ActivityAfuGiftDataManager.Instance.CanGetSuperReward(dayIndex);
}
else
{
canGet = ActivityAfuGiftDataManager.Instance.CanGetNormalReward(dayIndex);
}
// 已领
if (!canGet)
{
//bonus.m_imgGot.visible = true;
obj.touchable = false;
// 隐藏红点
RedDotController.Instance.SetComRedDot(obj.asCom, false);
}
// 未领
else
{
//bonus.m_imgGot.visible = false;
// 显示红点
RedDotController.Instance.SetComRedDot(obj.asCom, true);
}
}
UI_ComItem.ProxyEnd();
}
private void OnItemClick(EventContext eventContext)
{
GObject obj = (GObject)eventContext.sender;
GetState getState = (GetState)(obj.data);
if (getState.canGet)
{
// Vip奖励同时领取两个
if (getState.isVip)
{
int rewardID1 = ActivityAfuGiftDataManager.Instance.RewardCfgs[getState.dayIndex - 1].bonusPayArr[0][1];
int rewardID2 = ActivityAfuGiftDataManager.Instance.RewardCfgs[getState.dayIndex - 1].bonusPayArr[1][1];
}
else
{
int ID = ActivityAfuGiftDataManager.Instance.RewardCfgs[getState.dayIndex - 1].bonusFreeArr[0][1];
}
//ActivityGlobalSProxy.ReqGetActivityBonus(_activityInfo.ActivityId, getState.rewardID).Coroutine();
}
else
{
GoodsItemTipsController.ShowItemTips(getState.rewardID);
}
}
///
/// 领取所有奖励按钮
///
private void OnClickBtnGet()
{
if (!ActivityAfuGiftDataManager.Instance.GetIsBuy())
{
// 跳转充值画面
ReqShopBuy(990005).Coroutine();
return;
}
// 一键领取
int passDay = ActivityAfuGiftDataManager.Instance.GetMaxPassDay();
for(int i = passDay - 1; i >= 0; i++)
{
int itemNormalId = ActivityAfuGiftDataManager.Instance.RewardCfgs[i].bonusFreeArr[0][0];
int itemVipId1 = ActivityAfuGiftDataManager.Instance.RewardCfgs[i].bonusPayArr[0][0];
int itemVipId2 = ActivityAfuGiftDataManager.Instance.RewardCfgs[i].bonusPayArr[1][0];
if (ActivityAfuGiftDataManager.Instance.CanGetNormalReward(itemNormalId))
{
}
if (ActivityAfuGiftDataManager.Instance.CanGetSuperReward(itemVipId1))
{
}
if (ActivityAfuGiftDataManager.Instance.CanGetSuperReward(itemVipId2))
{
}
}
}
///
/// 充值,领取东西后调用
///
private void UpdateState()
{
// 更新底部按钮
_ui.m_c1.SetSelectedIndex(ActivityAfuGiftDataManager.Instance.GetIsBuy() ? 1 : 0);
// 更新锁 红点状态
_rewardListUI.numItems = ActivityAfuGiftDataManager.Instance.RewardCfgs.Count;
}
}
}