-- 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 frameXOffset = Player == PLAYER_2 and -16 or 16;

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;
	LoadActor("_frame "..ToEnumShortString(Player))..{
		InitCommand=cmd(x,frameXOffset);
	};

	Def.DifficultyIcon {
		File="_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;
		CurrentSongChangedMessageCommand=function(self) set(self, Player); end;
		CurrentCourseChangedMessageCommand=function(self) set(self, Player); end;
		CurrentStepsP1ChangedMessageCommand=function(self) set(self, PLAYER_1); end;
		CurrentStepsP2ChangedMessageCommand=function(self) set(self, PLAYER_2); end;
		CurrentTrailP1ChangedMessageCommand=function(self) set(self, PLAYER_1); end;
		CurrentTrailP2ChangedMessageCommand=function(self) set(self, PLAYER_2); end;
	};
	-- it was either SortDisplay or a shared font.
	LoadFont("SortDisplay")..{
		InitCommand=cmd(zoom,1.5;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";diffuseshift;effectcolor1,color("#FFFFFFFF");effectcolor2,color("#FFFFFF00"));
		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;
		CurrentSongChangedMessageCommand=cmd(playcommand,"Set");
		CurrentStepsP1ChangedMessageCommand=function(self) if Player == PLAYER_1 then self:playcommand("Set") end; end;
		CurrentStepsP2ChangedMessageCommand=function(self) if Player == PLAYER_2 then self:playcommand("Set") end; end;
		-- can trails even be autogen
		CurrentTrailP1ChangedMessageCommand=function(self) if Player == PLAYER_1 then self:playcommand("Set") end; end;
		CurrentTrailP2ChangedMessageCommand=function(self) if Player == PLAYER_2 then self:playcommand("Set") end; end;
	};

	-- old
	--[[
	LoadActor("_icons")..{
		InitCommand=cmd(pause;);
		BeginCommand=function(self)
			-- player check
			self:visible(GAMESTATE:IsHumanPlayer(Player));
		end;
		OnCommand=cmd(playcommand,"Set");
		PlayerJoinedMessageCommand=function(self,param)
			if param.Player == Player then
				self:visible(true);
				self:playcommand("Set");
			end;
		end;
		PlayerUnjoinedMessageCommand=function(self,param)
			if param.Player == Player then self:visible(false); end;
		end;
		SetCommand=function(self)
			-- song/course check
			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 == nil or StepsOrTrail == nil then
				self:visible(false);
				return;
			end;

			-- we should be good by now...
			if SongOrCourse then
				if StepsOrTrail then
					local difficulty = StepsOrTrail:GetDifficulty();
					self:setstate( difficultyFrames[difficulty] );
				end;
			end;
		end;
		SongChangedMessageCommand=cmd(playcommand,"Set");
		CurrentStepsP1ChangedMessageCommand=function(self)
			if Player == PLAYER_1 then self:playcommand("Set"); end;
		end;
		CurrentTrailP1ChangedMessageCommand=function(self)
			if Player == PLAYER_1 then self:playcommand("Set"); end;
		end;
		CurrentStepsP2ChangedMessageCommand=function(self)
			if Player == PLAYER_2 then self:playcommand("Set"); end;
		end;
		CurrentTrailP2ChangedMessageCommand=function(self)
			if Player == PLAYER_2 then self:playcommand("Set"); end;
		end;
	};
	]]
};

return t;