function user_menu(par)

% user_menu - create users menu
% -----------------------------
%
% user_menu(par)
%
% Input:
% ------
%  par - menu parent

% NOTE: consider using a single 'File' menu on the XBAT palette

return;

%----------------------
% HANDLE INPUT
%----------------------

%----------------------
% SETUP
%----------------------

%--
% clear former menu if needed
%--

top = findobj(par, 'type', 'uimenu', 'tag', 'TOP_USER_MENU');

if ~isempty(top)
	delete(allchild(top));
end

%----------------------
% CREATE MENUS
%----------------------

%--
% create top menu if needed
%--

if isempty(top)
	top = uimenu(par, 'label', 'User', 'tag', 'TOP_USER_MENU');
end 

%--
% get users and active user
%--

users = get_users; names = {users.name}; active = get_active_user;

%--
% create command menus
%--

uimenu(top, ...
	'label', 'New ...', ...
	'callback', @new_user_callback ...
);

if strcmpi(active.name, 'default')
	
	uimenu(top, 'enable', 'off', 'label', 'Edit ...');

else
	uimenu(top, ...
		'label', 'Edit ...', ...
		'callback', @edit_active_user_callback ...
	);
end

%--
% create user menus
%--

if isempty(names)
	
	% NOTE: this should never happen, there is always a default user
	
	uimenu(top, ...
		'label', '(No Users Found)', ... 
		'enable', 'off', ...
		'separator', 'on' ...
	);

else

	named = [];

	for k = 1:length(names)
		
		named(end + 1) = uimenu(top, ...
			'label', names{k}, ...
			'check', bin2str(strcmp(names{k}, active.name)), ...
			'callback', @set_active_user_callback ...
		);
	
	end

	set(named(1), 'separator', 'on');
	
	uimenu(top, ...
		'label', 'Show Files ...', ...
		'separator', 'on', ...
		'callback', @show_files_callback ...
	);

end


%--------------------------------------
% NEW_USER_CALLBACK
%--------------------------------------

function new_user_callback(obj, eventdata)

%--
% create new user
%--

user = new_user_dialog;

%--
% rebuild user menu if needed
%--

% NOTE: this may not be needed

if ~isempty(user)
	user_menu(ancestor(obj, 'figure'));
end

%--------------------------------------
% EDIT_USER_CALLBACK
%--------------------------------------

function edit_active_user_callback(obj, eventdata)

new_user_dialog(get_active_user);


%--------------------------------------
% SET_ACTIVE_USER_CALLBACK
%--------------------------------------

function set_active_user_callback(obj, eventdata)

% TODO: turn off 'Edit ...' user menu

%--
% get user by name
%--

user = get_users('name', get(obj, 'label'));

% TODO: this should refresh the user list

if isempty(user)
	return;
end

%--
% set user as active user
%---

set_user_control(user);


%--------------------------------------
% SHOW_FILES_CALLBACK
%--------------------------------------

function show_files_callback(obj, eventdata)

show_file(users_root);


%--------------------------------------
% SET_USER_CONTROL
%--------------------------------------

function set_user_control(user)

% TODO: consider how to centralize this type of code

%--
% set user control and perform update
%--

pal = xbat_palette;

set_control(pal, 'User', 'value', user.name); 

xbat_palette('User');
