-- ActorFrame name, used for controlling the children from control actor (in FG).
local name = 'section5'

local base_param = ...
local sw,sh,scx,scy,songdir,rr,am,bt,hs,pe,hr,ease,wratio,hratio,paraph = base_param.sw,base_param.sh,base_param.scx,base_param.scy,base_param.songdir,base_param.rr,base_param.am,base_param.bt,base_param.hs,base_param.pe,base_param.hr,base_param.ease,base_param.wratio,base_param.hratio,base_param.paraph

-- Camera manipulation table
local Cameras = {
	Name=name,
	Tween={
		{13008, 'queuecommand', 'Coin'},
		{13632, 'queuecommand', 'RemoveInputHandler'}
	}
}

-- Children manipulation table
local Actors = {
	Name=name,
	Tween={
	}
}

local Players = {
	Tween={
	}
}

-- Shortcut to the 'FG' ActorFrame.
local FG

-- Acceleration of the lanes.
local acc = 0

-- Input Handler.
local InputHandler = function(event)
	if not event then return end

	local a = FG:GetChild('elements'):GetChild('section4'):GetChild('texture'):GetChild('contents')
	if event.type == 'InputEventType_FirstPress' then
		if event.button == 'Scratch up' then
			acc = -0.07
		elseif event.button == 'Scratch down' then
			acc = 0.07
		end
	elseif event.type == 'InputEventType_Release' then
		if string.find(event.button, 'Scratch') then
			acc = 0
		end
	end
end

-- We can use "InitializeCommand" instead of usual "OnCommand", which is executed on the beginning of section.
-- This reduces that ridiculously large frame drop on beat 0.
return Def.ActorFrame{
Name=name,
InitCommand=function(self) FG = self:GetParent():GetParent() self:visible(false):zbuffer(1) end,
InitializeCommand=function(self)
	local c = FG:GetChild('Cameras')
	local a = FG:GetChild('Actors')
--	local plt = a[1].Tween
--	for i = 1, #Players do
--		plt[#plt+1] = Players[i]
--	end
	c[#c+1] = Cameras
	a[#a+1] = Actors
	self:fov(90)
	-- Load Input Handler.
	SCREENMAN:GetTopScreen():AddInputCallback(InputHandler)
end,
CoinCommand=function(self)
	MESSAGEMAN:Broadcast('Coin')
end,
RemoveInputHandlerCommand=function(self)
	SCREENMAN:GetTopScreen():RemoveInputCallback(InputHandler)
end,
paraph.AFT({})..{
	Name='texture',
	Def.ActorFrame{
		Name='contents',
		InitializeCommand=function(self) self:fov(90):queuecommand('Update') end,
		UpdateCommand=function(self)
			self:aux(self:getaux()*0.98)
			self:aux(self:getaux()+acc)
			if self:getaux() > 0.5 then self:aux(0.5)
			elseif self:getaux() < -0.5 then self:aux(-0.5)
			end
			self:linear(rr):addrotationz(self:getaux()):queuecommand('Update')
		end,
		loadfile(songdir..'fg/section5/lanes.lua')(base_param),
		loadfile(songdir..'fg/section5/lanes.lua')(base_param)..{
			InitializeCommand=function(self) self:blend('BlendMode_Add') end
		},
	},
},
paraph.AMVQuad({})..{
	InitializeCommand=function(self) self:SetTexture(self:GetParent():GetChild('texture'):GetTexture()) end,
},
Def.ActorMultiVertex{
	RemoveInputHandlerCommand=function(self)
		local v = {}
		for i = 1, 10 do
			v[#v+1] = {{0,0,1},{1,1,1,1}}
		end
		self:SetVertices(v):SetDrawState{First=1,Num=-1,Mode='DrawMode_QuadStrip'}
		self:linear(bt/8)
		self:SetVertex(1, {{-sw*3/24,0,1},{1,1,1,1}})
		self:SetVertex(3, {{0,-sh*3/24,1},{1,1,1,1}})
		self:SetVertex(5, {{ sw*3/24,0,1},{1,1,1,1}})
		self:SetVertex(7, {{0, sh*3/24,1},{1,1,1,1}})
		self:SetVertex(9, {{-sw*3/24,0,1},{1,1,1,1}})
		self:linear(bt*3/4)
		self:SetVertices{
			{{-sw-sw*3/24,0,1},{1,1,1,0}},
			{{-sw,0,1},{1,1,1,0}},
			{{0,-sh-sh*3/24,1},{1,1,1,0}},
			{{0,-sh,1},{1,1,1,0}},
			{{ sw+sw*3/24,0,1},{1,1,1,0}},
			{{ sw,0,1},{1,1,1,0}},
			{{0, sh+sh*3/24,1},{1,1,1,0}},
			{{0, sh,1},{1,1,1,0}},
			{{-sw-sw*3/24,0,1},{1,1,1,0}},
			{{-sw,0,1},{1,1,1,0}},
		}
	end,
}
}