local TS
local songdir = GAMESTATE:GetCurrentSong():GetSongDir()
local ntreset = false
local NoteSkins
local tab_sp = function(str, ts)
	if ts == nil then return {} end
	local t = {}
	local itr = 1
	for s in string.gmatch(str, '([^'..ts..']+)') do
		t[itr] = s;
		itr = itr+1;
	end
	return t
end

local wr, hr

return Def.ActorFrame{
Def.Quad{OnCommand=function(self) self:zoomto(SCREEN_WIDTH,SCREEN_HEIGHT):diffuse(0,0,0,1) end},
Def.ActorFrameTexture{
	OnCommand=function(self)
		-- Calculate AFT texture ratio
		self:SetWidth(SCREEN_WIDTH):SetHeight(SCREEN_HEIGHT):EnableDepthBuffer(true):EnableAlphaBuffer(true):Create()
		wr = SCREEN_WIDTH/self:GetTexture():GetTextureWidth()
		hr = SCREEN_HEIGHT/self:GetTexture():GetTextureHeight()
		local RF = RageFileUtil.CreateRageFile()
		if RF:Open(songdir..'fg/ratio.lua', 2) then
			local t = table.concat({wr,hr}, ',')
			RF:Write(t)
			RF:Close()
			RF:destroy()
		end
	end
},
Def.Actor{
	OnCommand=function(self)
		local RF = RageFileUtil.CreateRageFile()
		if RF:Open(songdir..'fg/currentskin.lua', 1) then
			NoteSkins = tab_sp(RF:Read(), ',')
			RF:Close()
			RF:destroy()
		else
			NoteSkins = {'default', 'default'}
		end

		if GAMESTATE:IsPlayerEnabled(0) and GAMESTATE:IsPlayerEnabled(1) then
			SCREENMAN:SystemMessage('Versus mode was detected: FG will not work')
		else
			self:queuecommand('getTS')
		end
	end,
	getTSCommand=function(self)
		TS = SCREENMAN:GetTopScreen()
		if TS and TS == SCREENMAN:GetTopScreen() then
			TS:AddChildFromPath(songdir..'fg/default.lua')
			for i = 1, 2 do
				if TS:GetChild('PlayerP'..i) then
					local P = TS:GetChild('PlayerP'..i)
					local zoom = (LoadModule("Config.Load.lua")("MiniSelector",CheckIfUserOrMachineProfile(i-1).."/OutFoxPrefs.ini") or 100)/100
					P:zoom(P:GetZoom()/zoom)
					for c, col in ipairs(P:GetChild('NoteField'):get_column_actors()) do
						col:rotationx(0):rotationy(0):rotationz(0):y(0)
					end
					local NoteSkin, bool = GAMESTATE:GetPlayerState(i-1):GetPlayerOptions('ModsLevel_Preferred'):NoteSkin('default')
					if not bool then
						SCREENMAN:SystemMessage('OutFox default NoteSkin was not found: please install default NoteSkin')
						GAMESTATE:ApplyGameCommand('screen,ScreenSelectMusic')
					elseif NoteSkin ~= 'default' then
						NoteSkins[i] = NoteSkin
						ntreset = true
					end
				end
			end

			if ntreset then
				NoteSkins[3] = 'r'
				local RF = RageFileUtil.CreateRageFile()
				if RF:Open(songdir..'fg/currentskin.lua', 2) then
					local t = table.concat(NoteSkins, ',')
					RF:Write(t)
					RF:Close()
					RF:destroy()
				end
				SCREENMAN:SystemMessage('This BMS works only with default NoteSkin. Reloading with default NoteSkin...')
				self:sleep(0.1):queuecommand('reload')
			elseif not NoteSkins[3] then
				local RF = RageFileUtil.CreateRageFile()
				if RF:Open(songdir..'fg/currentskin.lua', 2) then
					local t = 'default,default'
					RF:Write(t)
					RF:Close()
					RF:destroy()
				end		
			elseif NoteSkins[3] then
				for i = 1, 2 do
					if TS:GetChild('PlayerP'..i) then
						GAMESTATE:GetPlayerState(i-1):GetPlayerOptions('ModsLevel_Preferred'):NoteSkin(NoteSkins[i])
					end
					local RF = RageFileUtil.CreateRageFile()
					if RF:Open(songdir..'fg/currentskin.lua', 2) then
						NoteSkins[3] = nil
						local t = table.concat(NoteSkins, ',')
						RF:Write(t)
						RF:Close()
						RF:destroy()
					end
				end
			end
			
		else
			self:sleep(1/60):queuecommand('getTS')
		end
	end,
	reloadCommand=function(self)
		GAMESTATE:ApplyGameCommand('screen,ScreenGameplay')
	end
}
}