local EquipEntity = require "app/userdata/equip/equip_entity"
local EquipEntityOther = class("EquipEntityOther", EquipEntity)

local EquipCfg = ConfigManager:getConfig("equip")

function EquipEntityOther:ctor(equip)
	self.baseAttrs = {}
	self:init(equip)
end

function EquipEntityOther:init(equip)
	self:setId(equip.cfg_id)
	self:initBaseAttr()
	self:setExtraAttr(equip.extra_attrs)

	self.lastPower = nil
end

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

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

--@region 属性
function EquipEntityOther:initBaseAttr()
	for k,v in pairs(self.config.base_attr) do
		self.baseAttrs[k] = v
	end
end

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

function EquipEntityOther:getBaseAttr()
	return self.baseAttrs
end

function EquipEntityOther:getExtraMap()
	return self.extraMap
end

function EquipEntityOther:getExtraList()
	local list = {}
	for k,v in pairs(self.extraAttrs) do
		table.insert(list, {type = k, num = v, id = GFunc.getAttrIdByName(k)})
	end
	table.sort(list, function (a, b)
		return a.id < b.id
	end)
	return list
end

function EquipEntityOther:getExtraAttrs()
	return self.extraAttrs
end

function EquipEntityOther:getAllAttr()
	if self.allAttrs == nil then
		self.allAttrs = {}
		-- 基础
		-- for attrName, attrNum in pairs(self:getBaseAttr()) 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 EquipEntityOther:setPowerDirty()
	self.data.isPowerDirty = not self.data.isPowerDirty
end

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

-- 计算战斗力
function EquipEntityOther: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 EquipEntityOther:_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

return EquipEntityOther