local RewardCell = class("RewardCell", BaseCell)

local EFFECT_LIGHT = "assets/prefabs/effects/ui/vfx_ui_arena_gift_b02.prefab"

function RewardCell:init()
	local uiMap = self:getUIMap()
	self.content = uiMap["reward_cell.content"]
	self.icon = uiMap["reward_cell.item_bg.icon"]
	self.frameBg = uiMap["reward_cell.item_bg"]
	self.mask = uiMap["reward_cell.item_bg.mask"]
	self.check = uiMap["reward_cell.check"]
	self.numTx = uiMap["reward_cell.item_bg.num"]
	self.fragment = uiMap["reward_cell.item_bg.fragment"]
	self.rightUpIcon = uiMap["reward_cell.item_bg.right_up_icon"]
	self.sImg = uiMap["reward_cell.item_bg.s"]
	self.matchImg = uiMap["reward_cell.item_bg.match_img"]
	self.frameAni = uiMap["reward_cell.frame_ani"]
	self.lock = uiMap["reward_cell.lock"]
	-- 首通
	self.firstPass = uiMap["reward_cell.first"]
	self.firstPassTx = uiMap["reward_cell.first.tx_first"]
	-- 皮肤
	self.skin = uiMap["reward_cell.skin"]
	self.skinQlt = uiMap["reward_cell.skin.img_qlt"]
	-- 特效
	self.rootEffect = uiMap["reward_cell.root_effect"]
	-- 选中框
	self.select = uiMap["reward_cell.content.item_bg.select"]
	-- 自选
	self.choose = uiMap["reward_cell.choose"]

	self:hideFrameAnimation()
	self.baseObject:addClickListener(function()
		if self.clickCallback then
			self.clickCallback()
		elseif self.rewardId ~= nil then
			ModuleManager.TipsManager:showRewardTips(self.rewardId, self.rewardType, self.baseObject)
		end
	end)
end

function RewardCell:refresh(reward)
	self:showMask(false, false)
	if reward.type == GConst.REWARD_TYPE.ITEM then
		local info = ConfigManager:getConfig("item")[reward.item.id]
		if info == nil then
			return
		end
		self.rewardId = reward.item.id
		self.rewardType = reward.type
		self:_refreshItem(info, reward.item.count)
	else
		self.rewardId = nil
	end
	self:showFirstPass(false)
end

-- 展示自选
function RewardCell:refreshByChoose()
	self.choose:setVisible(true)
	self.content:setVisible(false)
end

function RewardCell:refreshByConfig(reward, mask, check)
	self:showMask(mask, check)
	if reward.type == GConst.REWARD_TYPE.ITEM then
		local info = ConfigManager:getConfig("item")[reward.id]
		if info == nil then
			return
		end
		self.rewardId = reward.id
		self.rewardType = reward.type
		self:_refreshItem(info, reward.num)
	else
		self.rewardId = nil
	end
	self:showFirstPass(false)
end

function RewardCell:refreshItemById(itemId, count, mask, check)
	self:showMask(mask, check)
	local info = ConfigManager:getConfig("item")[itemId]
	if info == nil then
		return
	end
	self.rewardId = itemId
	self.rewardType = GConst.REWARD_TYPE.ITEM
	self:_refreshItem(info, count)
	self:showFirstPass(false)
end

function RewardCell:_refreshItem(info, count)
	self.choose:setVisible(false)
	self.select:setActive(false)
	self.lock:setVisible(false)
	self.content:setVisible(true)
	self.numTx:setVisible(true)
	self.frameBg:setSprite(GConst.ATLAS_PATH.ICON_ITEM, GConst.FRAME_QLT[info.qlt])
	if count and count > 0 then
		if count > 100 then
			self.numTx:setText(GFunc.num2Str(count))
		else
			self.numTx:setText(GFunc.intToString(count))
		end
	else
		self.numTx:setText(GConst.EMPTY_STRING)
	end
	if info.type == GConst.ItemConst.ITEM_TYPE.HERO_FRAGMENT then
		-- 英雄碎片道具
		self.skin:setVisible(false)
		self.fragment:setVisible(true)
		local heroInfo = ConfigManager:getConfig("hero")[info.parameter]
		if heroInfo then
			self.icon:setSprite(GConst.ATLAS_PATH.ICON_HERO, heroInfo.icon)
			self.sImg:setVisible(heroInfo.qlt >= 4)
			self.matchImg:setVisible(true)
			self.matchImg:setSprite(GConst.ATLAS_PATH.ICON_HERO, GConst.HeroConst.MATCH_ICON_NAME[heroInfo.position])
		else
			self.icon:setSprite(GConst.ATLAS_PATH.COMMON, "common_alpha")
			self.sImg:setVisible(false)
			self.matchImg:setVisible(false)
		end
	elseif info.type == GConst.ItemConst.ITEM_TYPE.SKIN then
		-- 皮肤道具
		self.skin:setVisible(true)
		self.matchImg:setVisible(false)
		self.fragment:setVisible(false)
		local skinInfo = ConfigManager:getConfig("skin")[info.parameter]
		if skinInfo then
			self.skin:setVisible(true)
			self.sImg:setVisible(skinInfo.qlt >= 4)
			self.icon:setSprite(GConst.ATLAS_PATH.ICON_HERO, skinInfo.icon)
			self.skin:setSprite(GConst.ATLAS_PATH.ICON_HERO, "frame_dec_" .. skinInfo.qlt)
			self.skinQlt:setSprite(GConst.ATLAS_PATH.HERO, "hero_skin_" .. skinInfo.qlt)
			self.frameBg:setSprite(GConst.ATLAS_PATH.ICON_ITEM, GConst.FRAME_QLT[skinInfo.qlt])
		else
			self.icon:setSprite(GConst.ATLAS_PATH.COMMON, "common_alpha")
			self.sImg:setVisible(false)
		end
	else
		-- 其他
		self.icon:setSprite(GConst.ATLAS_PATH.ICON_ITEM, info.icon)
		self.fragment:setVisible(false)
		self.sImg:setVisible(false)
		self.matchImg:setVisible(false)
		self.skin:setVisible(false)
	end

	self:showLight(false)
end

function RewardCell:setIconSprite(atlas, res)
	self.icon:setSprite(atlas, res)
end

function RewardCell:setNumTx(str)
	self.numTx:setText(str)
end

function RewardCell:showMask(show, syncCheck)
	self.mask:setVisible(show == true)
	self:showCheck(syncCheck)
end

function RewardCell:hideCountTx()
	self:setNumTx(GConst.EMPTY_STRING)
end

function RewardCell:showCheck(show)
	self.check:setVisible(show == true)
end

function RewardCell:setVisible(visible)
	self.baseObject:setActive(visible)
end

function RewardCell:setActive(visible)
	self.baseObject:setActive(visible)
end

function RewardCell:setAnchoredPositionX(x)
	self.baseObject:setAnchoredPositionX(x)
end

function RewardCell:setTouchEnable(enable)
	self.baseObject:setTouchEnable(enable)
end

function RewardCell:addClickListener(callback)
	self.clickCallback = callback
end

function RewardCell:setLocalScale(x, y, z)
	self.baseObject:setLocalScale(x, y, z)
end

function RewardCell:showFrameAnimation(rewardType)
	self.frameAni:setVisible(true)
	self.frameAni:getComponent(GConst.TYPEOF_UNITY_CLASS.ANIMATOR).enabled = true
	if self.frameAniType ~= GConst.REWARD_TYPE.ITEM then
		-- CS.UnityEngine.Animator.StringToHash("frame_reward") 结果是997722489
		self.frameAni:getComponent(GConst.TYPEOF_UNITY_CLASS.ANIMATOR):Play(997722489, -1, 0)
		self.frameAniType = GConst.REWARD_TYPE.ITEM
	end
end

function RewardCell:hideFrameAnimation()
	self.frameAni:getComponent(GConst.TYPEOF_UNITY_CLASS.ANIMATOR).enabled = false
	self.frameAni:setVisible(false)
end

function RewardCell:showRightUpIcon(show, atlas, iconName)
	self.rightUpIcon:setVisible(show)
	if not show then
		return
	end
	self.rightUpIcon:setSprite(atlas, iconName)
end

-- 展示首通标记
function RewardCell:showFirstPass(show)
	self.firstPass:setVisible(show == true)
	if show then
		self.firstPassTx:setText(I18N:getGlobalText(I18N.GlobalConst.FIRST_PASS))
	end
end

-- 展示锁定标记
function RewardCell:showLock(show)
	self.lock:setVisible(show == true)
end

-- 展示光效
function RewardCell:showLight(show, ui)
	self.rootEffect:removeAllChildren()
	if show and ui ~= nil and not self.isLoadingEffectLight then
		self.isLoadingEffectLight = true
		EffectManager:loadUIEffectAsync(EFFECT_LIGHT, ui, self.rootEffect, GConst.UI_EFFECT_ORDER.LEVEL5, function(obj)
			self.isLoadingEffectLight = false
			obj:setAnchoredPosition(0, 0)
			obj:play()
		end)
	end
end

-- 展示选中框
function RewardCell:showSelect(show)
	self.select:setActive(show == true)
end

return RewardCell