function addToolbarMapButton(tb, mapButtonName, NameValueArgs)
%addToolbarMapButton Add map button to toolbar
%
%   addToolbarMapButton(TB) adds all map buttons to the toolbar specified
%   by TB. TB is an axes toolbar in a geographic axes inside a figure
%   created with the uifigure function and can be an array.
%
%   addToolbarMapButton(TB,mapButtonName) adds the map button specified by
%   mapButtonName. mapButtonName is a string scalar or character vector.
%   Add a basemap picker button by specifying mapButtonName as "basemap".
%
%   addToolbarMapButton(TB,"basemap","BasemapNames",basemapNames) specifies
%   the names of the basemaps to include in the basemap picker.
%   basemapNames is either a string array or cell array of character
%   vectors.
%
%   addToolbarMapButton(TB,"basemap","BasemapIcons",basemapIcons) specifies
%   the file names for the icons of the basemaps to include in the basemap
%   picker. basemapIcons is either a string array or cell array of
%   character vectors.
%
%   addToolbarMapButton(TB,"basemap","DisplayNames",displayNames) specifies
%   the names for the labels of the basemaps to include in the basemap
%   picker. displayNames is either a string array or cell array of
%   character vectors.
%
%   Example
%   -------
%   % Add a basemap picker to the geographic axes toolbar.
%   fig = uifigure;
%   gx = geoaxes(fig);
%   hold(gx,"on")
%   tb = axtoolbar(gx,"default");
%   addToolbarMapButton(tb)
%
%   See also geoaxes, removeToolbarMapButton, uifigure

% Copyright 2021 The MathWorks, Inc.

    arguments
        tb matlab.ui.controls.AxesToolbar
        mapButtonName map.graphics.datatype.MapButtonName = map.graphics.datatype.MapButtonName.empty

        % Basemap picker button
        NameValueArgs.BasemapNames string
        NameValueArgs.DisplayNames string
        NameValueArgs.BasemapIcons string
        NameValueArgs.NumColumns (1,1) double
    end
    
    try
        addMapButtonNameToToolbar(tb, mapButtonName, NameValueArgs)
    catch e
        throw(e)
    end
end


function  addMapButtonNameToToolbar(tb, mapButtonName, NameValueArgs)
% Process the inputs and add the map button name to toolbar.

    nameValueCell = namedargs2cell(NameValueArgs);
    switch string(mapButtonName)
        case "basemap"
            map.internal.basemaps.addBasemapPickerToolbarButton(tb, nameValueCell{:})
        otherwise
            if isempty(fieldnames(NameValueArgs))
                % all toolbar buttons.
                map.internal.basemaps.addBasemapPickerToolbarButton(tb)
            else
                % all
                % Name-Value pairs are not allowed without mapButtonName
                % Issue error:
                % Invalid argument at position 2: Value must be 'basemap'
                checkMapButtonNameSyntax(tb, NameValueArgs)
            end
    end
end


function checkMapButtonNameSyntax(tb, mapButtonName, NameValueArgs)
% Function used to generate error message:
% Invalid argument at position 2: Value must be 
%   <names from map.graphics.datatype.MapButtonName>
    arguments
            tb matlab.ui.controls.AxesToolbar {mustBeNonempty} %#ok<*INUSA>
            mapButtonName map.graphics.datatype.MapButtonName
            NameValueArgs.AnyName
    end
end
