-- zomgs it's difficultymeterDX

-- it goes like this:
--[[
[01][02][03][04][05]
[06][07][08][09][10]

If something isn't there, don't show it.
[01][02][03][04][05]
[06][07]
]]

--[[ configuration ]]
local maxDisplayTicks = 10;		-- number of ticks to display
local imgTick = "difficultystar";
local tickHeight = 26; -- hardcoded :(

--[[ start code proper ]]
local Player = ...
assert(Player);

local Meter = Def.ActorFrame{};

-- draw the active items
local StepsOrTrail = nil;
local meter = nil;

local tickWidth = 0;	-- to be filled in later by first run
local tickHeight = 0;	-- to be filled in later by first run

local dummyMeter = {};
for i=1,maxDisplayTicks do
	dummyMeter[#dummyMeter+1] = LoadActor(imgTick)..{
		Name="FilledTick"..i;
		BeginCommand=function(self)
			self:visible(false);

			if tickHeight == 0 then
				tickHeight = self:GetHeight()-6;
			end;
			if tickWidth == 0 then
				tickWidth = self:GetWidth()-5;
			end;

			-- the way this works is:
			-- 1-5  is on line 1
			-- 6-10 is on line 2
			if i < 6 then
				-- row 1
				self:y( tickHeight );
				self:x( tickWidth * i );
			else
				-- row 2
				self:y( tickHeight * 2 );
				self:x( tickWidth * (i-5) );
			end;
		end;
	};
end;

Meter[#Meter+1] = Def.ActorFrame{
	Name="MeterFill";
	OnCommand=cmd(playcommand,"Set");
	SetCommand=function(self)
		self:visible(GAMESTATE:IsHumanPlayer(Player))

		local SongOrCourse = nil;
		if GAMESTATE:IsCourseMode() then
			StepsOrTrail = GAMESTATE:GetCurrentTrail(Player);
			SongOrCourse = GAMESTATE:GetCurrentCourse();
		else
			StepsOrTrail = GAMESTATE:GetCurrentSteps(Player);
			SongOrCourse = GAMESTATE:GetCurrentSong();
		end;

		if SongOrCourse then
			if StepsOrTrail then
				local meter = StepsOrTrail:GetMeter();

				for i=1,maxDisplayTicks do
					local child = self:GetChild("FilledTick"..i);

					if i <= meter then child:visible(true);
					else child:visible(false);
					end;
				end;
			else
				for i=1,maxDisplayTicks do	
					local child = self:GetChild("FilledTick"..i);
					child:visible(false);
				end;
			end;
		else
			for i=1,maxDisplayTicks do
				local child = self:GetChild("FilledTick"..i);
				child:visible(false);
			end;
		end;
	end;

	children = dummyMeter;

	CurrentSongChangedMessageCommand=cmd(playcommand,"Set");
	CurrentStepsP1ChangedMessageCommand=cmd(playcommand,"Set");
	CurrentTrailP1ChangedMessageCommand=cmd(playcommand,"Set");
	CurrentStepsP2ChangedMessageCommand=cmd(playcommand,"Set");
	CurrentTrailP2ChangedMessageCommand=cmd(playcommand,"Set");
};

return Meter;

-- please don't hurt 'em dj hammer