function control = xbat_palette_controls(user, size)

% xbat_palette_controls - create xbat palette control layout
% ----------------------------------------------------------
%
% control = xbat_palette_controls(user, mode)
%
% Input:
% ------
%  user - user (def: get_active_user)
%  size - palette size (def: get_env('palette_size')) 

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

if nargin < 2 || isempty(size)
	size = get_env('palette_size');
end

if nargin < 1 || isempty(user)
	user = get_active_user;
end

%-----------------------------------
% SET PARAMETERS BASED ON SIZE
%-----------------------------------

switch size
	
	case 'smallest'
		
		list_lines = 2;
		
	case 'smaller'
		
		list_lines = 3;
		
	otherwise
		
		list_lines = 4;
		
end


%-----------------------------------
% DEFINE CONTROLS USING PARAMETERS
%-----------------------------------

control = empty(control_create);

%--------------------
% Source
%--------------------

control(end + 1) = control_create( ...
	'string','Source', ...
	'style','separator', ...
	'type','header', ...
	'space', 0.75 ...
);

%--------------------
% User
%--------------------

%--
% create user name list
%--

L = struct_field(get_users, 'name');

% NOTE: put default user on top

ix = find(strcmp('Default', L));

if ~isempty(ix)
	L(ix) = []; L = {'Default', L{:}};
end

ix = find(strcmp(user.name, L));

%--
% create control
%--

control(end + 1) = control_create( ...
	'name', 'User', ...
	'tooltip', ['Set active user'], ...
	'style', 'popup', ...
	'callback', '', ...
	'lines', 1, ...
	'space', 0.75, ...
	'string', L, ...
	'value', ix ...
);

control(end + 1) = control_create( ...
	'name', {'add_user', 'edit_user'}, ...
	'alias', {'New ...', 'Edit ...'}, ...
	'tooltip', {'Create new user', 'Edit user information and preferences'}, ...
	'width', 2/3, ...
	'align', 'right', ...
	'style', 'buttongroup', ...
	'space', 0, ...
	'lines', 1.75 ...
);

%--------------------
% Library
%--------------------

%--
% create user libraries list
%--

if isempty(user.library)
	L = {};
else
	L = library_name_list(user);
end

ix = user.active;

%--
% create control
%--

control(end + 1) = control_create( ...
	'name', 'Library', ...
	'tooltip', ['Set active library'], ...
	'style', 'popup', ...
	'callback', '', ...
	'lines', 1, ...
	'space', 0.75, ...
	'string', L, ...
	'value', ix ...
);

%--------------------
% Add and remove
%--------------------

control(end + 1) = control_create( ...
	'name',{'new_library', 'subscribe_user', 'unsubscribe_user'}, ...
	'alias', {'New ...', 'Add ...', 'Remove'}, ...
	'tooltip',{'Create a new library', 'Subscribe to a library', 'Unsubscribe from current library'}, ...
	'width', 1, ...
	'align','right', ...
	'style','buttongroup', ...
	'space', 1.25, ...
	'lines',1.75 ...
);

%--------------------
% Sounds
%--------------------

control(end + 1) = control_create( ...
	'string','Sounds', ...
	'style','separator', ...
	'type','header', ...
	'space',0.5 ...
);

%--------------------
% Find Sounds
%--------------------

control(end + 1) = control_create( ...
	'name','find_sounds', ...
	'alias','Search', ...
	'space',0.75, ...
	'color',ones(1,3), ...
	'style','edit' ...
);

% NOTE: in 'itunes', search has no label and tooltip says 'find songs quickly ...'

%--------------------
% Sounds
%--------------------

control(end + 1) = control_create( ...
	'name','Sounds', ...
	'style','listbox', ...
	'lines',list_lines, ...
	'space',0.75, ...
	'min',0, ...
	'max',2 ...
);

%--------------------
% Add File ... Add Folder ...
%--------------------

control(end + 1) = control_create( ...
	'name', {'Add File ...', 'Add Folder ...'}, ...
	'tooltip', {'Add single sound to library', 'Search folder for sounds and add to library'}, ...
	'style', 'buttongroup', ...
	'space',0.75, ...
	'lines', 1.75 ...
);

%--------------------
% Acquire Sound ...
%--------------------

% control(end + 1) = control_create( ...
% 	'name', 'Acquire ...', ...
% 	'tooltip', 'Record new sound', ...
% 	'style', 'buttongroup', ...
% 	'space',1.25, ...
% 	'lines',1.75 ...
% );


%--------------------
% Sound Info
%--------------------

% control(end + 1) = control_create( ...
% 	'string','Sound Info', ...
% 	'style','separator', ...
% 	'space', 1.25, ...
% 	'align','right' ...
% );

control(end + 1) = control_create( ...
	'name', 'sound_info', ...
	'alias','info', ...
	'style', 'listbox', ...
	'label', 1, ...
	'space', 1.25, ...
	'min', 0, ...
	'max', 2, ...
	'lines', 3 ...
);

%--------------------
% Logs
%--------------------

control(end + 1) = control_create( ...
	'string', 'Logs', ...
	'style', 'separator', ...
	'type', 'header', ...
	'space', 0.5 ...
);

%--------------------
% Find Logs
%--------------------

control(end + 1) = control_create( ...
	'name', 'find_logs', ...
	'alias', 'Search', ...
	'space', 0.75, ...
	'color', ones(1,3), ...
	'style', 'edit' ...
);

%--------------------
% Logs
%--------------------

control(end + 1) = control_create( ...
	'name', 'Logs', ...
	'style', 'listbox', ...
	'lines', list_lines, ...
	'space',0.75, ...
	'min', 0, ...
	'max', 2 ...
);

%--------------------
% Log Info
%--------------------

% control(end + 1) = control_create( ...
% 	'string', 'Log Info', ...
% 	'style', 'separator', ...
% 	'space', 1.25, ...
% 	'align', 'right' ...
% );

control(end + 1) = control_create( ...
	'name', 'log_info', ...
	'alias', 'info', ...
	'style', 'listbox', ...
	'label', 1, ...
	'space', 0, ...
	'min', 0, ...
	'max', 2, ...
	'lines', 3 ...
);

	