-- pick the right player or DIE!!
local Player = ...;
assert(Player);

local difficultyFrames = {
	Difficulty_Beginner = 0,
	Difficulty_Easy = 1,
	Difficulty_Medium = 2,
	Difficulty_Hard = 3,
	Difficulty_Challenge = 4,
	Difficulty_Edit = 5,
};

local function set(self, player)
	if player and player ~= Player then return end
	local SongOrCourse = nil;
	local StepsOrTrail = nil;
	if GAMESTATE:IsCourseMode() then
		SongOrCourse = GAMESTATE:GetCurrentCourse();
		StepsOrTrail = GAMESTATE:GetCurrentTrail(player);
	else
		SongOrCourse = GAMESTATE:GetCurrentSong();
		StepsOrTrail = GAMESTATE:GetCurrentSteps(player);
	end;
	if SongOrCourse and StepsOrTrail then
		local dc = StepsOrTrail:GetDifficulty()
		self:SetFromDifficulty( dc );
	else
		self:Unset();
	end

	--[[
	if GAMESTATE:IsCourseMode() then
		self:SetFromTrail(GAMESTATE:GetCurrentTrail(player));
	else
		self:SetFromSteps(GAMESTATE:GetCurrentSteps(player));
	end;
	--]]
end

local t = Def.ActorFrame{
	BeginCommand=cmd(player,Player);
	PlayerJoinedMessageCommand=function(self,param)
		if param.Player == Player then
			self:visible(true);
		end;
	end;
	PlayerUnjoinedMessageCommand=function(self,param)
		if param.Player == Player then
			self:visible(false);
		end;
	end;

	Def.DifficultyIcon {
		File=THEME:GetPathG("ScreenSelectMusic","DifficultyIcon/_icons");
		InitCommand=function(self)
			self:SetPlayer( Player );
			self:hide_if(not GAMESTATE:IsHumanPlayer(Player));
		end;
		PlayerJoinedMessageCommand=function( self, param )
			if param.Player ~= Player then return end;
			self:visible( true );
			self:playcommand("Init");
		end;
		OnCommand=function(self) set(self, Player); end;
	};
	LoadFont("SortDisplay")..{
		InitCommand=cmd(shadowlength,0;y,-8);
		BeginCommand=function(self)
			local xOffset = 31;
			self:x( Player == PLAYER_1 and -xOffset-2 or xOffset );
		end;
		OnCommand=cmd(playcommand,"Set";);
		SetCommand=function(self)
			local song = GAMESTATE:GetCurrentSong();
			local text = "";
			if song then
				local steps = GAMESTATE:GetCurrentSteps(Player);
				if steps then
					text = steps:IsAutogen() and "AG" or "";
				else
					text = "";
				end;
			else
				text = "";
			end;
			self:settext(text);
		end;
	};
};

return t;