--This whole file stolen from Benpai x Crash Cringle's Delightful Day from the pack Animal Crossover.

local P1, P2, P1Score, P2Score
local difficulty, sTable
local songName= GAMESTATE:GetCurrentSong():GetSongDir() -- Looks for Song

local WideScale = function(AR4_3, AR16_9)
	-- return scale( SCREEN_WIDTH, 640, 854, AR4_3, AR16_9 )
	local w = 480 * PREFSMAN:GetPreference("DisplayAspectRatio")
	return scale( w, 640, 854, AR4_3, AR16_9 )
 end

local function Handler_mod_internal(str, pn) -- Applies Players options to not be disabled-
	local ps= GAMESTATE:GetPlayerState(pn)
	local pmods= ps:GetPlayerOptionsString('ModsLevel_Song')
	ps:SetPlayerOptions('ModsLevel_Song', pmods .. ', ' .. str)
	--GAMESTATE:ApplyGameCommand('mod,'..str, pn)
end


local function isEditMode()
	for _,player in ipairs(GAMESTATE:GetHumanPlayers()) do
		if (GAMESTATE:GetCurrentSteps(player):GetDifficulty() == "Difficulty_Edit") then
			return true;
		end
	end
	return false;
end
local function setEditChart()
	sTable = GAMESTATE:GetCurrentSong():GetStepsByStepsType( "StepsType_Dance_Single" );
	for _,player in ipairs(GAMESTATE:GetHumanPlayers()) do
		if (GAMESTATE:GetCurrentSteps(player):GetDifficulty() == "Difficulty_Challenge" or GAMESTATE:GetCurrentSteps(player):GetDifficulty() == "Difficulty_Hard") then
			GAMESTATE:SetCurrentSteps(player,sTable[2])
			SCREENMAN:SetNewScreen('ScreenGameplay')
		end
	end
end

local function Handler_mod(str) -- Looks for many players are on the screen 
	if (isEditMode()) then return end
	for i=1,2 do
		Handler_mod_internal(str, 'PlayerNumber_P' .. i)
	end
end

local function Handler_init() -- Useful for command shorcuts
	if (isEditMode()) then
		setEditChart()
		return;
	end
	if SCREENMAN:GetTopScreen():GetChild('PlayerP1') and not isEditMode() then
		P1 = SCREENMAN:GetTopScreen():GetChild('PlayerP1')
		P1:visible(false)
	end
	if SCREENMAN:GetTopScreen():GetChild('PlayerP2') and not isEditMode() then
		P2 = SCREENMAN:GetTopScreen():GetChild('PlayerP2')
		P2:visible(false)
	end
	if SCREENMAN:GetTopScreen():GetChild('Underlay'):GetChild('P1Score') and not isEditMode() then
		P1Score = SCREENMAN:GetTopScreen():GetChild('Underlay'):GetChild('P1Score')
	end
	if SCREENMAN:GetTopScreen():GetChild('Underlay'):GetChild('P2Score')and not isEditMode() then
		P2Score = SCREENMAN:GetTopScreen():GetChild('Underlay'):GetChild('P2Score')
	end


	fgcurcommand = 0;
	wndr_skewx=0.3;
	checked = false;
	--lua course :D	/ timed mod management	
	curmod = 1;		
	--{beat,'mod'},
	mods = {
	{4, '*99999 100% boost'},
	{15, '*2 no boost'},	
    {72, '*1 100% confusion'},
    {72.50, '*1  no confusion'},
    {112, '*1 100% confusion'},
    {112.5, '*1  no confusion'},
    {152, '*1 100% confusion'},
    {152.5, '*1  no confusion'},
    {192, '*1 100% confusion'},
    {192.5, '*1  no confusion'},
    {232, '*1 100% confusion'},
    {232.5, '*1  no confusion'},

	}
	--SCREAMING GUMBALL / timed message broadcaster
	curmessage = 1;
	--{beat,message,ignoreIfAhead}
	messages = {
    --{5, 'DiffuseAlpha'},
    {64, 'Vibrate1'},
    {72, 'Vibrate3'},
    {112, 'Vibrate5'},
    {152, 'Vibrate10'},
    {192, 'Vibrate20'},
    {232, 'Vibrate30'},
    {248, 'Vibrate20'},
    {248, 'DiffuseAlpha'},
    {257, 'Vibrate10'},
    {266, 'Vibrate5'},
    {270, 'Vibrate3'},
    {292, 'Vibrate1'},
		{300, 'Stop'},

	}
	
end

local function Handler_update() -- Updates the command to look for the players at the start of the song.

	if GAMESTATE:GetSongBeat()>=0.1 and not checked then
		
		screen = SCREENMAN:GetTopScreen()
		
		checked = true;
		
	end

	local beat = GAMESTATE:GetSongBeat()

	-- Collect all the mods that will be applied in this frame into one string.
	-- Mod tweening doesn't work correctly if the mods are in seperate commands.
	local mods_this_frame= {}
	local function add_mod(mod_str)
		mods_this_frame[#mods_this_frame+1]= mod_str
	end
	local function execute_mods()
		if #mods_this_frame <= 0 then return end
		local total_mod_str= ""
		for i, ms in ipairs(mods_this_frame) do
			if #total_mod_str > 0 then
				total_mod_str= total_mod_str .. ", "
			end
			total_mod_str= total_mod_str .. ms
		end
		Handler_mod(total_mod_str)
	end
	
	while curmod<= #mods and GAMESTATE:GetSongBeat()>=mods[curmod][1] do
		add_mod(mods[curmod][2])
		curmod = curmod+1
	end

	execute_mods()
	
	while curmessage<= #messages and GAMESTATE:GetSongBeat()>=messages[curmessage][1] do
		if messages[curmessage][3] and GAMESTATE:GetSongBeat()>=messages[curmessage][1]+5 then
			curmessage = curmessage+1;
		else
			MESSAGEMAN:Broadcast(messages[curmessage][2])
			curmessage = curmessage+1;
		end
	end
end
local last_score_x = 0
local musicrate = GAMESTATE:GetSongOptionsObject("ModsLevel_Song"):MusicRate()
return Def.ActorFrame{

	OnCommand= function(self)
			Handler_init()
			self:SetUpdateFunction(Handler_update)
			local screen = SCREENMAN:GetTopScreen()
			if isEditMode() then 
				return;
			end
			-- Fade out everything but the SongBackground, SongForeground, and Player layers.
			-- In Simply Love (as of v4.8.5 anyway), player combos will unhide themselves,
			-- but that's okay for this stepchart.  If you need to hide combos in SL, call
			-- hibernate(math.huge) on each.  Sorry about that.
			-- don't fade out the theme's UI in EditMode
			-- Below was originally snipped From Watermelon by quietly-turning
			for name,layer in pairs(screen:GetChildren()) do
				if not (name=="SongBackground" or name=="SongForeground" or name=="Underlay" or name=="PlayerP1" or name=="PlayerP2") then
					layer:smooth(1.5):diffusealpha(0)
				end
				if (name=="SongBackground") then
					layer:visible(true)
				end
				if name == "In" then layer:smooth(1.5):visible(false) end
				if name == 'Underlay' then
					layer:smooth(1.5):visible(false)
					for player_index=1,2 do
						local score = layer:GetChild('P' .. player_index .. 'Score')					
						if score then score:visible(true):diffusealpha(1) end
					end
				end
			end

		end,
	Def.Quad{
		Name= "I may be sleeping, but I preserve the world.",
		InitCommand= cmd(visible,false),
		OnCommand= cmd(sleep,1000),
      StopMessageCommand= function(self)
        if P1 then
          P1:stopeffect()
        end
        if P2 then
          P2:stopeffect()
        end
      end,
      Vibrate1MessageCommand= function(self)
        if P1 then
          P1:vibrate():effectmagnitude(1,1,0)
        end
        if P2 then
          P2:vibrate():effectmagnitude(-1,-1,0)
        end
      end,
      Vibrate3MessageCommand= function(self)
        if P1 then
          P1:vibrate():effectmagnitude(3,3,0)
        end
        if P2 then
          P2:vibrate():effectmagnitude(-3,-3,0)
        end
      end,
      Vibrate5MessageCommand= function(self)
        if P1 then
          P1:vibrate():effectmagnitude(5,5,0)
        end
        if P2 then
          P2:vibrate():effectmagnitude(-5,-5,0)
        end
      end,
      Vibrate10MessageCommand= function(self)
        if P1 then
          P1:vibrate():effectmagnitude(10,10,0)
        end
        if P2 then
          P2:vibrate():effectmagnitude(-10,-10,0)
        end
      end,
      Vibrate20MessageCommand= function(self)
        if P1 then
          P1:vibrate():effectmagnitude(20,20,0)
        end
        if P2 then
          P2:vibrate():effectmagnitude(-20,-20,0)
        end
      end,
      Vibrate30MessageCommand= function(self)
        if P1 then
          P1:vibrate():effectmagnitude(30,30,0)
        end
        if P2 then
          P2:vibrate():effectmagnitude(-30,-30,0)
        end
      end,
      Vibrate40MessageCommand= function(self)
        if P1 then
          P1:vibrate():effectmagnitude(40,40,0)
        end
        if P2 then
          P2:vibrate():effectmagnitude(-40,-40,0)
        end
      end,
      DiffuseAlphaMessageCommand= function(self)
				self:diffusealpha(0);
				self:linear(0.8);
				self:diffusealpha(0.2);
      end,

	},
	Def.Quad{
		OnCommand=function(self)
			if (isEditMode()) then
				self:visible(false)
			end
			self:FullScreen():Center()
			self:diffuse(0,0,0,0.2)
		end,
	},
	Def.ActorProxy {
		OnCommand = function(self)
			self:queuecommand('Set')
		end,
		SetCommand = function(self)
			if P1Score and not isEditMode() then
				self:SetTarget(P1Score)
			end
		end
	},
	Def.ActorProxy {
		OnCommand = function(self)
			self:queuecommand('Set')
		end,
		SetCommand = function(self)
			if P2Score and not isEditMode() then
				self:SetTarget(P2Score)
			end
		end
	},
	Def.ActorProxy {
		OnCommand = function(self)
			self:queuecommand('Set')
		end,
		SetCommand = function(self)
			if P1 and not isEditMode() then
				self:SetTarget(P1)
			end
		end
	},

	Def.ActorProxy {
		OnCommand = function(self)
			self:queuecommand('Set')
		end,
		SetCommand = function(self)
			if P2 and not isEditMode() then
				self:SetTarget(P2)
			end
		end
	},

	
}