local TalentData = class("TalentData", BaseData)

local TalentCfg = ConfigManager:getConfig("talent")
local PlayerExpCfg = ConfigManager:getConfig("player_exp")

TalentData.SKILL_REFESH_ID = 9
TalentData.HP_RESTORE_ID = 10
TalentData.SELECT_SKILL_ID = 11
TalentData.ICON_ADD_ID = 12
TalentData.EXP_ADD_ID = 16

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

function TalentData:ctor()
	self.data.isDirty = false
end

function TalentData:init(data)
	data = data or GConst.EMPTY_TABLE
	self.stage = data.stage
	self.levels = data.levels or {}

	self.allAttr = {}
	self.attrDirty = true
	self:getAllAttr()
end

function TalentData:clear()
end

function TalentData:getIsOpen()
	if ModuleManager:getIsOpen(ModuleManager.MODULE_KEY.TALENT_OPEN, true) then
		return true
	end
	return false
end

function TalentData:getCfgList()
	return TalentCfg
end

function TalentData:getActiveList()
	local list = {}
	for id = 1, #TalentCfg do
		local cfg = TalentCfg[id]
		cfg.id = id
		local lv = self:getLevels(id)
		if lv and lv > 0 then
			table.insert(list, cfg)
		end
	end
	return list
end

function TalentData:getPlayerExpCfgList(stage)
	if stage then
		return PlayerExpCfg[stage]
	end
	return PlayerExpCfg
end

function TalentData:getStage()
	return self.stage
end

function TalentData:checkStage()
	local lv = DataManager.PlayerData:getLv()
	return lv >= self.stage, self.stage
end

function TalentData:getLevels(id)
	if id then
		return self.levels[id] or 0
	end
	return self.levels
end

function TalentData:onUpgrade(data)
	self.upData = data
	self.stage = data.stage
	self.levels[data.id] = data.level
	self.attrDirty = true
	self:getAllAttr()
	self:setDirty()
end

function TalentData:getUpData()
	local data = self.upData
	self.upData = nil
	return data
end

function TalentData:isAllMax()
	for id = 1, #TalentCfg do
		local cfg = TalentCfg[id]
		local lv = self:getLevels(id)
		if not lv or lv < cfg.max_level then
			return false
		end
	end
	return true
end

--@region 属性
function TalentData:getAttrById(id)
	local cfg = TalentCfg[id]
	if not cfg then
		return {}
	end
	local lv = self:getLevels(id)
	local allAttr = {}
	local num = 0
	if not cfg.attr then
		num = cfg.value
	else
		for i,v in ipairs(cfg.attr) do
			local attr = {}
			attr.type = v.type
			attr.num = v.num * lv
			table.insert(allAttr, attr)
		end
	end
	return allAttr, num
end

function TalentData:getAllAttr()
	if self.attrDirty == true then
		self.attrDirty = false
		self:_updateAllAttr()
	end
	return self.allAttr
end

function TalentData:_updateAllAttr()
	self.allAttr = {}
	for id = 1, #TalentCfg do
		local cfg = TalentCfg[id]
		local lv = self:getLevels(id)
		if lv and lv > 0 and cfg.attr then
			for _, v in ipairs(cfg.attr) do
				self.allAttr[v.type] = (self.allAttr[v.type] or 0) + v.num * lv
			end
		end
	end

	-- 处理全局属性
	local attr = {}
	for _, v in pairs(GConst.ATTR_ALL) do
		attr[v] = self.allAttr[v]
	end

	DataManager.HeroData:setTalentAttr(attr)

	return self.allAttr
end
--@endregion

--@region 获取特殊天赋属性
function TalentData:getSkillRefreshCount()
	local lv = self:getLevels(self.SKILL_REFESH_ID)
	local cfg = TalentCfg[self.SKILL_REFESH_ID]
	return lv * (cfg.value or 0)
end

function TalentData:getHpRestore()
	local lv = self:getLevels(self.HP_RESTORE_ID)
	local cfg = TalentCfg[self.HP_RESTORE_ID]
	return lv * (cfg.value or 0)
end

function TalentData:getSelectSkillBegin()
	local lv = self:getLevels(self.SELECT_SKILL_ID)
	local cfg = TalentCfg[self.SELECT_SKILL_ID]
	return lv * (cfg.value or 0)
end

function TalentData:getIconAdd()
	local lv = self:getLevels(self.ICON_ADD_ID)
	local cfg = TalentCfg[self.ICON_ADD_ID]
	return lv * (cfg.value or 0)
end

function TalentData:getExpAdd()
	local lv = self:getLevels(self.EXP_ADD_ID)
	lv = 1
	local cfg = TalentCfg[self.EXP_ADD_ID]
	return lv * (cfg.value or 0)
end
--@endregion

--@region 红点
function TalentData:showRedPoint()
	local stage = self:getStage()
	local cfg = self:getPlayerExpCfgList(stage)
	if not GFunc.checkCost(cfg.cost.id, cfg.cost.num) then
		return false
	end
	local can = self:checkStage()
	if not can then
		return false
	end
	return true
end
--@endregion
return TalentData