local EquipEntity = class("EquipEntity", BaseData)

function EquipEntity:setDirty()
	self.allAttrs = 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:setDirty()
end

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

--@region 配置
function EquipEntity:getPart()
	return DataManager.EquipData:getPart(self:getId())
end

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

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

function EquipEntity:getIconRes()
	return DataManager.EquipData:getIconRes(self:getId())
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.baseAttr = 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
		-- 额外属性
		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()
    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 atk = self.baseAttr.num + (self.extraAttrs[GConst.BattleConst.ATTR_NAME.ATTR_ATK_PRIVATE] or 0)
	-- local atkPower = AttrNameCfg[GConst.BattleConst.ATTR_NAME.ATTR_ATK_PRIVATE].power or 0
	-- local crit = self.extraAttrs[GConst.BattleConst.ATTR_NAME.CRIT_PRIVATE] or 0			--暴击
	-- local critPower = AttrNameCfg[GConst.BattleConst.ATTR_NAME.CRIT_PRIVATE].power or 0
	-- local critTime = self.extraAttrs[GConst.BattleConst.ATTR_NAME.CRIT_TIME_PRIVATE] or 0	--暴伤
	-- local critTimePower = AttrNameCfg[GConst.BattleConst.ATTR_NAME.CRIT_TIME_PRIVATE].power or 0
	-- local def = self.extraAttrs[GConst.BattleConst.ATTR_NAME.DEF_DEC_PRIVATE] or 0			--破防
	-- local defPower = AttrNameCfg[GConst.BattleConst.ATTR_NAME.DEF_DEC_PRIVATE].power or 0
	-- def = def / GConst.DEFAULT_FACTOR / (10 * GFunc.getBattleConstIntValue("def_dec_constant_k") + def/ GConst.DEFAULT_FACTOR)
	-- local score = atk/GConst.DEFAULT_FACTOR
	-- 	*(1+crit/GConst.DEFAULT_FACTOR*(critPower / GConst.DEFAULT_FACTOR )
	-- 	*critTime/GConst.DEFAULT_FACTOR*(critTimePower/ GConst.DEFAULT_FACTOR))
	-- 	*(1+def*defPower/GConst.DEFAULT_FACTOR)
	-- return math.floor(score)

	local power = 0
	for attrName, attrNum in pairs(self:getAllAttr()) 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 / GConst.DEFAULT_FACTOR + 0.0000001)
		end
	end

	return power
end
--@endregion

return EquipEntity