--[[
GameGUI.Settings.Queue.MouseButton1Click:connect(function()
	BonusRoundFrame.Visible = not BonusRoundFrame.Visible;
end)]]


local BuyingIndex;
for _,Frame in pairs(BonusRoundFrame.BuyMode:GetChildren()) do
	Frame.Buy.MouseButton1Click:connect(function()
		game.ReplicatedStorage.BuyMode:FireServer(Frame.Name,BuyingIndex);
	end)
end

game.ReplicatedStorage.BuyMode.OnClientEvent:connect(function(Index,GameMode)
	local ModeFrame = BonusRoundFrame.Title.Container.ModeContainer["Mode"..Index];
	ModeFrame.Icon.Image = Icons[GameMode] or "";
	ModeFrame.ModeName.Text = "";
	ModeFrame.QMark.Visible = false;
	local Overhead = BonusRoundFrame.Title.Container.OverheadContainer["Mode"..Index];
	Overhead.Buy.Visible = false;
	Overhead.Purchased.Visible = true;
end)


local function UpdateQueue(BonusRoundQueue,CurrentMode,RandomM,StartVoting)
	BonusRoundFrame.Title.Container.ModeContainer:ClearAllChildren();
	BonusRoundFrame.Title.Container.OverheadContainer:ClearAllChildren();
	for Index,GameModeName in pairs(BonusRoundQueue) do
		local ModeFrame = script.GameMode:Clone();
		ModeFrame.QMark.Visible = (GameModeName=="Locked"or GameModeName=="Random");
		ModeFrame.ModeName.Text = (GameModeName=="Locked"or GameModeName=="Random") and "Random" or "";
		ModeFrame.Locked.Visible = (GameModeName=="Locked");
		ModeFrame.Icon.Image = Icons[GameModeName] or "";
		if Index==1 then
			ModeFrame.BackgroundColor3 = Color3.new(40/255,40/255,40/255);
			ModeFrame.BorderColor3 = Color3.new(25/255,25/255,25/255);
			ModeFrame.ModeName.TextColor3 = Color3.new(59/255,59/255,59/255);
			ModeFrame.QMark.TextColor3 = Color3.new(59/255,59/255,59/255);
			ModeFrame.Icon.Image = BWIcons[GameModeName]or"";
		end
		ModeFrame.Position = UDim2.new(0,(Index-1)*(76+7),0,4);
		ModeFrame.Name = "Mode"..Index;
		ModeFrame.Parent = BonusRoundFrame.Title.Container.ModeContainer;
		
		local OverheadFrame = script.Overhead:Clone();
		local NextUp = (Index==2);		
		local Buy = (GameModeName=="Random");
		local Purchased = (GameModeName~="Random"and GameModeName~="Locked");
		local Buyable = (Buy and not NextUp);
		OverheadFrame.Name = "Mode"..Index;
		
		OverheadFrame.Buy.Visible = Buyable;
		if Buyable then
			OverheadFrame.Buy.MouseButton1Click:connect(function()
				if BuyingIndex ~= Index then
					BonusRoundFrame.BuyMode.Visible = true;
					BonusRoundFrame.Loading.Visible = false;
					BonusRoundFrame.Voting.Visible = false;
					BonusRoundFrame.Nothing.Visible = false;
					BuyingIndex = Index;

					for _,OV in pairs(BonusRoundFrame.Title.Container.OverheadContainer:GetChildren()) do
						OV.Buy.Style = (OV==OverheadFrame and Enum.ButtonStyle.RobloxRoundButton) or Enum.ButtonStyle.RobloxRoundDefaultButton;
					end
					
				else
					BuyingIndex = nil;
					OverheadFrame.Buy.Style = Enum.ButtonStyle.RobloxRoundDefaultButton;
					BonusRoundFrame.BuyMode.Visible = false;
					BonusRoundFrame.Loading.Visible = Stage=="Loading";
					BonusRoundFrame.Voting.Visible = Stage=="Voting";
					BonusRoundFrame.Nothing.Visible = Stage=="Nothing";
				end;
				-- game.ReplicatedStorage.BuyMode:FireServer(Index);
			end)
		end

		OverheadFrame.Locked.Visible = (GameModeName=="Locked" and not NextUp);
		OverheadFrame.Next.Visible = NextUp;
		OverheadFrame.Purchased.Visible = Purchased and not NextUp;
		OverheadFrame.Position = UDim2.new(0,(Index-1)*(76+7),0,-2);
		OverheadFrame.Parent = BonusRoundFrame.Title.Container.OverheadContainer;
			
	end
	
	if StartVoting then
		BonusRoundFrame.Nothing.Visible = false;
		if RandomM then
			RandomModes = RandomM;
			BonusRoundFrame.Voting.Visible = true;
			Stage = "Voting";
		else
			RandomModes = {};
			BonusRoundFrame.Voting.Visible = false;
			BonusRoundFrame.Loading.Visible = true;
			Stage = "Loading";
			BonusRoundFrame.Loading[CurrentMode].Visible = true;
		end;
		UpdateVotes();
	end;
	
end

game.ReplicatedStorage.VoteBonusRound.OnClientEvent:connect(function(BonusRoundQueue,CurrentMode,RandomM)
	UpdateQueue(BonusRoundQueue,CurrentMode,RandomM,true);
	BonusRoundFrame.Visible = true;
end)

local BonusRoundQueue = game.ReplicatedStorage.GetQueue:InvokeServer();
UpdateQueue(BonusRoundQueue);

