local ItemEntity = class("ItemEntity", BaseData)

function ItemEntity:ctor(id, num)
	self.data.id = id
	self.data.num = num
	self.data.isDirty = false
	self.config = nil

	self:_loadConfig(id)
	self:checkForceLockAndAddNum(num)
end

function ItemEntity:getConfig(id)
	local ItemCfg = ConfigManager:getConfig("item")
	local config = ItemCfg[id]
	return config
end

function ItemEntity:_loadConfig(id)
	local config = self:getConfig(id)
	self.config = config
end

function ItemEntity:setDirty()
	self.data.isDirty = not self.data.isDirty
end

-- id
function ItemEntity:getId()
	return self.data.id
end

-- 道具数量
function ItemEntity:getNum()
	return self.data.num
end

-- 加减道具数量
function ItemEntity:addNum(num)
	self.data.num = self.data.num + num
	self:checkForceLockAndAddNum()
	self:setDirty()
end

function ItemEntity:checkForceLockAndAddNum()
	local type = self:getItemType()
	if type == GConst.ItemConst.ITEM_TYPE.HERO_FRAGMENT then
		local isOwn = DataManager.HeroData:getHeroIsUnlock(self:getId())
		local qlt = DataManager.HeroData:getHeroQlt(self:getId())
		-- local fragmentId = DataManager.ForceData:getForceItemIdByQlt(self:getId())
		local upCost = DataManager.HeroData:getHeroLevelCost(1, qlt)
		if not isOwn and self.data.num >= upCost[1] then
			-- self.data.num = self.data.num - upCost[1] 不消耗碎片了
			-- DataManager.HeroData:onForceUnlock(self:getId(), true)
			-- BIReport:postForceUnlock(self:getId())
		end
		-- 不转换万能碎片
		-- local parameter = self:getParam()
		-- if qlt >= 5 and parameter and parameter[3] then
		-- 	DataManager.HeroData:addSummonUnlockItemCount(self.data.num * parameter[3])
		-- 	-- DataManager.BagData.ItemData:addItemNumById(fragmentId, self.data.num * parameter[3], BIReport.ITEM_GET_TYPE.FORCE_SUMMON_EXCHANGE)
		-- 	self.data.num = 0
		-- end
	end
end

-- 设置数量
function ItemEntity:setNum(num)
	self.data.num = num
	self:setDirty()
end

-- 道具类型
function ItemEntity:getItemType()
	return self.config.type
end

-- 品质
function ItemEntity:getQuality()
	return self.config.qlt
end

-- 稀有度
function ItemEntity:getRarity()
	return self.config.rarity
end

function ItemEntity:getFrameRes()
	return GConst.ATLAS_PATH.ICON_ITEM, "frame_" .. self.config.qlt
end

function ItemEntity:getIconRes()
	return GConst.ATLAS_PATH.ICON_ITEM, self.config.icon
end

-- 其他参数
function ItemEntity:getParam()
	return self.config.parameter
end

-- 道具描述
function ItemEntity:getName()
	return I18N:getText("item", self.data.id, "name")
end

-- 道具描述
function ItemEntity:getDesc()
	return I18N:getText("item", self.data.id, "desc")
end

function ItemEntity:getItemType()
	return self.config.type
end

return ItemEntity