if( BlackmarketMenu ) then
	BlackmarketMenu:Remove();
end
BlackmarketMenu = nil;

BlackmarketIconGrid = { }
BlackmarketW = 0;
BlackmarketH = 0;
 
function RemoveAllBlackmarketIcons()

	for k, v in pairs( BlackmarketIconGrid ) do
	
		for n, m in pairs( v ) do
		
			if( BlackmarketIconGrid[k][n]:IsValid() ) then
				BlackmarketIconGrid[k][n]:Remove();
				BlackmarketIconGrid[k][n] = nil;
			end
			
		end
	
	end
	
	BlackmarketIconGrid = nil;
	BlackmarketIconGrid = { }
	
	ChosenBlackmarketItem = nil;

end

function RemoveBlackmarketButtons()

	if( not BlackmarketMenu ) then return; end

	if( BlackmarketMenu.TakeButton ) then
		BlackmarketMenu.TakeButton:Remove();
		BlackmarketMenu.TakeButton = nil;
	end

end

function ShowBlackmarketButtons()

	local function take()
		
		if( string.find( ClientVars["Flags"], "T" ) ) then
			
			RunConsoleCommand( "eng_buybmitem", ChosenBlackmarketItem.id, ChosenBlackmarketItem.x, ChosenBlackmarketItem.y );
			
		end
		
	end

	if( string.find( ClientVars["Flags"], "T" ) ) then	

		BlackmarketMenu.TakeButton = BlackmarketMenu:AddButton( "Purchase", 10, 10, take );
		
	end	

end

function CreateBlackmarketMenu( msg )

	if( BlackmarketMenu ) then
		BlackmarketMenu:Remove();
		BlackmarketMenu = nil;
	end
	
	RemoveAllBlackmarketIcons();
	RemoveBlackmarketButtons();
	
	local title = "Blackmarket"
	StorageW = 14
	StorageH = 8

	BlackmarketMenu = CreateBPanel( title, 10, 10, 720, 510 );
	BlackmarketMenu:SetBodyColor( Color( 20, 20, 20, 100 ) );
	
	BlackmarketMenu.OnClose = function()
	
		RunConsoleCommand( "eng_closebm", "" );
	
	end

	BlackmarketMenu.CanPutInto = false;
	
	BlackmarketMenu.DescriptionPanel = CreateBPanel( nil, 150, 0, 560, 70 );
	BlackmarketMenu.DescriptionPanel:SetParent( BlackmarketMenu );
	BlackmarketMenu.DescriptionPanel:SetBodyColor( Color( 0, 0, 0, 170 ) );
	
	BlackmarketMenu.ContentPanel = CreateBPanel( nil, 10, 80, 700, 400 );	
	BlackmarketMenu.ContentPanel:SetParent( BlackmarketMenu );
	BlackmarketMenu.ContentPanel:SetBodyColor( Color( 30, 30, 30, 170 ) );
	BlackmarketMenu.ContentPanel:EnableScrolling( true );
	
	BlackmarketMenu.ContentPanel.PaintHook = function()
	
		local row = StorageH - 1;
		local col = StorageW - 1;
		
		local black = false;
		local newblack = true;
		
		for y = 0, row do
		
			for x = 0, col do
			
				if( black ) then
					draw.RoundedBox( 0, x * GRIDWIDTH, y * GRIDHEIGHT, GRIDWIDTH, GRIDHEIGHT, Color( 0, 0, 0, 200 ) );
				end
				
				black = !black;
				
			end
			
			black = newblack;
			newblack = !newblack;
		
		end
	
		if( ChosenBlackmarketItem and ChosenBlackmarketItem.x > -1 ) then
		
			draw.RoundedBox( 0, ChosenBlackmarketItem.x * GRIDWIDTH, ChosenBlackmarketItem.y * GRIDHEIGHT, GRIDWIDTH * TS.ItemsData[ChosenBlackmarketItem.id].Width, GRIDHEIGHT * TS.ItemsData[ChosenBlackmarketItem.id].Height, Color( 0, 0, 128, 128 ) );
		
		end
	
	end
	
	gui.EnableScreenClicker( true );
	
end
usermessage.Hook( "CBMM", CreateBlackmarketMenu );

function InsertBlackmarketItem( msg )

	if( not BlackmarketMenu ) then return; end

	local item = msg:ReadString();
	local x = msg:ReadShort();
	local y = msg:ReadShort();

	local data = TS.ItemsData[item];
	
	if( not data ) then timer.Simple( 1, InsertBlackmarketItem, msg ); return; end
	
	local icon = vgui.Create( "DModelPanel", BlackmarketMenu.ContentPanel );
	icon:SetModel( data.Model );
	icon:SetPos( x * GRIDWIDTH, y * GRIDHEIGHT );
	icon:SetSize( data.Width * GRIDWIDTH, data.Height * GRIDHEIGHT );
	icon:SetCamPos( data.CamPos );
	icon:SetLookAt( data.LookAt );
	icon:SetFOV( data.Fov );
	
	icon.LayoutEntity = function( self )
	    

 	end
 	
 	icon.OnMousePressed = function()
 	
 		ChosenBlackmarketItem = { id = item, x = x, y = y };
 		ShowBlackmarketButtons();
		ShowBlackmarketDescription( item );
 	
 	end
 	
 	if( BlackmarketIconGrid[x] and BlackmarketIconGrid[x][y] ) then

 		if( BlackmarketIconGrid[x][y]:IsValid() ) then
 			BlackmarketIconGrid[x][y]:Remove();
 		end
 		
 		BlackmarketIconGrid[x][y] = nil;
 		
 	end
 	
 	if( not BlackmarketIconGrid[x] ) then
 		BlackmarketIconGrid[x] = { }
 	end
 	
 	BlackmarketIconGrid[x][y] = icon;


end
usermessage.Hook( "IBMI", InsertBlackmarketItem );

function RemoveBlackmarketItem( msg )

	local x = msg:ReadShort();
	local y = msg:ReadShort();

	if( ChosenBlackmarketItem and
		ChosenBlackmarketItem.x == x and
		ChosenBlackmarketItem.y == y ) then
		
		ChosenBlackmarketItem = nil;
		RemoveBlackmarketButtons();
		
	end
	
	if( BlackmarketMenu.DescriptionPanel.Icon ) then
		BlackmarketMenu.DescriptionPanel.Icon:Remove();
		BlackmarketMenu.DescriptionPanel.Icon = nil;
	end

	if( BlackmarketIconGrid[x] and BlackmarketIconGrid[x][y] ) then
	
		BlackmarketIconGrid[x][y]:Remove();
		BlackmarketIconGrid[x][y] = nil;
		
	end
	

end
usermessage.Hook( "RBMI", RemoveBlackmarketItem );

function ShowBlackmarketDescription( item )

	if( BlackmarketMenu.DescriptionPanel.Icon ) then
		BlackmarketMenu.DescriptionPanel.Icon:Remove();
		BlackmarketMenu.DescriptionPanel.Icon = nil;
	end
	
	local data = TS.ItemsData[item];
	
	BlackmarketMenu.DescriptionPanel.Icon = vgui.Create( "DModelPanel", BlackmarketMenu.DescriptionPanel );
	BlackmarketMenu.DescriptionPanel.Icon:SetModel( data.ContainsModel or data.Model );
	BlackmarketMenu.DescriptionPanel.Icon:SetPos( 2, 2 );
	BlackmarketMenu.DescriptionPanel.Icon:SetSize( math.Clamp( data.Width * GRIDWIDTH * 2, 0, BlackmarketMenu.DescriptionPanel:GetWide() ), math.Clamp( data.Height * GRIDHEIGHT * 2, 0, BlackmarketMenu.DescriptionPanel:GetTall() ) );
	BlackmarketMenu.DescriptionPanel.Icon:SetCamPos( data.CamPos );
	BlackmarketMenu.DescriptionPanel.Icon:SetLookAt( data.LookAt );
	BlackmarketMenu.DescriptionPanel.Icon:SetFOV( data.Fov );
	
	BlackmarketMenu.DescriptionPanel.Text = FormatLine( data.Desc, "NewChatFont", BlackmarketMenu.DescriptionPanel:GetWide() - math.Clamp( data.Width * GRIDWIDTH * 2, 0, BlackmarketMenu.DescriptionPanel:GetWide() ) );

	BlackmarketMenu.DescriptionPanel.PaintHook = function()
	
		draw.DrawText( data.Name, "NewChatFont", BlackmarketMenu.DescriptionPanel:GetWide() - 5, 2, Color( 255, 255, 255, 255 ), 2 );
		draw.DrawText( BlackmarketMenu.DescriptionPanel.Text, "NewChatFont", BlackmarketMenu.DescriptionPanel:GetWide() - 2, 18, Color( 255, 255, 255, 255 ), 2 );
		
	end
	
	BlackmarketMenu.DescriptionPanel.Icon.LayoutEntity = function( self ) 
	
	end
	
end

function RemoveBlackmarketMenu()
	
	if( BlackmarketMenu and BlackmarketMenu.TitleBar ) then
	
		BlackmarketMenu.TitleBar:Remove();
		BlackmarketMenu:Remove();

	end

end
usermessage.Hook( "RBMM", RemoveBlackmarketMenu );

