local EquipEntity = class("EquipEntity", BaseData)

local EquipCfg = ConfigManager:getConfig("equip")

function EquipEntity:setDirty()
	self.allAttrs = nil
	self.curPower = nil
end

function EquipEntity:ctor(equip)
	self:init(equip)
end

function EquipEntity:init(equip)
	self.uid = equip.uid
	self:setId(equip.cfg_id)
	self:initBaseAttr()
	self:setExtraAttr(equip.extra_attrs)

	self.lastPower = nil
end

-- 装备唯一id
function EquipEntity:getUid()
	return self.uid
end

function EquipEntity:setId(id)
	self.id = id
	self.config = EquipCfg[id]
	self:setDirty()
end

--获取配置id
function EquipEntity:getId()
	return self.id
end

--@region 配置
function EquipEntity:getPart()
	return self.config.type
end

function EquipEntity:getQlt()
	return self.config.qlt
end

function EquipEntity:getStar()
	return self.config.star
end

function EquipEntity:getIconRes()
	return self.config.icon
end

function EquipEntity:getEntityType()
	return GConst.ENTITY_TYPE.EQUIP_ENTITY
end

function EquipEntity:getName()
	return DataManager.EquipData:getName(self:getId())
end

-- function EquipEntity:getNameQltColor()
-- 	return string.format("<color=%s>%s</color>", GFunc.getQltColor(self:getQlt()), self:getName())
-- end

-- function EquipEntity:getPartName()
-- 	return I18N:getGlobalText(GConst.EquipConst.EQUIP_PART_NAME[self:getPart()])
-- end
--@endregion

--@region 属性
function EquipEntity:initBaseAttr()
	self.baseAttrs = DataManager.EquipData:getBaseAttr(self:getId())
end

function EquipEntity:setExtraAttr(attr)
	self.extraAttrs = {}
	self.extraMap = attr
	if attr and #attr > 0 then
		for i, data in pairs(attr) do
			if self.extraAttrs[GFunc.getAttrNameById(data.id)] then
				self.extraAttrs[GFunc.getAttrNameById(data.id)] = self.extraAttrs[GFunc.getAttrNameById(data.id)] + data.value
			else
				self.extraAttrs[GFunc.getAttrNameById(data.id)] = data.value
			end
		end
	end
	self.allAttrs = nil
end

function EquipEntity:getBaseAttrs()
	return self.baseAttrs
end

function EquipEntity:getExtraMap()
	return self.extraMap
end

function EquipEntity:getExtraAttrs()
	return self.extraAttrs
end

function EquipEntity:getAllAttr()
	if self.allAttrs == nil then
		self.allAttrs = {}
		-- 基础
		-- for attrName, attrNum in pairs(self:getBaseAttrs()) do
		-- 	self.allAttrs[attrName] = (self.allAttrs[attrName] or 0) + attrNum
		-- end
		self.allAttrs[self.baseAttrs.type] = (self.allAttrs[self.baseAttrs.type] or 0) + self.baseAttrs.num
		-- 额外属性
		for attrName, attrNum in pairs(self:getExtraAttrs()) do
			self.allAttrs[attrName] = (self.allAttrs[attrName] or 0) + attrNum
		end

		self:calcPower()
	end

	return self.allAttrs
end
--@endregion

--@region 战力
function EquipEntity:setPowerDirty()
	self.data.isPowerDirty = not self.data.isPowerDirty
end

function EquipEntity:getPower()
	if not self.curPower then
		self:getAllAttr()
	end
	return self.curPower
end

-- 计算战斗力
function EquipEntity:calcPower()
	if self.lastPower then
		self.lastPower = self.curPower
	end
	self.curPower = math.floor(self:_getAttrPower())
	if not self.lastPower then
		self.lastPower = self.curPower
	end

	if self.lastPower ~= self.curPower then
		self:setPowerDirty()
	end
end

function EquipEntity:_getAttrPower()
	local power = 0
	local attr = self:getAllAttr()
	for attrName, attrNum in pairs(attr) do
		local cfg = GFunc.getAttrNameCfg()[attrName]
		if cfg then
			local realValue = attrNum
			-- 特殊处理，玩家基础暴击伤害不算
			if attrName == GConst.BattleConst.ATTR_NAME.CRIT_TIME then
				realValue = attrNum - 15000
			end
			power = power + math.floor(realValue * cfg.power / GConst.DEFAULT_FACTOR + 0.0000001)
		end
	end

	return power
end
--@endregion

--@region 数据
function EquipEntity:getPartLevel(slotId)
	return DataManager.EquipData:getPartLevel(slotId, self:getPart())
end
--@endregion
return EquipEntity