-- 此通用方法会加重资源消耗，改为单个

modifier_ability_power_common = class({})

local public = modifier_ability_power_common

local m_modifier_funcs=
{
	MODIFIER_PROPERTY_OVERRIDE_ABILITY_SPECIAL,
	MODIFIER_PROPERTY_OVERRIDE_ABILITY_SPECIAL_VALUE,
	-- 有个显示bug，_Description描述中引用了值名称的不会变，单独的会变
}

-- function public:IsHidden()
-- 	return true
-- end

function public:IsPermanent()
	return true
end

function public:RemoveOnDeath()
	return false
end

function public:IsDebuff()
	return false
end

function public:IsPurgable()
	return false
end

function public:DeclareFunctions()
	return m_modifier_funcs
end

function public:OnCreated( kv )
	-- self.bDirty = true  --本地时在其它函数不起作用

	if IsServer() then
		-- local playerId = self:GetParent():GetPlayerOwnerID()	
		-- CustomNetTables:SetTableValue("CustomGameInfo", "PowerAbilityValue"..tostring(playerId), GameRules.player_power_enabled[playerId])
		-- print("---- is Server")
		-- DeepPrintTable(GameRules.player_power_enabled[playerId])
	-- else   --这里可以生效，但在其它函数里失效
	-- 	self:GetParent().PowerAbilityUpgrades = CustomNetTables:GetTableValue("CustomGameInfo", "PowerAbilityValue"..tostring(self:GetParent():GetPlayerOwnerID()))
	-- 	print("---- not Server")
	-- 	DeepPrintTable(self:GetParent().PowerAbilityUpgrades)
	end
end

-- function public:OnCreated( kv )
-- 	if not IsServer() then return end
-- 	self:OnRefresh( kv )
-- end

-- function public:OnRefresh( kv )
-- 	-- 设置层数仅本地会触发，无层数设置为1层时不触发
-- 	-- 使用 modifier:ForceRefresh() 会双端触发	
-- 	if not IsServer() then return end	
-- end

function public:GetModifierOverrideAbilitySpecial( params )
	-- 这两个必须在双端跑

	if self:GetParent() == nil or params.ability == nil then
		return 0
	end

	-- 本地取值失效，并且从NetTable取值后每次生成新的一张table，下同
	-- local hUpgrades = self:GetParent().PowerAbilityUpgrades
	-- if hUpgrades == nil then
	-- 	hUpgrades = CustomNetTables:GetTableValue("CustomGameInfo", "PowerAbilityValue"..tostring(self:GetParent():GetPlayerOwnerID()))
	-- 	print("---- hUpgrades == nil， CustomNetTables:GetTableValue")
	-- 	print(hUpgrades)
	-- end
	local hUpgrades = CustomNetTables:GetTableValue("CustomGameInfo", "PowerAbilityValue"..tostring(self:GetParent():GetPlayerOwnerID()))
	local szUnitName = self:GetParent():GetUnitName()
	local szAbilityName = params.ability:GetAbilityName() 
	local szSpecialValueName = params.ability_special_value
	
	if hUpgrades == nil or hUpgrades[szUnitName] == nil or hUpgrades[szUnitName][szAbilityName] == nil then
		return 0
	end

	if hUpgrades[szUnitName][szAbilityName][szSpecialValueName] == nil then
		return 0
	end

	return 1
end

function public:GetModifierOverrideAbilitySpecialValue( params )
	-- 这两个必须在双端跑

	local hUpgrades =  CustomNetTables:GetTableValue("CustomGameInfo", "PowerAbilityValue"..tostring(self:GetParent():GetPlayerOwnerID()))	
	local szUnitName = self:GetParent():GetUnitName()
	local szAbilityName = params.ability:GetAbilityName() 
	local szSpecialValueName = params.ability_special_value
	local nSpecialLevel = params.ability_special_level
	if hUpgrades == nil or hUpgrades[szUnitName] == nil or hUpgrades[szUnitName][szAbilityName] == nil then
		return 0
	end

	local flBaseValue = params.ability:GetLevelSpecialValueNoOverride(szSpecialValueName, nSpecialLevel)
	local SpecialValueUpgrade = hUpgrades[szUnitName][szAbilityName][szSpecialValueName]

	if SpecialValueUpgrade ~= nil then	
		return flBaseValue + SpecialValueUpgrade
		
		-- 本地不起作用
		-- if self.bDirty == false and SpecialValueUpgrade["cached_result"] ~= nil and SpecialValueUpgrade["cached_result"][nSpecialLevel] ~= nil then
		-- 	print("---- return cached_result")
		-- 	return SpecialValueUpgrade["cached_result"][nSpecialLevel]
		-- end

		-- local flResult = flBaseValue + SpecialValueUpgrade["value"]
		-- if SpecialValueUpgrade["cached_result"] == nil then 
		-- 	SpecialValueUpgrade["cached_result"] = {}
		-- end
		-- SpecialValueUpgrade["cached_result"][nSpecialLevel] = flResult		
		-- self.bDirty = false
		-- return flResult	
	end

	return flBaseValue
end