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

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={
		{13632, 'queuecommand', 'Prepare'}
	}
}

-- Children manipulation table
local Actors = {
	Name=name,
	Tween={
		{'Lanes', 15240, 'diffusealpha', 0},
		{'GOTO', 15240, 'diffusealpha', 1},
		{'Lanes', 15276, 'diffusealpha', 1},
		{'GOTO', 15276, 'diffusealpha', 0},
		{'NASA', 15312, 'diffusealpha', 1},
		{'Lanes', 15312, 'diffusealpha', 0},
		{'NASA', 15348, 'diffusealpha', 0},
		{'Lanes', 15348, 'diffusealpha', 1},
		{'THX', 15384, 'diffusealpha', 1},
		{'Lanes', 15384, 'diffusealpha', 0},
	}
}

local Players = {
	Tween={
	}
}

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

-- 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)
end,

-- contour extraction
paraph.AFT({})..{
	Name='NoteField',
	paraph.AFT({})..{
		Name='Contour1',
		Def.ActorFrame{
			-- I know this is stupid but
			PrepareCommand=function(self)
				if pe[2] then self:addx(6) end
			end,
			paraph.AMVQuad({})..{
				PrepareCommand=function(self)
					self:SetTexture(FG:GetChild('PlayerAFTorig'):GetTexture()):z(1):diffusealpha(0.1)
				end
			},
			paraph.AMVQuad({})..{
				PrepareCommand=function(self)
					self:SetTexture(FG:GetChild('PlayerAFTorig'):GetTexture()):xy(4,4)
				end
			},
			paraph.AMVQuad({})..{
				PrepareCommand=function(self)
					self:SetTexture(FG:GetChild('PlayerAFTorig'):GetTexture()):xy(4,-4)
				end
			},
			paraph.AMVQuad({})..{
				PrepareCommand=function(self)
					self:SetTexture(FG:GetChild('PlayerAFTorig'):GetTexture()):xy(-4,4)
				end
			},
			paraph.AMVQuad({})..{
				PrepareCommand=function(self)
					self:SetTexture(FG:GetChild('PlayerAFTorig'):GetTexture()):xy(-4,-4)
				end
			},
		},
	},

	Def.ActorFrame{
		Name='Frame',
		InitializeCommand=function(self) self:zoom(0.9) end,
		Def.Quad{
			InitializeCommand=function(self) self:zoomto(sw/0.9,sh/0.9):diffuse(1,1,1,1):z(-1) end,
		},
		paraph.AMVQuad({})..{
			InitializeCommand=function(self)
				self:SetTexture(self:GetParent():GetParent():GetChild('Contour1'):GetTexture()):diffusealpha(0.05)
			end,
		},
		Def.Quad{
			InitializeCommand=function(self) self:zoomto(sw/0.9,sh/0.9):diffuse(0,0,0,1):z(-1) end,
		},
		Def.Quad{
			InitializeCommand=function(self)
				self:zoomto(384,184):diffuse(1,1,1,1):xy(439,270):z(-1)
			end
		},
		Def.Quad{
			InitializeCommand=function(self)
				self:zoomto(380,180):diffuse(0,0,0,1):xy(439,270):z(-1)
			end
		},
		paraph.BitmapText('Applied Options:',{Size=24,Align='left'})..{
			InitializeCommand=function(self)
				self:x(-scx+32):y(-scy+48):z(-1)
			end,
		},
		paraph.BitmapText('none',{Size=24,Align='left'})..{
			InitializeCommand=function(self)
				self:x(-scx+64):y(-scy+80):z(-1)
			end,
		},
		paraph.BitmapText('-- #GOTO lua system', {Size=24,Align='left'})..{
			InitializeCommand=function(self)
				self:x(-scx+32):y(-scy+144):z(-1)
			end,
		},
		paraph.BitmapText('SONG TITLE',{Size=24,Align='left'})..{
			InitializeCommand=function(self)
				self:x(248):y(200):z(-1)
			end,
		},
		paraph.BitmapText('#GOTO',{Size=24,Align='left'})..{
			InitializeCommand=function(self)
				self:x(248+200):y(200):z(-1)
			end,
		},
		paraph.BitmapText('ARTIST',{Size=24,Align='left'})..{
			InitializeCommand=function(self)
				self:x(248):y(200+40):z(-1)
			end,
		},
		paraph.BitmapText('NASA',{Size=24,Align='left'})..{
			InitializeCommand=function(self)
				self:x(248+200):y(200+40):z(-1)
			end,
		},
		paraph.BitmapText('GENRE',{Size=24,Align='left'})..{
			InitializeCommand=function(self)
				self:x(248):y(200+80):z(-1)
			end,
		},
		paraph.BitmapText('Re:CODING',{Size=24,Align='left'})..{
			InitializeCommand=function(self)
				self:x(248+200):y(200+80):z(-1)
			end,
		},
		paraph.BitmapText('BPM',{Size=24,Align='left'})..{
			InitializeCommand=function(self)
				self:x(248):y(200+140):z(-1)
			end,
		},
		paraph.BitmapText('135',{Size=24,Align='left'})..{
			InitializeCommand=function(self)
				self:x(248+200):y(200+140):z(-1)
			end,
		},
		Def.ActorMultiVertex{
			InitializeCommand=function(self)
				local v = {
					{{-152-2,-scy*2/3,0}, {1,1,1,1}},
					{{ 155+2,-scy*2/3,0}, {1,1,1,1}},
					{{ 155+2, scy*2/3,0}, {1,1,1,1}},
					{{-152-2, scy*2/3,0}, {1,1,1,1}},
					{{-152-2,-scy*2/3,0}, {1,1,1,1}},
				}
				self:SetVertices(v)
				self:SetDrawState{First=1,Num=-1,Mode='DrawMode_LineStrip'}
				self:SetLineWidth(4/1.5):zoom(1.5)
			end,
		},
		Def.Quad{
			InitializeCommand=function(self)
				self:zoomto((152+155)*1.5,4):xy(2,222):diffuse(1,1,1,1)
			end
		},
		Def.Quad{
			Name='UpperLine',
			InitializeCommand=function(self)
				self:zoomto((152+155)*1.5,4):xy(2,-222):diffuse(1,1,1,0)
			end
		},
		Def.Quad{
			InitializeCommand=function(self)
				self:zoomto(450,72):y(292):diffuse(1,1,1,1):z(-1)
			end,
		},
		Def.Quad{
			InitializeCommand=function(self)
				self:zoomto(442,64):y(292):diffuse(0,0,0,1):z(-1)
			end,
		},
		Def.Quad{
			InitializeCommand=function(self)
				self:horizalign('left'):xy(-219,292):zoomto(0,60):diffuse(1,1,1,1):z(-1)
			end,
			PrepareCommand=function(self) self:queuecommand('Update') end,
			UpdateCommand=function(self)
				local pn = pe[1] and 0 or 1
				local life = STATSMAN:GetCurStageStats():GetPlayerStageStats(pn):GetCurrentLife()
				local width = 438*life
				self:zoomto(width,60):sleep(rr):queuecommand('Update')
			end
		},
	},
},
Def.Quad{
	InitializeCommand=function(self)
		self:zoomto(sw,sh):diffuse(0,0,0,1):z(-2)
	end,
},
paraph.AMVQuad({})..{
	Name='Lanes',
	PrepareCommand=function(self)
		self:SetTexture(self:GetParent():GetChild('NoteField'):GetTexture()):z(-1)
	end
},
Def.ActorMultiVertex{
	InitializeCommand=function(self)
		local v = {
			{{0,0,1},{1,1,1,1}},
			{{0,0,1},{1,1,1,1}},
			{{0,0,1},{1,1,1,1}},
			{{0,0,1},{1,1,1,1}},
		}
		self:diffusealpha(0.01):SetVertices(v):SetDrawState{First=1,Num=-1,Mode='DrawMode_Quads'}
		self:sleep(bt/4)
		self:linear(bt*3/4)
		self:SetVertices{
			{{-sw,0,1},{1,1,1,1}},
			{{0,-sh,1},{1,1,1,1}},
			{{ sw,0,1},{1,1,1,1}},
			{{0, sh,1},{1,1,1,1}},
		}
	end,
},
paraph.BitmapText('#GOTO',{Name='GOTO'})..{
	OnCommand=function(self) self:diffusealpha(0):z(1) end
},
paraph.BitmapText('NASA',{Name='NASA'})..{
	OnCommand=function(self) self:diffusealpha(0):z(1) end
},
paraph.BitmapText('Thank you for playing',{Name='THX'})..{
	OnCommand=function(self) self:diffusealpha(0):z(1) end
},
}