local CollectionUI = class("CollectionUI", BaseUI)

function CollectionUI:isFullScreen()
	return true
end

function CollectionUI:showCommonBG()
	return false
end

function CollectionUI:getPrefabPath()
	return "assets/prefabs/ui/collection/collection_ui.prefab"
end

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

function CollectionUI:ctor(params)
	self.collectType = params
end

function CollectionUI:onCover()
end

function CollectionUI:onReshow()
end

function CollectionUI:onClose()
	if self.animFly then
		self.animFly:Kill()
		self.animFly = nil
	end
	if self.showRewardNodeSid then
		self:unscheduleGlobal(self.showRewardNodeSid)
	end
end

function CollectionUI:onLoadRootComplete()
	local uiMap = self.root:genAllChildren()

	self.txTitle = uiMap["collect_ui.tx_title"]
	self.btnHelp = uiMap["collect_ui.tx_title.btn_help"]
	self.btnClose = uiMap["collect_ui.banner.btn_close"]
	self.scrollrectComp = uiMap["collect_ui.scrollrect"]:addLuaComponent(GConst.TYPEOF_LUA_CLASS.SCROLL_RECT_BASE)
	self.rewardsNode = uiMap["collection_ui.rewards"]
	self.txTotalValue = uiMap["collect_ui.rewards.btn_rewards.tx_total"]
	self.btnGet = uiMap["collection_ui.rewards.btn_get"]
	self.imgGet = uiMap["collection_ui.rewards.btn_get.img_icon"]
	self.txGet = uiMap["collection_ui.rewards.btn_get.tx_get"]
	self.txDesc = uiMap["collect_ui.rewards.tx_desc"]
	self.txStage = uiMap["collect_ui.rewards.tx_stage"]
	self.imgProg = uiMap["collect_ui.rewards.prog.img_prog"]:getComponent(GConst.TYPEOF_UNITY_CLASS.BF_SLIDER)
	self.btnRewards = uiMap["collection_ui.rewards.point"]
	self.flyIcon = uiMap["collection_ui.fly_icon"]
	-- 英雄
	self.btnHero = uiMap["collection_ui.banner.btn_hero"]
	self.selectHero = uiMap["collection_ui.banner.btn_hero.select"]
	self.txHero = uiMap["collection_ui.banner.btn_hero.tx_hero"]
	-- 皮肤
	self.btnSkin = uiMap["collection_ui.banner.btn_skin"]
	self.selectSkin = uiMap["collection_ui.banner.btn_skin.select"]
	self.txSkin = uiMap["collection_ui.banner.btn_skin.tx_skin"]

	self.txHero:setText(I18N:getGlobalText(I18N.GlobalConst.MAIN_BTN_2))
	self.txSkin:setText(I18N:getGlobalText(I18N.GlobalConst.SKIN))
	self.scrollrectComp:addInitCallback(function()
		return "app/ui/collection/cell/collection_cell"
	end)
	self.scrollrectComp:addRefreshCallback(function(index, cell)
		cell:refresh(self.collectType, self.collectList[index])
	end)

    self.btnRewards:addClickListener(function()
		UIManager:showUI("app/ui/collection/collection_reward_ui", self.collectType)
	end)
	self.btnGet:addClickListener(function()
		ModuleManager.CollectionManager:reqPointReward()
	end)
	self.btnClose:addClickListener(function()
		self:closeUI()
	end)
	self.btnHelp:addClickListener(function()
		local params = {
            type = GConst.TipsConst.HELP_TIPS_TYPE.ARENA,
            title = I18N:getGlobalText(I18N.GlobalConst.COLLECTION_DESC_11),
            desc = I18N:getGlobalText(I18N.GlobalConst.COLLECTION_DESC_7),
        }
        ModuleManager.TipsManager:showHelpTips(params)
	end)
	self.btnHero:addClickListener(function()
		self:showHeroCollect()
	end)
	self.btnSkin:addClickListener(function()
		self:showSkinCollect()
	end)
	self:bind(DataManager.CollectionData, "isDirty", function()
        self:refreshCollectPoint()
		self:showFlyAnim()
    end)
	self:addEventListener(EventManager.CUSTOM_EVENT.COLLECTION_CLICK_GET_POINT, function(pos)
        self.flyFromPos = pos
    end)
end

function CollectionUI:onRefresh()
	if self.collectType == GConst.CollectionConst.TYPE.HERO then
		self:showHeroCollect()
	elseif self.collectType == GConst.CollectionConst.TYPE.SKIN then
		self:showSkinCollect()
	end
	self:showCollectList()
	self:refreshCollectPoint()
end

function CollectionUI:showCollectList()
	self.collectList = DataManager.CollectionData:getCollectList(self.collectType)
	self.scrollrectComp:clearCells()
	self.scrollrectComp:refillCells(#self.collectList)
	self.flyIcon:setActive(false)
end

-- 刷新收集点数状态
function CollectionUI:refreshCollectPoint()
	local showId = DataManager.CollectionData:getCanGetOrCollectingTargetId()
	-- Logger.logHighlight("展示档位："..showId)
	if showId then
		local target = DataManager.CollectionData:getTargetPoint(showId)
		local owned = DataManager.CollectionData:getTargetOwnedPoint(showId)
		self.txStage:setText(owned.."/"..target)
		self.imgProg.value = owned / target
		if DataManager.CollectionData:isMeetTargetPoint(showId) then
			-- 可领取
			self.txGet:setText(I18N:getGlobalText(I18N.GlobalConst.BTN_CLAIM))
			self.txDesc:setText(I18N:getGlobalText(I18N.GlobalConst.COLLECTION_DESC_9))
			self.imgGet:setSprite(GConst.ATLAS_PATH.HERO, "hero_dec_7")
			self.btnGet:setSprite(GConst.ATLAS_PATH.COMMON, "common_btn_green_2")
			self.btnGet:setTouchEnable(true)
		else
			-- 进行中
			self.txGet:setText(I18N:getGlobalText(I18N.GlobalConst.COLLECTION_DESC_3))
			self.txDesc:setText(I18N:getGlobalText(I18N.GlobalConst.COLLECTION_DESC_4, target - owned))
			self.imgGet:setSprite(GConst.ATLAS_PATH.HERO, "hero_dec_8")
			self.btnGet:setSprite(GConst.ATLAS_PATH.COMMON, "common_btn_grey_2")
			self.btnGet:setTouchEnable(false)
		end
	else
		-- 已全部领完所有目标
		self.txGet:setText(I18N:getGlobalText(I18N.GlobalConst.BTN_CLAIM))
		self.txDesc:setText(I18N:getGlobalText(I18N.GlobalConst.COLLECTION_DESC_8))
		self.btnGet:setSprite(GConst.ATLAS_PATH.COMMON, "common_btn_grey_2")
		self.btnGet:setTouchEnable(false)
	end

	local total = DataManager.CollectionData:getTotalCollectPoint()
	local cur = DataManager.CollectionData:getCurCollectPoint()
	self.txTotalValue:setText(cur.."/"..total)

	GFunc.centerImgAndTx(self.imgGet, self.txGet, 5)

	-- 红点
	if DataManager.CollectionData:hasCanCollectPoint(GConst.CollectionConst.TYPE.HERO) then
		self.btnHero:addRedPoint(25, 30, 0.6)
	else
		self.btnHero:removeRedPoint()
	end
	if DataManager.CollectionData:hasCanCollectPoint(GConst.CollectionConst.TYPE.SKIN) then
		self.btnSkin:addRedPoint(25, 30, 0.6)
	else
		self.btnSkin:removeRedPoint()
	end
end

function CollectionUI:showFlyAnim()
	if not self.flyFromPos then
		return
	end

	self.flyIcon:setLocalScale(0.4, 0.4, 0.4)
	self.flyIcon:setPosition(self.flyFromPos.x, self.flyFromPos.y, self.flyFromPos.z)
	self.flyIcon:setActive(true)
	if self.animFly then
		self.animFly:Kill()
		self.animFly = nil
	end
	self.animFly = self.root:createBindTweenSequence()
	self.animFly:Insert(0, self.flyIcon:getTransform():DOScale(0.8, 0.2))
	self.animFly:Insert(0.2, self.flyIcon:getTransform():DOMove(self.btnRewards:getPosition(), 1))
	self.animFly:Insert(0.4, self.flyIcon:getTransform():DOScale(0.4, 0.4))
	self.animFly:OnComplete(function()
		self.flyIcon:setActive(false)
		self.flyFromPos = nil
		self.animFly = nil
	end)
end

-- 展示英雄收集
function CollectionUI:showHeroCollect()
	self.collectType = GConst.CollectionConst.TYPE.HERO

	self.btnHelp:setActive(true)
	self.selectHero:setActive(true)
	self.selectSkin:setActive(false)
	self.txTitle:setText(I18N:getGlobalText(I18N.GlobalConst.COLLECTION_DESC_1))
	self:showCollectList()
end

-- 展示皮肤收集
function CollectionUI:showSkinCollect()
	self.collectType = GConst.CollectionConst.TYPE.SKIN

	self.btnHelp:setActive(false)
	self.selectHero:setActive(false)
	self.selectSkin:setActive(true)
	self.txTitle:setText(I18N:getGlobalText(I18N.GlobalConst.SKIN_COLLECT))
	self:showCollectList()
end

return CollectionUI