
.namespace SoundFader


---------------------------copier coller de "progressive action"-------------------------------------------------------

CurrentAction={}
TIMER=0.03 --valeur que l'on peut changer aprés, selon la fluidité
g_timer = nil

function Act() --function spécifique à chaque script
	return
end

function CheckEnd()
	return false
end

function Finish()

end

function StartTimer()
    g_timer=GlobalFunctions.CreateTimerSystem(self.Name,TIMER,false,true,false, self)
    g_timer.Enabled=true
    g_timer.Start()
	self.CanTimeOut=true
end

--a appelé lors du lancement
function CheckTimer()
	if g_timer == nil then
		StartTimer()
	end
end

function OnUpdateUnit()

end

function OnUpdate(dt)
	self.CanTimeOut=true
	OnUpdateUnit(dt)
end

function OnTimeOut()

	if self.CanTimeOut then
		if CurrentAction==nil then
			OPLog.Log('erreur ! CurrentAction=nil '..self.Name)
		end
		if table.getn(CurrentAction) == 0 then
			OPUnitFactory.CloseUnit(g_timer)
			g_timer = nil
		end
		
		for u,action in pairs(CurrentAction) do
			if not CheckEnd(action) then
				newact = Act(action)
				if newact~=nil then
					CurrentAction[u] = newact
				end
			else
				table.remove(CurrentAction,u)
				Finish(action)
			end
		end
		self.CanTimeOut=false
	end
end

--------------------------------------------------------------------------

--TODO à fusionner avec le spritefader ?

function Fade(ut,speed,volume,utnotify, closeatend)

	if ut == nil or ut== nil then
		AssertPopup(false,"unit null !")
		return
	end
	
	AssertPopup(type(speed) =="number" and type(volume) == "number", "erreur params sur "..ut.Name)

	
	--OPLog.Log("soundfade "..ut.Name.." "..speed.." "..volume)
    local action={}

    --on vérifie d'abord si cette unit n'est pas dans la liste des move--
    for u,v in pairs(CurrentAction) do
        if v.ut.Name==ut.Name then
            v.speed = speed
            v.utnotify = utnotify
			v.volume = volume
			v.name = ut.Name
			v.closeatend = closeatend
			CheckTimer()
            return true
        end
    end
    
	action.name = ut.Name
	action.ut=ut
	action.speed=speed
	action.utnotify=utnotify
	action.volume=volume
	action.closeatend = closeatend
	
	CheckTimer()
	
    table.insert(CurrentAction,action)
	
	
end

function StopAllSoundFade()
	CurrentAction = {}
end

function Act(action)
	if action.ut == nil or action.ut== nil then
		AssertPopup(false,"erreur le son n'est plus la !"..action.name)
		Stop(action.name)
		return
	end

	if action.ut.Volume < action.volume then
		action.ut.Volume=math.min(action.volume,action.ut.Volume+action.speed)
	else
		action.ut.Volume=math.max(action.volume,action.ut.Volume-action.speed)
	end
end

function CheckEnd(action)
	if action.ut == nil then
		return true
	end

	if  action.ut.Volume == action.volume then
		return true
	end
	return false
end

function Finish(action)
	if action.utnotify~=nil then
		action.utnotify.OnSoundFadeEnd(action.ut)
	end
	if action.closeatend == true then
		OPUnitFactory.CloseUnit(action.ut)
	end
end

function Stop(name)
	for u,v in pairs(CurrentAction) do
		if v.name== name then
			table.remove(CurrentAction,u)
		end
    end
end

--permet de changer le coeff d'un son en cours
function ScaleSound(name,coeff)
	OPLog.Log("name coeff"..name.." "..coeff)
	for u,v in pairs(CurrentAction) do
		if v.name== name then
			v.volume = v.volume * coeff
			return true
		end
    end
	return false
end
