local easing = require("easing")

local Shaqi = Class(function(self, inst)
    self.inst = inst
	self.current = 50
    self.max = 200
    --self.current = 50
	
	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 Shaqi:OnSave()
	return {
	current = self.current, 
	}
end

function Shaqi:OnLoad(data)

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


function Shaqi:GetMaxShaqi()
	return (self.max)
end


function Shaqi:GetPercent()

	return self.current / self.max

end

function Shaqi:SetPercent(per)
    local target = per * self.max
    local delta = target - self.current
    self:DoDelta(delta)
end

function Shaqi:GetDebugString()
    return string.format("%2.2f / %2.2f at %2.4f. Penalty of %2.2f", self.current, self.max, self.rate)
end

function Shaqi:SetMax(amount)
    self.max = amount
    self.current = amount
end

function Shaqi:GetRate()
	return self.rate
end


function Shaqi:DoDelta(delta, overtime)

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



    local old = self.current
    self.current = self.current + delta
    if self.current < 0 then 
        self.current = 0
    elseif self.current > self:GetMaxShaqi() then
        self.current = self:GetMaxShaqi()
    end
    
    local oldpercent = old/self.max
    local newpercent = self.current/self.max
    
    self.inst:PushEvent("shaqidelta", {oldpercent = oldpercent, newpercent = newpercent, overtime=overtime})
	
    if self.inst == GetPlayer() then
        if delta > 0 then
            ProfileStatsAdd("shaqi+", math.floor(delta))
        end
    end

    
end

function Shaqi: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 Shaqi: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 = TUNING.DAPPERNESS_MED*15* GetSeasonManager():GetPrecipitationRate()
    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 Shaqi:LongUpdate(dt)
	self:OnUpdate(dt)
end


function Shaqi:StartShadc()
	local shaqiResume = 1
    if self.current < 20 then
        return false--,"NOT_SANITY_ENOUGH"
    --elseif self.current>=self.max then
        --return false--,"MANA_ENOUGH"
		
	
    
    elseif not self.shadcTask then
        self.shadcTask = self.inst:DoPeriodicTask(1,function()
            if self.current < 20 then
				--self.inst.components.talker:Say("煞气不够!", 2.5,true)
		--	
		   if self.inst:HasTag("Shaqihua") then

		    



		  self.inst.components.talker:Say("我好头晕~~.....")
		  self.inst.components.shaqi:StopShadc()
		  --self.inst.sg:GoToState("shaqidizzy")
		  self.inst:DoTaskInTime(0.5, function() self.inst.sg:GoToState("death") end)
		  self.inst:DoTaskInTime(5, function() self.inst.sg:GoToState("wakeup") end)
		  self.inst:DoTaskInTime(0.1, function() self.inst.sg:GoToState("idle") end)
		   end
		--		
            else
		  	  self.inst.components.combat.areahitrange = self.inst.tosolevel/15+2.5
		      self.inst.components.combat.areahitdamagepercent = 1
			  self.inst.components.hunger.hungerrate = 0	
              self:DoDelta(-shaqiResume)
			  --self.inst.shaqi_fx = SpawnPrefab("shaistate")
			  --SpawnPrefab("shaistate").Transform:SetPosition(0, -1, 0)
			--self.inst.shaqi_fx = SpawnPrefab("baguadun")
			  --self.inst.shaqi_fx.entity:SetParent(self.inst.entity)
			  --self.inst.shaqi_fx.Transform:SetPosition(0, -1, 0)
			  --self.inst.shaqi_fx.AnimState:PushAnimation("idle", true)	
            end
        end)
        return true
    end
end

function Shaqi:StopShadc()
            self.inst:RemoveTag("Shaqihua")
		    self.inst.shaqibaocun = false
		    self.inst.components.combat.areahitrange = 0
		    self.inst.components.combat.areahitdamagepercent = 0
			self.inst.components.hunger.hungerrate = TUNING.WILSON_HUNGER_RATE * 1.2
		--关闭夜视 
                if  GetClock() then
                    GetClock():SetNightVision(false)
                end
                if  GetWorld() and GetWorld().components.colourcubemanager then
                    GetWorld().components.colourcubemanager:SetOverrideColourCube(nil, .5)
                end	

    if self.shadcTask then
        self.shadcTask:Cancel()
        self.shadcTask=nil
    end
	
	--if self.inst.shaqi_fx then self.inst.shaqi_fx:Remove() self.inst.shaqi_fx = nil end	
end

return Shaqi
