-- For the sake of organization, I am placing chat commands such as flagging in this file.
-- They tend to be larger and more complex and make the chat.lua file rather bloated feeling.

function openHelp( ply, text )

	umsg.Start("openHelpMenu",ply);
	umsg.End();

end
CAKE.AddChatCommand("/help", openHelp);

function openAdmin( ply, text )

	if( ply:IsAdmin() or ply:IsSuperAdmin() ) then
	
		umsg.Start("openAdminMenu",ply);
		umsg.End();
	
	end

end
CAKE.AddChatCommand("/admin", openAdmin);

-- This function attempts to flag a person for the NSRF.
-- It checks if they are NSRF or NSRF-leadership. And activates their model picking menu if true,
-- which would call their weapon loadout.
function MIChat(ply, text)

	if ( ply:GetField("flags") != nil and table.HasValue(ply:GetField("flags"), "E" ) ) then	
		ply:ConCommand( "rp_flag mi" )
		ply:SetNWInt("flaggedmi", 1)
		--ply:ConCommand("rp_flag NSRFL");
		umsg.Start("ModelSelect",ply);
		umsg.End();

	elseif ( ply:GetField("flags") != nil and table.HasValue(ply:GetField("flags"), "mi" ) ) then 
		
		ply:ConCommand( "rp_flag mi" )
		ply:SetNWInt("flaggedmi", 1)
		umsg.Start("ModelSelect", ply);
		umsg.String("NSRF");
		umsg.String("NSRF");
		umsg.End();
		
	else
		CAKE.Notify(ply, " You are not part of the Mobile Infantry");
	end
	
	return "";
end
--CAKE.AddChatCommand("/mi", MIChat);



function MIPromote(ply, text)

	if ( ply:GetField("flags") != nil and table.HasValue(ply:GetField("flags"), "E" )) then
	
			local args = string.Explode(" ", text);
	
			local target = CAKE.SearchForPlayer(1, args[1]); -- checking by IC name I think
			
			if( target == nil ) then
				target = CAKE.SearchForPlayer(2, args[1]);  -- maybe it was steamid
			end
	
			if(target != nil and target:IsPlayer()) then -- Target found, is player
	
				local flagtable = target:GetField("flags");
			
				-- If already NSRF don't flag it again.
				if( table.HasValue(flagtable, "mi") ) then
					return "";
				end
			
				table.insert(flagtable, "mi");
				target:SetField("flags", flagtable, true);
			
				CAKE.Notify(target, " You have been recruited to the Mobile Infantry by: " .. ply:Nick() .. "!");
				CAKE.Notify(ply, " Recruited " .. target:Nick() .. " to the Mobile Infantry!");
				CAKE.DayLog(ply:Name() .. " [" .. ply:SteamID() .. "] recruited " .. target:Name() .. " [" .. target:SteamID() .. "] to the Mobile Infantry.");
				return "";
		
			elseif(target == nil) then -- Target was not found
	
				CAKE.Notify(ply, "Cannot find target!");
				return "";
		
			elseif(target == "err") then -- More than one player of the same name
	
				CAKE.Notify(ply, "Multiple targets selected!");
				return "";
		
			end
	

	else
		CAKE.Notify(ply, "You are not part of the Command Circle.");
	end 
		
	return "";
	
end
--CAKE.AddChatCommand("/recruitnsrf", NSRFPromote); -- conflicts with /r
--CAKE.AddChatCommand("/makemi", MIPromote);

function ccQualify(ply, text)
	if ( ply:GetField("flags") != nil and table.HasValue(ply:GetField("flags"), "E" )) then
	
			local args = string.Explode(" ", text);
	
			local target = CAKE.SearchForPlayer(1, args[1]); -- checking by IC name I think
			
			if( target == nil ) then
				target = CAKE.SearchForPlayer(2, args[1]);  -- maybe it was steamid
			end
	
			if(target != nil and target:IsPlayer()) then -- Target found, is player
	
				local flagtable1 = target:GetField("flags");
			
				table.insert(flagtable1, "".. args[2] .. "");
				target:SetField("flags", flagtable1, true);
			
				CAKE.Notify(target, " You have been qualified for " .. args[2] .. " by: " .. ply:Nick() .. "!");
				CAKE.Notify(ply, " Qualified " .. target:Nick() .. " for " .. args[2] .. "!");
				CAKE.DayLog(ply:Name() .. " [" .. ply:SteamID() .. "] recruited " .. target:Name() .. " [" .. target:SteamID() .. "] to the Command Circle.");
				return "";
		
			elseif(target == nil) then -- Target was not found
	
				CAKE.Notify(ply, "Cannot find target!");
				return "";
		
			elseif(target == "err") then -- More than one player of the same name
	
				CAKE.Notify(ply, "Multiple targets selected!")
				return "";
		
			end
	

	else
		CAKE.Notify(ply, "You are not part of the Command Circle.");
	end 
		
	return "";
	
end
--CAKE.AddChatCommand("/qualify", ccQualify)

function CCCitizen(ply, text)
	ply:ConCommand("say !flag citizen")
	ply:SetNWInt("flaggedmi", 0)
end
--CAKE.AddChatcommand("/citizen", CCCitizen)

function CCPromote(ply, text)

	if ( ply:GetField("flags") != nil and table.HasValue(ply:GetField("flags"), "E" )) then
	
			local args = string.Explode(" ", text);
	
			local target = CAKE.SearchForPlayer(1, args[1]); -- checking by IC name I think
			
			if( target == nil ) then
				target = CAKE.SearchForPlayer(2, args[1]);  -- maybe it was steamid
			end
	
			if(target != nil and target:IsPlayer()) then -- Target found, is player
	
				local flagtable = target:GetField("flags");
			
				-- If already NSRF don't flag it again.
				if( table.HasValue(flagtable, "E") ) then
					return "";
				end
			
				table.insert(flagtable, "E");
				target:SetField("flags", flagtable, true);
			
				CAKE.Notify(target, " You have been recruited to the Command Circle by: " .. ply:Nick() .. "!");
				CAKE.Notify(ply, " Recruited " .. target:Nick() .. " to the Command Circle!");
				CAKE.DayLog(ply:Name() .. " [" .. ply:SteamID() .. "] recruited " .. target:Name() .. " [" .. target:SteamID() .. "] to the Command Circle.");
				return "";
		
			elseif(target == nil) then -- Target was not found
	
				CAKE.Notify(ply, "Cannot find target!");
				return "";
		
			elseif(target == "err") then -- More than one player of the same name
	
				CAKE.Notify(ply, "Multiple targets selected!");
				return "";
		
			end
	

	else
		CAKE.Notify(ply, "You are not part of the Command Circle.");
	end 
		
	return "";
	
end
--CAKE.AddChatCommand("/recruitnsrf", NSRFPromote); -- conflicts with /r
--CAKE.AddChatCommand("/makecommand", CCPromote);