local FirstRechargeUI = class("FirstRechargeUI", BaseUI)

local BTN_LIGHT = {
	"act_firstcharge_btn_1_1",
	"act_firstcharge_btn_2_1",
	"act_firstcharge_btn_3_1",
}

local BTN_NORMAL = {
	"act_firstcharge_btn_1",
	"act_firstcharge_btn_2",
	"act_firstcharge_btn_3",
}

function FirstRechargeUI:isFullScreen()
	return false
end

function FirstRechargeUI:getPrefabPath()
	return "assets/prefabs/ui/shop/first_recharge_ui.prefab"
end

function FirstRechargeUI:onPressBackspace()
	self:closeUI()
end

function FirstRechargeUI:onClose()
	EventManager:dispatchEvent(EventManager.CUSTOM_EVENT.MAIN_UI_CHECK_POP)
end

function FirstRechargeUI:ctor(params)
	self:initData()
end

function FirstRechargeUI:initData()
	if DataManager.ShopData:isFirstRechargeOpen(GConst.ShopConst.FIRST_RECHARGE_GEARS.GEAR_1) then
		self.curGear = GConst.ShopConst.FIRST_RECHARGE_GEARS.GEAR_1
	elseif DataManager.ShopData:isFirstRechargeOpen(GConst.ShopConst.FIRST_RECHARGE_GEARS.GEAR_2) then
		self.curGear = GConst.ShopConst.FIRST_RECHARGE_GEARS.GEAR_2
	else
		self.curGear = GConst.ShopConst.FIRST_RECHARGE_GEARS.GEAR_3
	end
end

function FirstRechargeUI:onLoadRootComplete()
	local uiMap = self.root:genAllChildren()
	self.mask = uiMap["first_recharge_ui.mask"]
	self.btnClose = uiMap["first_recharge_ui.btn_close"]
	self.txTitle = uiMap["first_recharge_ui.tx_title"]
	self.spineBanner = uiMap["first_recharge_ui.spine_banner"]
	self.txTips = uiMap["first_recharge_ui.tips.tx_tips"]
	self.btnGet = uiMap["first_recharge_ui.btn_get"]
	self.txGet = uiMap["first_recharge_ui.btn_get.tx_desc"]
	self.txGet1 = uiMap["first_recharge_ui.btn_get.tx_desc_1"]

	self.matchImg = uiMap["first_recharge_ui.match_img"]
	self.nameTx = uiMap["first_recharge_ui.match_img.name_tx"]
	self.qltBg = uiMap["first_recharge_ui.match_img.qlt_bg"]
	self.qltBgTx = uiMap["first_recharge_ui.match_img.qlt_bg.qlt_tx"]

	self.daysBg = {}
	self.daysTitle = {}
	self.daysRewardCells = {}
	for i = 1, 3 do
		table.insert(self.daysBg, uiMap["first_recharge_ui.rewards.day_" .. i])
		table.insert(self.daysTitle, uiMap["first_recharge_ui.rewards.day_" .. i .. ".tx_day"])
		local rewardCells = {}
		for j = 1, 3 do
			table.insert(rewardCells, uiMap["first_recharge_ui.rewards.day_" .. i .. ".reward_cell_" .. j]:addLuaComponent(GConst.TYPEOF_LUA_CLASS.REWARD_CELL))
		end
		self.daysRewardCells[i] = rewardCells
	end
	self.tagNode = uiMap["first_recharge_ui.tag"]
	self.btnTags = {}
	self.txTags = {}
	for k, key in pairs(GConst.ShopConst.FIRST_RECHARGE_GEARS) do
		self.btnTags[key] = uiMap["first_recharge_ui.tag.btn_" .. key]
		self.txTags[key] = uiMap["first_recharge_ui.tag.btn_" .. key .. ".tx_desc"]
	end

	DataManager.ShopData:setFirstrechargePoped()
	-- 上报
	BIReport:postPayUIShow(BIReport.GIFT_TYPE.FIRST_RECHARGE, DataManager.ShopData:getFirstRechargeGiftId(self.curGear))
	self:postChangePage()

	for gear, btn in pairs(self.btnTags) do
		btn:addClickListener(function()
			self.curGear = gear
			self:onRefresh()
			-- 上报
			BIReport:postPayUIShow(BIReport.GIFT_TYPE.FIRST_RECHARGE, DataManager.ShopData:getFirstRechargeGiftId(self.curGear))
			self:postChangePage()
		end)
	end
	self.mask:addClickListener(function()
		self:closeUI()
	end)
	self.btnClose:addClickListener(function()
		self:closeUI()
	end)
	self.btnGet:addClickListener(function()
		local boughtDay = DataManager.ShopData:getFirstRechargeBoughtDays(self.curGear)
		local rewardDay = DataManager.ShopData:getFirstRechargeRewardDays(self.curGear)
		if boughtDay > 0 then
			-- 已购买
			if boughtDay > rewardDay then
				ModuleManager.ShopManager:onBuyFirstRecharge(self.curGear, rewardDay + 1)
			end
		else
			-- 未购买
			ModuleManager.ShopManager:onBuyFirstRecharge(self.curGear, rewardDay + 1)
		end
	end)
	self:bind(DataManager.PaymentData, "isDirty", function()
		if not DataManager.ShopData:isFirstRechargeOpen(self.curGear) then
			self:initData()
		end
		self:onRefresh()
	end)
	self:addEventListener(EventManager.CUSTOM_EVENT.CROSS_DAY, function()
		self:onRefresh()
	end)
end

function FirstRechargeUI:postChangePage()
	if self.curGear then
		BIReportV2:postOperation(BIReportV2.OPERATION_UI_NAME["FIRST_RECHARGE_PAGE_" .. self.curGear])
	end
end

function FirstRechargeUI:onRefresh()
	if not DataManager.ShopData:isFirstRechargeOpen() then
		self:closeUI()
	end

	local cfg = DataManager.ShopData:getActGiftConfig(DataManager.ShopData:getFirstRechargeGiftId(self.curGear))
	local price = GFunc.getFormatPrice(cfg.recharge_id)

	self.txTitle:setText(I18N:getGlobalText(I18N.GlobalConst["FIRST_CHARGE_TITLE_" .. self.curGear]))

	local tips
	local spineName
	tips = I18N:getGlobalText(I18N.GlobalConst["FIRST_CHARGE_TIPS_" .. self.curGear], price)
	spineName = "ui_act_first_" .. self.curGear
	self.txTips:setText(tips)

	self.spineBanner:setActive(false)
	self.spineBanner:loadAssetAsync(spineName, function()
		self.spineBanner:setActive(true)
		self.spineBanner:setLocalScale(1, 1)
		self.spineBanner:playAnim("idle", true, true, true)
	end)

	-- 档位标签
	local openCount = DataManager.ShopData:getFirstRechargeOpenCount()
	for gear, btn in pairs(self.btnTags) do
		if openCount > 1 and DataManager.ShopData:isFirstRechargeOpen(gear) then
			btn:setActive(true)
			local cfg = DataManager.ShopData:getActGiftConfig(DataManager.ShopData:getFirstRechargeGiftId(gear))
			-- btn:setSprite(GConst.ATLAS_PATH.UI_FIRST_CHARGE, gear == self.curGear and ("act_firstcharge_bg_2") or ("act_firstcharge_bg_3"))
			-- self.txTags[gear]:setText("<color=" .. (gear == self.curGear and "#A1591A" or "#C5EAFF")  .. ">" .. I18N:getGlobalText(I18N.GlobalConst.FIRST_CHARGE_2, GFunc.getFormatPrice(cfg.recharge_id)) .. "</color>")
			btn:setSprite(GConst.ATLAS_PATH.UI_FIRST_CHARGE, gear == self.curGear and BTN_LIGHT[gear] or BTN_NORMAL[gear])
			self.txTags[gear]:setText(I18N:getGlobalText(I18N.GlobalConst.FIRST_CHARGE_2, GFunc.getFormatPrice(cfg.recharge_id)))
			local boughtDay = DataManager.ShopData:getFirstRechargeBoughtDays(gear)
			local rewardDay = DataManager.ShopData:getFirstRechargeRewardDays(gear)
			if boughtDay > 0 and boughtDay > rewardDay then
				btn:addRedPoint(90, 30, 0.9)
			else
				btn:removeRedPoint()
			end
		else
			btn:setActive(false)
		end
	end

	local boughtDay = DataManager.ShopData:getFirstRechargeBoughtDays(self.curGear)
	local rewardDay = DataManager.ShopData:getFirstRechargeRewardDays(self.curGear)

	local heroId
	-- 奖励相关
	for day, bg in ipairs(self.daysBg) do
		local giftId = DataManager.ShopData:getFirstRechargeGiftId(self.curGear, day)
		local cfg = DataManager.ShopData:getActGiftConfig(giftId)
		local rewards = cfg.reward
		local received = DataManager.ShopData:isBoughtFirstRecharge(self.curGear, day)
		local canGet = DataManager.ShopData:canGetFirstRechargeReward(self.curGear, day)
		for i, cell in ipairs(self.daysRewardCells[day]) do
			if rewards and rewards[i] then
				if not heroId then
					heroId = rewards[i].id
				end
				cell:setActive(true)
				cell:refreshByConfig(rewards[i], received, received)
				if canGet then
					cell:showFrameAnimation()
				else
					cell:hideFrameAnimation()
				end
			else
				cell:setActive(false)
			end
		end
		self.daysTitle[day]:setText(I18N:getGlobalText(I18N.GlobalConst.FIRST_CHARGE_1, day))
	end
	if heroId and self.curGear ~= 3 then
		self.matchImg:setActive(true)
		local heroEntity = DataManager.HeroData:getEntity({id = heroId, level = 0})
		local matchType = heroEntity:getMatchType()
		local qlt = heroEntity:getQlt()
		self.matchImg:setSprite(GConst.ATLAS_PATH.ICON_HERO, ModuleManager.HeroManager:getMatchTypeIcon(matchType))
		self.qltBg:setSprite(GFunc.getHeroQltImg(qlt))
		self.qltBgTx:setText(GFunc.getHeroQltStr(qlt))
		self.nameTx:setText(heroEntity:getName())
	else
		self.matchImg:setActive(false)
	end

	-- 购买领取按钮
	if boughtDay > 0 then
		-- 已购买
		if boughtDay > rewardDay then
			self.txGet:setActive(false)
			self.txGet1:setActive(true)
			self.txGet1:setText(I18N:getGlobalText(I18N.GlobalConst.BTN_CLAIM))
			self.btnGet:setSprite(GConst.ATLAS_PATH.COMMON, "common_btn_yellow_3")
			self.btnGet:setTouchEnable(true)
			self.btnGet:addRedPoint(114, 38, 1)
		else
			self.txGet:setActive(false)
			self.txGet1:setActive(true)
			self.txGet1:setText(I18N:getGlobalText(I18N.GlobalConst.FIRST_CHARGE_3))
			self.btnGet:setSprite(GConst.ATLAS_PATH.COMMON, "common_btn_grey_3")
			self.btnGet:setTouchEnable(false)
			self.btnGet:removeRedPoint()
		end
	else
		-- 未购买
		self.txGet:setActive(true)
		self.txGet1:setActive(false)
		self.txGet:setText(price)
		self.btnGet:setSprite(GConst.ATLAS_PATH.COMMON, "common_btn_yellow_3")
		self.btnGet:setTouchEnable(true)
		self.btnGet:removeRedPoint()
	end
end

return FirstRechargeUI