local SummonMainUI = class("SummonMainUI", BaseUI)

-- function SummonMainUI:getPreLoadList()
-- 	return {
-- 		[GConst.TYPEOF_UNITY_CLASS.GAME_OBJECT] = {
-- 			"assets/prefabs/effects/ui/vfx_b13_ui_chouka_bg_b02.prefab",
-- 			"assets/prefabs/effects/ui/vfx_b13_ui_chouka_bg_b03.prefab",
-- 		}
-- 	}
-- end

function SummonMainUI:ctor(params)
	self.page = params.summonId or 1
	self._bgVfxMap = {}
end

function SummonMainUI:getPrefabPath()
	return "assets/prefabs/ui/summon/summon_main_ui.prefab"
end

function SummonMainUI:getCurrencyParams()
	local ItemConst = require "app/module/item/item_const"
	local params = {}
	params.showType = GConst.CURRENCY_TYPE.HORIZONTAL
	if self.page == 1 then
		params.itemIds = {
			ItemConst.ITEM_ID_GOLD,
			ItemConst.ITEM_ID_GEM,
			ItemConst.ITEM_ID_SUMMON_1,
		}
	else
		params.itemIds = {
			ItemConst.ITEM_ID_GOLD,
			ItemConst.ITEM_ID_GEM,
			ItemConst.ITEM_ID_SUMMON_2,
		}
	end
	return params
end

function SummonMainUI:onClose()
	if self._bgVfxMap then
		for k, v in pairs(self._bgVfxMap) do
			v:destroy()
		end
		self._bgVfxMap = nil
	end
	-- EventManager:dispatchEvent(EventManager.CUSTOM_EVENT.MAIN_UI_CHECK_POP)
end

function SummonMainUI:onLoadRootComplete()
	local uiMap = self.root:genAllChildren()
	uiMap["summon_main_ui.bottom.close_btn"]:addClickListener(function()
		self:closeUI()
	end)
	self.btnSummonFree = uiMap["summon_main_ui.node.btn_summon_free"]
	self.btnSummonFreeTx = uiMap["summon_main_ui.node.btn_summon_free.text_free"]
	self.btnSummonAd = uiMap["summon_main_ui.node.btn_summon_ad"]
	self.btnSummonAdTx = uiMap["summon_main_ui.node.btn_summon_ad.desc_tx"]
	--两个抽奖按钮
	self.btnSummonOne = uiMap["summon_main_ui.node.btn_summon_one"]
	self.txSummonOneName = uiMap["summon_main_ui.node.btn_summon_one.text"]
	self.imgSummonOneIcon = uiMap["summon_main_ui.node.btn_summon_one.cost.img_icon_1"]
	self.txNumOne = uiMap["summon_main_ui.node.btn_summon_one.cost.tx_num_one"]

	self.btnSummonTen = uiMap["summon_main_ui.node.btn_summon_ten"]
	self.txSummonTenName = uiMap["summon_main_ui.node.btn_summon_ten.text"]
	self.imgSummonTenIcon = uiMap["summon_main_ui.node.btn_summon_ten.cost.img_icon_1"]
	self.txNumTen = uiMap["summon_main_ui.node.btn_summon_ten.cost.tx_num_ten"]

	self.txFreeTime = uiMap["summon_main_ui.node.tx_free_time"]

	self.btnWish = uiMap["summon_main_ui.node.btn_wish"] --心愿
	self.btnWishTx = uiMap["summon_main_ui.node.btn_wish.unlock.tx_guarantee"] --心愿
	self.btnWishIcon = uiMap["summon_main_ui.node.btn_wish.unlock.img_wish_icon"] --心愿
	self.btnWishAddImg = uiMap["summon_main_ui.node.btn_wish.add_img"]

	-- self.toggleJump = uiMap["summon_main_ui.jump_tween"]:getComponent(GConst.TYPEOF_UNITY_CLASS.UI_TOGGLE)
	-- self.isJumpTween = self.toggleJump.isOn
	-- self.toggleJump.onValueChanged:AddListener(function(isOn) self.isJumpTween = isOn end)
	self.touchNode = uiMap["summon_main_ui.node.touch_node"]
	self.touchNode:setActive(false)
	-- uiMap["summon_main_ui.jump_tween"]:setActive(false)

	-- self.vfx_b13_ui_chouka_bg_b01 = uiMap["summon_main_ui.vfx_b13_ui_chouka_bg_b01"]
	-- self.wishMask = uiMap["summon_main_ui.node.btn_wish.unlock.mask"]
	-- self.wishQlt = uiMap["summon_main_ui.node.btn_wish.unlock.qlt"]
	-- self.wishMaskSlider = uiMap["summon_main_ui.node.btn_wish.unlock.mask"]:getComponent(GConst.TYPEOF_UNITY_CLASS.UI_IMAGE)

	-- self.descTx = uiMap["summon_main_ui.btn_wish.desc_tx"]
	-- self.descTx = uiMap["summon_main_ui.btn_info.desc_tx"]
	-- self.descTx = uiMap["summon_main_ui.jump_tween.desc_tx"]
	self.btnSummonFree:addClickListener(function()
		self:onSummon(1)
	end)
	self.btnSummonOne:addClickListener(function()
		self:onSummon(1)
	end)
	self.btnSummonTen:addClickListener(function()
		self:onSummon(10)
	end)
	self.btnSummonAd:addClickListener(function()
		SDKManager:showFullScreenAds(BIReport.ADS_CLICK_TYPE.SUMMON_AD, function()
			self:onSummon(1, true)
		end)
	end)

	uiMap["summon_main_ui.node.btn_info"]:addClickListener(function()
		ModuleManager.SummonManager:showSummonOddsUI(self.page)
	end)
	self.txTitle = uiMap["summon_main_ui.node.tx_title"]
	
	self.summonBg = uiMap['summon_main_ui.bg']
	uiMap["summon_main_ui.node.btn_summon_one.text_one"]:setText(I18N:getGlobalText(I18N.GlobalConst.SUMMON_ONE))
	uiMap["summon_main_ui.node.btn_summon_ten.text_ten"]:setText(I18N:getGlobalText(I18N.GlobalConst.SUMMON_TEN))
	uiMap["summon_main_ui.node.btn_summon_free.text_free"]:setText(I18N:getGlobalText(I18N.GlobalConst.FREE_DESC_1))
	uiMap["summon_main_ui.node.btn_summon_ad.desc_tx"]:setText(I18N:getGlobalText(I18N.GlobalConst.SUMMON_DESC_1))
	uiMap["summon_main_ui.node.btn_info.desc_tx"]:setText(I18N:getGlobalText(I18N.GlobalConst.SUMMON_FORCE_3))
	self.descBg = uiMap["summon_main_ui.node.desc_bg"]
	self.descTx = uiMap["summon_main_ui.node.desc_bg.desc_tx"]
	-- self.spineHero = {}
	-- for i = 1, 3 do
	-- 	self.spineHero[i] = uiMap['summon_main_ui.ui_spine_obj_'..i]
	-- 	if i ~= 1 then
	-- 		self.spineHero[i]:setDefaultMix(0)
	-- 	end
	-- end
	-- self.heroTips = {}
	-- for i = 1, 2 do
	-- 	self.heroTips[i] = {}
	-- 	self.heroTips[i].tips = uiMap['summon_main_ui.node.hero_'..i]
	-- 	self.heroTips[i].txName = uiMap['summon_main_ui.node.hero_'..i..'.tx_hero_name']
	-- 	self.heroTips[i].txDesc = uiMap['summon_main_ui.node.hero_'..i..'.tx_hero_desc']
	-- end
	self.btnTxs = {
		I18N:getGlobalText(I18N.GlobalConst.SUMMON_DESC_6),
		I18N:getGlobalText(I18N.GlobalConst.SUMMON_DESC_7),
	}
	self.pageBtns = {}
	self.pageBtnTxs = {}
	for i = 1, 2 do
		self.pageBtns[i] = uiMap['summon_main_ui.bottom.btn_'.. i]
		self.pageBtnTxs[i] = uiMap['summon_main_ui.bottom.btn_'.. i ..'.tx_desc']
		self.pageBtnTxs[i]:setText(self.btnTxs[i])
		self.pageBtns[i]:addClickListener(function()
			if self.page == i then
				return
			end
			self.page = i
			self:onRefresh()
			UIManager:updateBarsState(self)
		end)
	end

	-------------抽奖相关---------
	self:addEventListener(EventManager.CUSTOM_EVENT.FORCE_SUMMON, function(result, newForce)
		self:refreshWishBtn()
		self:onSummonRsp(result, newForce)
	end)
	self:addEventListener(EventManager.CUSTOM_EVENT.FORCE_SUMMON_WISH_CLAIM, function(result, newForce)
		self:refreshWishBtn()
		ModuleManager.SummonManager:showSummonRewardUI({
			rewards = result.rewards,
			isWish = true,
			newForce = newForce,
			page = self.page
		})
	end)

	self:addEventListener(EventManager.CUSTOM_EVENT.FORCE_SUMMON_WISH_HERO_ID, function()
		if self:isClosed() then
			return
		end
		self:refreshWishBtn()
	end)

	self.btnWish:addClickListener(function()
		if self.isUnlock then
			local wishHeroId = DataManager.SummonData:getSummonWishHeroId(self.page)
			local wishGuarantee = DataManager.SummonData:getSummonWishGuarantee2(self.page)
			local wishCount = DataManager.SummonData:getSummonWishCount(self.page)
			if wishCount >= wishGuarantee and wishHeroId ~= 0 then
				--发送领取奖励的协议
				ModuleManager.SummonManager:onSummonWishClaimReq(self.page)
			else
				--打开设置心愿界面
				ModuleManager.SummonManager:showSummonWishUI(self.page)
			end
		else
			GFunc.showToast(I18N:getGlobalText(I18N.GlobalConst.SUMMON_WISH_LOCK))
		end
	end)
	self:bind(DataManager.SummonData, "isDirty", function()
		self:onRefresh()
	end)
	-- self:bind(DataManager.ActTimeData, "isDirty", function()
	-- 	self:onRefresh()
	-- end)

	self:scheduleGlobal(function()
		self:updateTime()
	end, 1)
	self:updateTime()
	
end

--每秒刷新单抽红点
function SummonMainUI:updateTime()
	local isFree = DataManager.SummonData:hasSummonFree(self.page)
	if self.isFree ~= isFree then
		self.isFree = isFree
		self:onRefresh()
	end
	if not self.isFree then
		self.remain_time = Time:getTodaySurplusTime()
		self.txFreeTime:setText(I18N:getGlobalText(I18N.GlobalConst.SUMMON_FREE_TIME, Time:formatNumTime(self.remain_time)))
	else
		self.txFreeTime:setText("")
	end
end

--刷新
function SummonMainUI:onRefresh()
	self:refreshSummonBtn()
	self:refreshWishBtn()
	-- self:refreshBtnRedPoint()
	self:refreshPageBtn()
	self:updateTime()
end

function SummonMainUI:refreshPageBtn()
	for i = 1, 2 do
		if self.page == i then
			self.pageBtns[i]:setSprite(GConst.ATLAS_PATH.ACT_COMMON, "act_common_btn_" .. i .."_1")
			-- self.pageBtnTxs[i]:setText(self.btnTxs[i])
		else
			self.pageBtns[i]:setSprite(GConst.ATLAS_PATH.ACT_COMMON, "act_common_btn_" .. i .."_2")
			-- self.pageBtnTxs[i]:setText("<color=#72778C>" .. self.btnTxs[i] .. "</color>")
		end
	end
	-- for i, v in ipairs(self.pageBtns) do
	-- 	if i <= table.nums(summonList) then
	-- 		local isRedPoint = false
	-- 		if i == 1 then
	-- 			if DataManager.SummonData:hasSummonFree(1) or GFunc.checkCost(GConst.ItemConst.ITEM_ID_QLT_5_FRAGMENT, 1, false) then
	-- 				isRedPoint = true
	-- 			end
	-- 		else
	-- 			local itemCost = DataManager.SummonData:getSummonItemCost(summonList[i])
	-- 			if itemCost then
	-- 				local costId = GFunc.getRewardId(itemCost)
	-- 				local costNum = GFunc.getRewardNum(itemCost)
	-- 				if GFunc.checkCost(costId, costNum, false) then
	-- 					isRedPoint = true
	-- 				end
	-- 			end
	-- 		end
	-- 		if isRedPoint then
	-- 			v.btn:addRedPoint(33, 33, 1)
	-- 		else
	-- 			v.btn:removeRedPoint()
	-- 		end
	-- 	else
	-- 		v.btn:setActive(false)
	-- 	end
	-- end
end

--刷新抽奖按钮
function SummonMainUI:refreshSummonBtn()
	local freeCount = DataManager.SummonData:getSummonFreeCount(self.page)
	local adCount = DataManager.SummonData:getSummonAdCount(self.page)
	self.btnSummonAd:setActive(adCount > 0)
	self.btnSummonFree:setActive(freeCount > 0)
	self.btnSummonOne:setActive(freeCount <= 0)
	-- self.txFreeTime:setActive(not isFree and not DataManager.SummonData:getIsActivity())

	local itemCost = DataManager.SummonData:getSummonItemCost(self.page)
	local costId = itemCost.id
	-- local costNum = itemCost.num
	local hadNum = DataManager.BagData.ItemData:getItemNumById(costId)
	-- local obj = DataManager.BagData.ItemData:getItemById(costId)
	if freeCount <= 0 then
		if hadNum >= 1 then
			self.txNumOne:setText(1)
			self.btnSummonOne:addRedPoint(124, 32, 1)
			self.imgSummonOneIcon:setSprite(ModuleManager.ItemManager:getItemIcon(costId))
		else
			self.btnSummonOne:removeRedPoint()
			local cost = DataManager.SummonData:getSummonGemCost(self.page)
			if cost and hadNum == 0 and GFunc.checkCost(GConst.ItemConst.ITEM_ID_GEM, cost.num, false) then
				self.txNumOne:setText(cost.num)
				self.imgSummonOneIcon:setSprite(ModuleManager.ItemManager:getItemIcon(GConst.ItemConst.ITEM_ID_GEM))
				-- obj = DataManager.BagData.ItemData:getItemById(GConst.ItemConst.ITEM_ID_GEM)
			else
				self.txNumOne:setText("<color=#EC3636>1</color>")
				self.imgSummonOneIcon:setSprite(ModuleManager.ItemManager:getItemIcon(costId))
			end
		end
	end

	if hadNum >= 10 then
		self.txNumTen:setText(10)
		self.imgSummonTenIcon:setSprite(ModuleManager.ItemManager:getItemIcon(costId))
		self.btnSummonTen:addRedPoint(124, 32, 1)
	else
		self.btnSummonTen:removeRedPoint()
		local cost = DataManager.SummonData:getSummonGemCost(self.page)
		if cost and hadNum == 0 and GFunc.checkCost(GConst.ItemConst.ITEM_ID_GEM, cost.num * 10, false) then
			self.txNumTen:setText(cost.num * 10)
			self.imgSummonTenIcon:setSprite(ModuleManager.ItemManager:getItemIcon(GConst.ItemConst.ITEM_ID_GEM))
			-- obj = DataManager.BagData.ItemData:getItemById(GConst.ItemConst.ITEM_ID_GEM)
		else
			self.txNumTen:setText("<color=#EC3636>10</color>")
			self.imgSummonTenIcon:setSprite(ModuleManager.ItemManager:getItemIcon(costId))
		end
	end
end

--刷新心愿按钮
function SummonMainUI:refreshWishBtn()
	local guarantee1 = DataManager.SummonData:getSummonConfig(self.page).guarantee1
	if not guarantee1 then
		self.descBg:setActive(false)
	else
		self.descBg:setActive(true)
		local count = guarantee1 - DataManager.SummonData:getSummonTriggerCount(self.page)
		self.descTx:setText(I18N:getGlobalText(I18N.GlobalConst.SUMMON_FORCE_4, count))
	end
	local love = DataManager.SummonData:getSummonWishConfig(self.page)
	if not love then
		self.btnWish:setActive(false)
		return
	end
	self.btnWish:setActive(true)
	local summonCount = DataManager.SummonData:getSummonCount(self.page)
	local unlockCount = DataManager.SummonData:getSummonWishUnlock(self.page)
	self.isUnlock = summonCount >= unlockCount
	-- self.txUnlock:setText(summonCount .. "/" .. unlockCount .. I18N:getGlobalText(I18N.GlobalConst.MAGIC_BOOK_DESC_4))
	local wishCount = DataManager.SummonData:getSummonWishCount(self.page)
	local needCount = DataManager.SummonData:getSummonWishGuarantee2(self.page)
	self.btnWishTx:setText(wishCount .. "/" .. needCount)

	local wishHeroId = DataManager.SummonData:getSummonWishHeroId(self.page)
	local cfg = DataManager.HeroData:getHeroConfig(wishHeroId)
	if cfg then
		self.btnWishIcon:setSprite(GFunc.getHeroIcon(cfg.icon))
		self.btnWish:setSprite(GConst.ATLAS_PATH.ICON_ITEM, GConst.FRAME_QLT[cfg.qlt])
		self.btnWishAddImg:setActive(false)
	else
		self.btnWishIcon:setSprite(GConst.ATLAS_PATH.COMMON, "common_alpha")
		-- self.btnWish:setSprite(GConst.ATLAS_PATH.ICON_ITEM, "frame_1")
		self.btnWishAddImg:setActive(true)
	end
end

--检查单抽红点
function SummonMainUI:refreshBtnRedPoint()
	if DataManager.SummonData:hasSummonFree(self.page) then
		self.btnSummonFree:addRedPoint(124, 33, 1)
	else
		self.btnSummonFree:removeRedPoint()
	end
end

function SummonMainUI:onSummon(count, isAd)
	if isAd then
		AudioManager:playEffect(AudioManager.EFFECT_ID.UI_SUMMON_START)
		ModuleManager.SummonManager:onForceSummonReq(count, 1, self.page)
	else
		local freeCount = DataManager.SummonData:getSummonFreeCount(self.page)
		if freeCount > 0 and count == 1 then
			ModuleManager.SummonManager:onForceSummonReq(count, 2, self.page)
			return
		end
		local costs = DataManager.SummonData:getSummonCosts(self.page, count)
		local hasGemCost = false
		for i, cost in ipairs(costs) do
			if cost.id == GConst.ItemConst.ITEM_ID_GEM then
				hasGemCost = true
			end
		end
		if hasGemCost then
			local params = {}
			local itemCost = DataManager.SummonData:getSummonItemCost(self.page)
			local costId = itemCost.id
			local costNum = (count - DataManager.BagData.ItemData:getItemNumById(costId))
			local gemCost = DataManager.SummonData:getSummonGemCost(self.page)
			local costGemNum = gemCost.num * costNum
			params.content = I18N:getGlobalText(I18N.GlobalConst.ITEM_NOT_ENOUGH_DESC, costGemNum, costNum)
			params.boxType = GConst.MESSAGE_BOX_TYPE.MB_OK_CANCEL
			params.showToday = GConst.MESSAGE_BOX_SHOW_TODAY.SUMMON_FORCE
			params.okFunc = function()
				for i, cost in ipairs(costs) do
					if not GFunc.checkCost(cost.id, GFunc.getRewardNum(cost), true) then
						return
					end
				end
				AudioManager:playEffect(AudioManager.EFFECT_ID.UI_SUMMON_START)
				ModuleManager.SummonManager:onForceSummonReq(count, 0, self.page)
			end
			GFunc.showMessageBox(params)
			return
		else
			for i, cost in ipairs(costs) do
				if not GFunc.checkCost(cost.id, GFunc.getRewardNum(cost), true) then
					return
				end
			end
		end
		AudioManager:playEffect(AudioManager.EFFECT_ID.UI_SUMMON_START)
		ModuleManager.SummonManager:onForceSummonReq(count, 0, self.page)
	end
end

function SummonMainUI:onSummonRsp(result, newForce)
	self:refreshSummonBtn()
	self.summonResult = result
	if self.isJumpTween or self.selectSpineId ~= 1 then
		ModuleManager.SummonManager:showSummonRewardUI({
			rewards = self.summonResult.rewards,
			callback = function(count, isAd)
				self:onSummon(count, isAd)
			end,
			newForce = newForce,
			page = self.page
		})
	else
		self:disableTouch()
		self.selectSpine:playAnimComplete("open01", false, true, function()
			self.selectSpine:playAnim("idle02", true, false)
		end, false, 0.4, function()
			ModuleManager.SummonManager:showSummonRewardUI({
				rewards = self.summonResult.rewards,
				callback = function(count)
					self:onSummon(count)
				end,
				tweenEnd = function()
					self:enableTouch()
					self.selectSpine:playAnim("idle01", true, false)
				end,
				newForce = newForce,
				page = self.page
			})
		end)
	end
end

function SummonMainUI:disableTouch()
	self.touchNode:setActive(true)
end

function SummonMainUI:enableTouch()
	self.touchNode:setActive(false)
end

return SummonMainUI
