local easing = require("easing")
--定义类
local Lingli = Class(function(self, inst)
    self.inst = inst
    self.maxlingli = 250
    self.minlingli = 0
    self.currentlingli = 100
	
	self.rate = 0
	--self.sane = true
	self.fxtime = 0
	--self.dapperness = 0
	--self.inducedinsanity = nil
	--self.night_drain_mult = 1
	--self.neg_aura_mult = 1

	--self.penalty = 0

	self.inst:StartUpdatingComponent(self)
	self:Recalc(0)
end)

function Lingli:OnSave()
	return {
		lingli = self.currentlingli,
	}
end

function Lingli:OnLoad(data)

    if data.lingli then
        self.currentlingli = data.lingli
        self:DoDelta(0)
    end
    
end

function Lingli:DoRegen()
    --print(string.format("Lingli:DoRegen ^%.2g/%.2fs", self.regen.amount, self.regen.period))
    if not self:IsDead() then
        self:DoDelta(self.regen.amount, true, "regen")
    else
        --print("    can't regen from dead!")
    end
end

function Lingli:StartRegen(amount, period, interruptcurrentregen)
    --print("Lingli:StopRegen", amount, period)

    -- We don't always do this just for backwards compatibility sake. While unlikely, it's possible some modder was previously relying on
    -- the fact that StartRegen didn't stop the existing task. If they want to continue using that behavior, they now just need to add
    -- a "false" flag as the last parameter of their StartRegen call. Generally, we want to restart the task, though.
    if interruptcurrentregen == nil or interruptcurrentregen == true then
        self:StopRegen()
    end

    --print("Lingli:StopRegen", amount, period)
    if not self.regen then
        self.regen = {}
    end
    self.regen.amount = amount
    self.regen.period = period

    if not self.regen.task then
        --print("   starting task")
        self.regen.task = self.inst:DoPeriodicTask(self.regen.period, function() self:DoRegen() end)
    end
end

function Lingli:StopRegen()
    --print("Lingli:StopRegen")
    if self.regen then
        if self.regen.task then
            --print("   stopping task")
            self.regen.task:Cancel()
            self.regen.task = nil
        end
        self.regen = nil
    end
end

function Lingli:GetPercent()
    return self.currentlingli / self.maxlingli
end

function Lingli:GetDebugString()
    local s = string.format("%2.2f / %2.2f", self.currentlingli, self.maxlingli, self.rate)
    if self.regen then
        s = s .. string.format(", regen %.2f every %.2fs", self.regen.amount, self.regen.period)
    end
    return s
end

function Lingli:SetMaxLingli(amount)
    self.maxlingli = amount
    self.currentlingli = amount

end

function Lingli:SetMinLingli(amount)
    self.minlingli = amount
end

function Lingli:GetRate()
	return self.rate
end

function Lingli:GetMaxLingli()
    return (self.maxlingli)
end

function Lingli:SetPercent(percentl)
    local target = percentl * self.max
    local delta = target - self.current
    self:DoDelta(delta)

    --self.currentlingli  = percentl*self.maxlingli
    --self:DoDelta(0)
end

function Lingli:DoDelta(delta,overtime)

    if self.redirect then
        self.redirect(self.inst, delta, overtime)
        return
    end

    local old = self.currentlingli
    self.currentlingli = self.currentlingli + delta
    if self.currentlingli < 0 then 
        self.currentlingli = 0
    elseif self.currentlingli > self:GetMaxLingli() then
        self.currentlingli = self:GetMaxLingli()
    end
    
    local oldpercent = old/self.maxlingli
    local newpercent = self.currentlingli/self.maxlingli
    
    self.inst:PushEvent("linglidelta", {oldpercent = oldpercent, newpercent = newpercent, overtime=overtime})

    if self.inst == GetPlayer() then
        if delta > 0 then
            ProfileStatsAdd("lingli+", math.floor(delta))
        end
    end
    --作用不明
    --if self.ondelta then
		--self.ondelta(self.inst, old, self:GetPercent())
    --end
end

function Lingli:OnUpdate(dt)
	
	local speed = easing.outQuad( 1 - self:GetPercent(), 0, .2, 1) 
	self.fxtime = self.fxtime + dt*speed
	
	PostProcessor:SetEffectTime(self.fxtime)
	
	local distortion_value = easing.outQuad( self:GetPercent(), 0, 1, 1) 
	--local colour_value = 1 - easing.outQuad( self:GetPercent(), 0, 1, 1) 
	--PostProcessor:SetColourCubeLerp( 1, colour_value )
	PostProcessor:SetDistortionFactor(distortion_value)
	PostProcessor:SetDistortionRadii( 0.5, 0.685 )

	--if self.inst.components.health.invincible == true or self.inst.is_teleporting == true then
		--return
	--end
	
	self:Recalc(dt)	
end

function Lingli:Recalc(dt)
	--local total_dapperness = self.dapperness or 0
	--local mitigates_rain = false
	--for k,v in pairs (self.inst.components.inventory.equipslots) do
		--if v.components.dapperness then
			--total_dapperness = total_dapperness + v.components.dapperness:GetDapperness(self.inst)
			--if v.components.dapperness.mitigates_rain then
				--mitigates_rain = true
			--end
		--end		
	--end

	
	local day = GetClock():IsDay() and not GetWorld():IsCave()
	
	
	local rain_delta = 0
    if GetSeasonManager() and GetSeasonManager():IsRaining() then
    	rain_delta = 0
    end

	self.rate = (rain_delta)	
	
	if self.custom_rate_fn then
		self.rate = self.rate + self.custom_rate_fn(self.inst)
	end

	--print (string.format("dapper: %2.2f light: %2.2f TOTAL: %2.2f", dapper_delta, light_delta, self.rate*dt))
	self:DoDelta(self.rate*dt, true)
end

function Lingli:LongUpdate(dt)
	self:OnUpdate(dt)
end


function Lingli:StartLingdc()
	local lingliResume = 1
    if self.currentlingli < 50 then
        return false--,"NOT_SANITY_ENOUGH"
    elseif self.currentlingli >= self.maxlingli then
        return false--,"MANA_ENOUGH"
    elseif not self.lingdcTask then
        self.lingdcTask = self.inst:DoPeriodicTask(1,function()
            if self.currentlingli < 50 then
				--self.inst.components.talker:Say("煞气不够!", 2.5,true)
		--	
				if self.inst:HasTag("Linghua") then
		        self.inst:RemoveTag("Linghua")
		
		  self.inst.Light:Enable(false)
		  self.inst.components.combat.areahitrange = 0
		  self.inst.components.combat.areahitdamagepercent = 0
		  self.inst.components.locomotor.walkspeed = self.inst.components.locomotor.walkspeed/1.2
		  self.inst.components.locomotor.runspeed = self.inst.components.locomotor.runspeed/1.2
		  self.inst.components.hunger.hungerrate = TUNING.WILSON_HUNGER_RATE * 1.2
		  self.inst.Light:Enable(false)
		  if self.inst.linghua then self.inst.linghua:Remove() self.inst.linghua = nil end	
		
		  self.inst.components.talker:Say("灵力不足！")
		  self.inst.components.lingli:StopLingdc()
		  --self.inst.sg:GoToState("shaqidizzy")
		  --self.inst:DoTaskInTime(4, function() self.inst.sg:GoToState("wakeup") end)
		  --self.inst:DoTaskInTime(0.1, function() self.inst.sg:GoToState("idle") end)
		        end
		--		
            else
                self:DoDelta(-lingliResume)
            end
        end)
        return true
    end
end

function Lingli:StopLingdc()
    if self.lingdcTask then
        self.lingdcTask:Cancel()
        self.lingdcTask=nil
    end
end


return Lingli