local HeroEntity = require "app/userdata/hero/hero_entity"
local HeroEntityOther = class("HeroEntityOther", HeroEntity)
local ATTR_NAME = GConst.BattleConst.ATTR_NAME

function HeroEntityOther:ctor(cfgId, lv, skin, star)
	self.cfgId = cfgId
	self.data.isDirty = false
	self.data.lv = lv
	self.data.skin = skin
	self.data.star = star or 0
	self.config = ConfigManager:getConfig("hero")[self.cfgId]
	self.beginLv = 1 -- 激活等级
	self.isNew = false

	self.baseAttrOriginal = {}
	self.starAttr = {}
	self.skinAttr = {}
	self.allBaseAttr = {}
	self.allAttr = {}
	self.attrDirty = true
	self.attrBaseDirty = true
end

-- 更新所有属性(包括其他英雄的加成)
function HeroEntityOther:_updateAllAttr()
	self.allAttr = {}
	self.allBaseAttr = self:_getAllBaseAttr()
	for k, v in pairs(self.allBaseAttr) do
		self.allAttr[k] = v
	end

	-- 装备
	local equipAttr = DataManager.EquipDataOther:getEquipAttrBySlotId(self:getMatchType())
	for k, v in pairs(equipAttr) do
		self.allAttr[k] = (self.allAttr[k] or 0) + v
	end

	-- 同属性通用加成
	self.allAttr[GConst.MATCH_ALL_ATKP_NAME[self:getMatchType()]] = nil
	for _, v in pairs(GConst.ATTR_ALL) do
		self.allAttr[v] = nil
	end

	-- 攻击力（百分比）
	local atkType = GConst.MATCH_ATTACK_NAME[self:getMatchType()]
	-- 全局增加攻击力
	local allAtk = self:getGlobalAttrByType(GConst.ATTR_ALL.ATTR_ATK_ALL)
	local persionalAtk = self.allAttr[GConst.ATTR_PERSIONAL.ATTR_ATK] or 0
	local atk = self.allAttr[atkType] + allAtk + persionalAtk
	-- 全局元素攻击力百分比加成
	local allFactorValue = self:getGlobalAttrByType(GConst.MATCH_ALL_ATKP_NAME[self:getMatchType()])
	-- 个人攻击力百分比加成
	local factorValue = self.allAttr[GConst.MATCH_ATTACK_ADD_NAME[self:getMatchType()]] or 0
	-- 全局攻击力百分比加成
	local allAtkP = self:getGlobalAttrByType(GConst.ATTR_ALL.ATTR_ATKP_ALL)
	self.allAttr[atkType] = math.floor(atk * (1 + (factorValue + allFactorValue + allAtkP) / GConst.DEFAULT_FACTOR) + 0.0000001)
	self.allAttr[GConst.MATCH_ATTACK_ADD_NAME[self:getMatchType()]] = nil

	-- 生命（百分比）
	local hpType = GConst.MATCH_HP_NAME[self:getMatchType()]
	-- 全局增加生命
	local allHp = self:getGlobalAttrByType(GConst.ATTR_ALL.ATTR_HP_ALL)
	local persionalHp = self.allAttr[GConst.ATTR_PERSIONAL.ATTR_HP] or 0
	local hp = self.allAttr[hpType] + allHp + persionalHp
	-- 全局元素生命百分比加成
	local allFactorValue = self:getGlobalAttrByType(GConst.MATCH_ALL_HPP_NAME[self:getMatchType()])
	-- 个人生命百分比加成
	local factorValue = self.allAttr[GConst.MATCH_HP_ADD_NAME[self:getMatchType()]] or 0
	-- 全局生命百分比加成
	local allHpP = self:getGlobalAttrByType(GConst.ATTR_ALL.ATTR_HPP_ALL)
	self.allAttr[hpType] = math.floor(hp * (1 + (factorValue + allFactorValue + allHpP) / GConst.DEFAULT_FACTOR) + 0.0000001)
	self.allAttr[GConst.MATCH_HP_ADD_NAME[self:getMatchType()]] = nil

	self.allAttr[ATTR_NAME.DMGDEC] = (self.allAttr[ATTR_NAME.DMGDEC] or 0) + self:getGlobalAttrByType(GConst.ATTR_ALL.ATTR_DMGDEC_ALL)
	self.allAttr[GConst.MATCH_CRIT_NAME[self:getMatchType()]] = (self.allAttr[GConst.MATCH_CRIT_NAME[self:getMatchType()]] or 0) + self:getGlobalAttrByType(GConst.ATTR_ALL.ATTR_CRIT_ALL)
	self.allAttr[GConst.MATCH_CRIT_TIME_NAME[self:getMatchType()]] = (self.allAttr[GConst.MATCH_CRIT_TIME_NAME[self:getMatchType()]] or 0) + self:getGlobalAttrByType(GConst.ATTR_ALL.ATTR_CRIT_TIME_ALL)
	self.allAttr[GConst.MATCH_NORMAL_HURTP_NAME[self:getMatchType()]] = (self.allAttr[GConst.MATCH_NORMAL_HURTP_NAME[self:getMatchType()]] or 0) + self:getGlobalAttrByType(GConst.ATTR_ALL.ATTR_NORMAL_HURTP_ALL) + self:getGlobalAttrByType(GConst.ATTR_PERSIONAL.ATTR_NORMAL_HURTP)
	self.allAttr[GConst.MATCH_SKILL_HURTP_NAME[self:getMatchType()]] = (self.allAttr[GConst.MATCH_SKILL_HURTP_NAME[self:getMatchType()]] or 0) + self:getGlobalAttrByType(GConst.ATTR_ALL.ATTR_SKILL_HURTP_ALL) + self:getGlobalAttrByType(GConst.ATTR_PERSIONAL.ATTR_SKILL_HURTP)

	self:calcPower()
end

function HeroEntityOther:getGlobalAttrByType(attrType)
	return DataManager.HeroDataOther:getAttrByMatchType(self:getMatchType(), attrType) or 0
end

return HeroEntityOther