local EquipCell = class("EquipCell", BaseCell)

function EquipCell:init()
	local uiMap = self:getUIMap()
	self.imgQlt = uiMap["equip_cell.content.img_qlt"]
	self.imgIcon = uiMap["equip_cell.content.img_icon"]
	self.rankNode = uiMap["equip_cell.content.rank"]
	self.txRank = uiMap["equip_cell.content.rank.tx_rank"]
	self.levelNode = uiMap["equip_cell.content.level"]
	self.txLv = uiMap["equip_cell.content.level.tx_lv"]
	self.imgUp = uiMap["equip_cell.content.img_up"]
	self.mask = uiMap["equip_cell.mask"]
	self.check = uiMap["equip_cell.check"]
	self.lock = uiMap["equip_cell.lock"]
	self.select = uiMap["equip_cell.select"]
	self.light = uiMap["equip_cell.light"]
	self.levelNode:setActive(false)

	-- self:hideEffectUp()
end

-- function EquipCell:setParentUI(parent)
-- 	self.parentUI = parent
-- end

function EquipCell:refresh(entity, slotId, showMask, showCheck, showLock)
	if entity == nil then
		return
	end
	self:_refreshShow(entity:getQlt(), entity:getIconRes(), entity:getPartLevel(slotId), showMask, showCheck, showLock)
end

function EquipCell:refreshByUid(uid)
	self:refresh(DataManager.EquipData:getEntityByUid(uid))
end

-- message Equip {
-- 	int64         uid              = 1; // 唯一 id
-- 	int32         cfg_id           = 2; // 配置 id
-- 	int32         init_id          = 3; // 初始配置 id (升品前)
-- 	repeated Attr purity_attrs     = 4; // 洗炼属性
-- 	repeated Attr new_purity_attrs = 5; // 未应用的洗炼属性
--   }
function EquipCell:refreshByServer(equip)
	local id = equip.cfg_id
	self:_refreshShow(DataManager.EquipData:getQlt(id), DataManager.EquipData:getIconRes(id))
end

-- message EquipInfo {
-- 	int32         cfg_id       = 1; // 配置 ID
-- 	int32         level        = 2; // 槽位等级
-- 	repeated Attr purity_attrs = 3; // 洗炼属性
--   }
function EquipCell:refreshByServer2(equip)
	local id = equip.cfg_id
	local lv = equip.level
	self:_refreshShow(DataManager.EquipData:getQlt(id), DataManager.EquipData:getIconRes(id), lv)
end

function EquipCell:refreshByCfg(id, showMask, showCheck, showLock)
	self:_refreshShow(DataManager.EquipData:getQlt(id), DataManager.EquipData:getIconRes(id), nil, showMask, showCheck, showLock)
end

function EquipCell:refreshByCustom(qlt, icon, lv, showMask, showCheck, showLock)
	self:_refreshShow(qlt, icon, lv, showMask, showCheck, showLock)
end

function EquipCell:_refreshShow(qlt, icon, lv, showMask, showCheck, showLock)
	if qlt then
		self.imgQlt:setSprite(GConst.ATLAS_PATH.ICON_EQUIP, "frame_" .. qlt)
	else
		self.imgQlt:setSprite(GConst.ATLAS_PATH.ICON_EQUIP, "frame_0")
	end

	if icon then
		self.imgIcon:setActive(true)
		Logger.logHighlight("icon: %s", icon)
		self.imgIcon:setSprite(GConst.ATLAS_PATH.ICON_EQUIP, icon)
	else
		self.imgIcon:setActive(false)
	end

	if lv then
		self.levelNode:setActive(true)
		self.txLv:setText(I18N:getGlobalText(I18N.GlobalConst.EQUIP_DESC_2, lv))
	else
		self.levelNode:setActive(false)
	end

	self:showMask(showMask)
	self:showCheck(showCheck)
	self:showLock(showLock)
	self:showSelect(false)
	self:showLight(false)
	self:setShowUp(false)
end

function EquipCell:refreshEmpty(part, showSelect)
	self.imgQlt:setSprite(GConst.ATLAS_PATH.ICON_EQUIP, "frame_0")
	-- self.imgIcon:setActive(true)
	self.imgIcon:setSprite(GConst.ATLAS_PATH.ICON_EQUIP, "101")
	self.rankNode:setActive(false)
	self.levelNode:setActive(false)

	self:showMask(false)
	self:showCheck(false)
	self:showLock(false)
	self:showSelect(showSelect or false)
	self:showLight(false)
	self:setShowUp(false)
end

function EquipCell:setShowUp(show)
	self.imgUp:setActive(show)
end

function EquipCell:setShowLv(show)
	self.levelNode:setActive(show)
end

function EquipCell:showMask(show)
	self.mask:setActive(show)
end

function EquipCell:showCheck(show)
	self.check:setActive(show)
end

function EquipCell:showLock(show)
	self.lock:setActive(show)
end

function EquipCell:showSelect(show)
	self.select:setActive(show)
end

function EquipCell:showLight(show)
	self.light:setActive(show)
end

function EquipCell:setTouchEnable(enable)
	self:getBaseObject():setTouchEnable(enable == true)
end

function EquipCell:addClickListener(func)
    self:getBaseObject():addClickListener(func)
end

function EquipCell:removeClickListener()
	self:getBaseObject():removeClickListener()
end

function EquipCell:getAnchoredPositionX()
    return self:getBaseObject():getAnchoredPositionX()
end

function EquipCell:setVisible(visible, scale)
	self:getBaseObject():setVisible(visible, scale)
end

function EquipCell:setActive(active)
	self:getBaseObject():setActive(active)
end

-- function EquipCell:showEffectUp()
-- 	if self.effectUp == nil and self.parentUI then
-- 		EffectManager:loadUIEffectAsync("assets/prefabs/effects/ui/vfx_ui_b9_yjqh_b01.prefab", self.parentUI, self.baseObject, GConst.UI_EFFECT_ORDER.LEVEL5, function(obj)
-- 			obj:setLocalScale(1, 1, 1)
-- 			obj:setAnchoredPosition(0, 0, 0)
-- 			obj:play()
-- 			self.effectUp = obj

-- 			self.baseObject:performWithDelayGlobal(function()
-- 				self:hideEffectUp()
-- 			end, 0.5)
-- 		end)
-- 	end
-- end

-- function EquipCell:hideEffectUp()
-- 	if self.effectUp then
-- 		self.effectUp:setActive(false)
-- 	end
-- end

return EquipCell
