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

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={
		{228, 'queuecommand', 'Move1'},
		{420, 'queuecommand', 'Move2'},
		{612, 'queuecommand', 'Move3'},
		{804, 'queuecommand', 'Move4'},
		{876, 'queuecommand', 'Open'},
		{960, 'queuecommand', 'DeleteNASA'},
	}
}

-- Children manipulation table
local Actors = {
	Name=name,
	Tween={
		{'Displays/13', 3072, 'zoom', 0.5},
		{'Displays/13', 3072, 'xy', {-sw/4,-sh/4}},
		{'Displays/14', 3072, 'zoom', 0.5},
		{'Displays/14', 3072, 'xy', { sw/4,-sh/4}},
		{'Displays/15', 3072, 'zoom', 0.5},
		{'Displays/15', 3072, 'xy', {-sw/4, sh/4}},
		{'Displays/16', 3072, 'zoom', 0.5},
		{'Displays/16', 3072, 'xy', { sw/4, sh/4}},

		{'Displays/8', 3120, 'zoom', 1/3},
		{'Displays/8', 3120, 'xy', {-sw/3,-sh/3}},
		{'Displays/9', 3120, 'zoom', 1/3},
		{'Displays/9', 3120, 'xy', { 0,-sh/3}},
		{'Displays/10', 3120, 'zoom', 1/3},
		{'Displays/10', 3120, 'xy', { sw/3,-sh/3}},
		{'Displays/11', 3120, 'zoom', 1/3},
		{'Displays/11', 3120, 'xy', {-sw/3, 0}},
		{'Displays/12', 3120, 'zoom', 1/3},
		{'Displays/12', 3120, 'xy', { 0, 0}},
		{'Displays/13', 3120, 'zoom', 1/3},
		{'Displays/13', 3120, 'xy', { sw/3, 0}},
		{'Displays/14', 3120, 'zoom', 1/3},
		{'Displays/14', 3120, 'xy', {-sw/3, sh/3}},
		{'Displays/15', 3120, 'zoom', 1/3},
		{'Displays/15', 3120, 'xy', { 0, sh/3}},
		{'Displays/16', 3120, 'zoom', 1/3},
		{'Displays/16', 3120, 'xy', { sw/3, sh/3}},

		{'Displays/1', 3168, 'zoom', 1/4},
		{'Displays/1', 3168, 'xy', {-sw*3/8,-sh*3/8}},
		{'Displays/2', 3168, 'zoom', 1/4},
		{'Displays/2', 3168, 'xy', {-sw*1/8,-sh*3/8}},
		{'Displays/3', 3168, 'zoom', 1/4},
		{'Displays/3', 3168, 'xy', { sw*1/8,-sh*3/8}},
		{'Displays/4', 3168, 'zoom', 1/4},
		{'Displays/4', 3168, 'xy', { sw*3/8,-sh*3/8}},
		{'Displays/5', 3168, 'zoom', 1/4},
		{'Displays/5', 3168, 'xy', {-sw*3/8,-sh*1/8}},
		{'Displays/6', 3168, 'zoom', 1/4},
		{'Displays/6', 3168, 'xy', {-sw*1/8,-sh*1/8}},
		{'Displays/7', 3168, 'zoom', 1/4},
		{'Displays/7', 3168, 'xy', { sw*1/8,-sh*1/8}},
		{'Displays/8', 3168, 'zoom', 1/4},
		{'Displays/8', 3168, 'xy', { sw*3/8,-sh*1/8}},
		{'Displays/9', 3168, 'zoom', 1/4},
		{'Displays/9', 3168, 'xy', {-sw*3/8, sh*1/8}},
		{'Displays/10', 3168, 'zoom', 1/4},
		{'Displays/10', 3168, 'xy', {-sw*1/8, sh*1/8}},
		{'Displays/11', 3168, 'zoom', 1/4},
		{'Displays/11', 3168, 'xy', { sw*1/8, sh*1/8}},
		{'Displays/12', 3168, 'zoom', 1/4},
		{'Displays/12', 3168, 'xy', { sw*3/8, sh*1/8}},
		{'Displays/13', 3168, 'zoom', 1/4},
		{'Displays/13', 3168, 'xy', {-sw*3/8, sh*3/8}},
		{'Displays/14', 3168, 'zoom', 1/4},
		{'Displays/14', 3168, 'xy', {-sw*1/8, sh*3/8}},
		{'Displays/15', 3168, 'zoom', 1/4},
		{'Displays/15', 3168, 'xy', { sw*1/8, sh*3/8}},
		{'Displays/16', 3168, 'zoom', 1/4},
		{'Displays/16', 3168, 'xy', { sw*3/8, sh*3/8}},

		{'Displays/16', 3216, 'zoom', 1},
		{'Displays/16', 3216, 'xy', { 0, 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,
DeleteNASACommand=function(self) self:RemoveChild('pixel') end,

-- contour extraction
paraph.AFT({})..{
	Name='NoteField',
	paraph.AFT({})..{
		Name='Contour1',
		Def.ActorFrame{
			-- I know this is stupid but
			OpenCommand=function(self)
				if pe[2] then self:addx(6) end
			end,
			paraph.AMVQuad({})..{
				Name='AMV1',
				OpenCommand=function(self)
					self:SetTexture(FG:GetChild('PlayerAFTorig'):GetTexture()):z(1):diffusealpha(0.1)
				end
			},
			paraph.AMVQuad({})..{
				Name='AMV2',
				OpenCommand=function(self)
					self:SetTexture(FG:GetChild('PlayerAFTorig'):GetTexture()):xy(4,4)
				end
			},
			paraph.AMVQuad({})..{
				Name='AMV3',
				OpenCommand=function(self)
					self:SetTexture(FG:GetChild('PlayerAFTorig'):GetTexture()):xy(4,-4)
				end
			},
			paraph.AMVQuad({})..{
				Name='AMV4',
				OpenCommand=function(self)
					self:SetTexture(FG:GetChild('PlayerAFTorig'):GetTexture()):xy(-4,4)
				end
			},
			paraph.AMVQuad({})..{
				Name='AMV5',
				OpenCommand=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{
			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,
			OpenCommand=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
		},
	},
},
loadfile(songdir..'fg/section1/display.lua')(base_param)..{
	Name='Displays'
},

-- mask
Def.Quad{
	InitCommand=function(self) self:visible(false):zoomto(1,3):diffuse(0,0,0,0.01):z(2) end,
	OpenCommand=function(self) self:visible(true):decelerate(bt*7/4):zoomto(sh,sw):rotationz(450) end,
},
Def.Quad{
	InitCommand=function(self) self:visible(false):zoomto(1+8,3+8):diffuse(1,1,1,1):z(1) end,
	OpenCommand=function(self) self:visible(true):decelerate(bt*7/4):zoomto(sh+8,sw+8):rotationz(450) end,
},

-- song informations
paraph.AFT({
	Width = sw*3,
	Height = sh*3,
})..{
	Name='Texture',
	Def.ActorFrame{
		paraph.BitmapText('TEAM',{Size=64})..{
			InitCommand=function(self) self:y(-sh/6) end,
			Move2Command=function(self) self:visible(false) end,
		},
		paraph.BitmapText('Gimmick Info',{Size=96})..{
			InitCommand=function(self) self:y(sh/16) end,
			Move2Command=function(self) self:visible(false) end,
		},
		paraph.BitmapText('BOF:ET Branch',{Size=64})..{
			InitCommand=function(self) self:y(sh/16+sh/8) end,
			Move2Command=function(self) self:visible(false) end,
		},
		paraph.BitmapText('SONG TITLE',{Size=64})..{
			InitCommand=function(self) self:y(-sh-sh/6) end,
		},
		paraph.BitmapText('#GOTO',{Size=96})..{
			InitCommand=function(self) self:y(-sh+sh/8) end,
		},
		paraph.BitmapText('ARTIST',{Size=64})..{
			InitCommand=function(self) self:xy(sw,-sh/6) end,
		},
		paraph.BitmapText('NASA',{Size=96})..{
			InitCommand=function(self) self:xy(sw,sh/8) end,
		},
		paraph.BitmapText('GENRE',{Size=64})..{
			InitCommand=function(self) self:xy(sw,sh-sh/6) end,
		},
		paraph.BitmapText('Re:CODING',{Size=96})..{
			InitCommand=function(self) self:xy(sw,sh+sh/8) end,
		},
		paraph.BitmapText('CHART',{Size=64})..{
			InitCommand=function(self) self:y(-sh/6):visible(false) end,
			Move2Command=function(self) self:visible(true) end,
		},
		paraph.BitmapText('paraph',{Size=96})..{
			InitCommand=function(self) self:y(sh/8):visible(false) end,
			Move2Command=function(self) self:visible(true) end,
		},
	},
},
Def.Quad{
	InitCommand=function(self) self:zoomto(sw,sh):diffuse(0,0,0,1) end,
},
loadfile(songdir..'fg/section1/pixelize.lua')(base_param)..{
	Move1Command=function(self)
		local bit = self:getaux()
		for i = 1, sw/bit do
			if i/(sw/bit) >= 1/3 and i/(sw/bit) <= 2/3 then
			 	local r = math.random(0, 32)*bt/64
			 	for j = 1, sh/bit do
					self:GetChild(i..j):sleep(r):linear(bt/2):addy(sh/3)
				end
			end
		end
	end,
	Move2Command=function(self)
		local bit = self:getaux()
		for j = 1, sh/bit do
			if j/(sh/bit) >= 0 and j/(sw/bit) <= 2/3 then
				local r = math.random(0, 32)*bt/64
			 	for i = 1, sw/bit do
					self:GetChild(i..j):sleep(r):linear(bt/2):addx(-sw/3)
				end
			end
		end
	end,
	Move3Command=function(self)
		local bit = self:getaux()
		for i = 1, sw/bit do
			if i/(sw/bit) >= 1/3 and i/(sw/bit) <= 1 then
			 	local r = math.random(0, 32)*bt/64
			 	for j = 1, sh/bit do
					self:GetChild(i..j):sleep(r):linear(bt/2):addy(-sh/3)
				end
			end
		end
	end,
	Move4Command=function(self)
		local bit = self:getaux()
		for j = 1, sh/bit do
			if j/(sh/bit) >= 0 and j/(sw/bit) <= 2/3 then
				local r = math.random(0, 32)*bt/64
			 	for i = 1, sw/bit do
					self:GetChild(i..j):sleep(r):linear(bt/2):addx(sw/3)
				end
			end
		end
	end,
},

}