function control = parameter__control__create(parameter, context)

% DATA TEMPLATE - parameter__control__create

% TODO: add user to context mode to user and use to provide simple and advanced interfaces

%-----------------------------
% TEMPLATES
%-----------------------------

%--
% tags
%--

control(1) = control_create( ...
	'name', 'code', ...
	'alias', 'tags', ...
	'style', 'edit', ...
	'onload', 1 ...
);

%--
% templates
%--

control(end + 1) = control_create( ...
	'name', 'templates', ...
	'alias', 'Clip', ...
	'value', [], ...
	'label', 0, ...
	'style', 'axes', ...
	'onload', 1, ...
	'lines', 5, ...
	'space', 1.5 ...
);

%--
% template navigation
%--

% TODO: implement this as a helper

% [L,ix] = templates_info_str(parameter.templates);

L = {'(No Available Templates)'};

control(end + 1) = control_create( ...
	'name','template_select', ...
	'label',0, ...
	'style','popup', ...
	'string',L, ...
	'space',0.8, ...
	'value',1 ...
);

control(end + 1) = control_create( ...
	'style','buttongroup', ...
	'name',{'previous_template','next_template'}, ...
	'alias',{'<','>'}, ...
	'lines',1.25, ...
	'width',0.25, ...
	'space',-1.25, ...
	'value',1 ...
);

%--
% template mode
%--

L = { ...
	'Ignore', ...
	'Keep (Exclusive)', ...
	'Keep (Non-Exclusive)', ...
	'Reject', ...
};

control(end + 1) = control_create( ...
	'name','template_mode', ...
	'label',0, ...
	'style','popup', ...
	'string',L, ...
	'width',0.7, ...
	'align','right', ...
	'space',1.5, ...
	'value',2 ...
);

%--
% system interaction
%--

% NOTE: these controls affect how we add templates from selections

control(end + 1) = control_create( ...
	'align','right', ...
	'space',0.75, ...
	'style','separator' ...
);

% NOTE: the name of this control is an observer tag INTERCEPT_SELECTION_COPY

control(end + 1) = control_create( ...
	'name','INTERCEPT_SELECTION_COPY', ...
	'alias','Copy Selection', ...
	'style','checkbox', ...
	'value',1, ...
	'space',0.5, ...
	'width',0.5, ...
	'tooltip','Intercept selection on copy' ...
);

% NOTE: consider subtract as something to avoid (oblique projection)

L = { ...
	'Add', ...
	'Replace' ...
};

control(end + 1) = control_create( ...
	'name','copy_mode', ...
	'label',0, ...
	'width',0.5, ...
	'style','popup', ...
	'string',L, ...
	'space',1.25, ...
	'value',1 ...
);

%--
% Separator
%--

control(end + 1) = control_create( ...
	'align','right', ...
	'space',0.1, ...
	'style','separator' ...
);

%-----------------------------
% PARAMETERS
%-----------------------------

tabs = { ...
	'Correlation', ...
	'Mask' ...
};

control(end + 1) = control_create( ...
	'style','tabs', ...
	'label',0, ...
	'lines',1.25, ...
	'space',1.25, ...
	'tab',tabs, ...
	'name','Parameter Tabs' ...
);

control(end + 1) = control_create( ...
	'name','thresh', ...
	'alias','Threshold', ...
	'tab',tabs{1}, ...
	'space',0.75, ...
	'style','slider', ...
	'min',0, ...
	'max',1, ...
	'tooltip','Normalized correlation threshold for detection' ...
);

control(end + 1) = control_create( ...
	'name','thresh_test', ...
	'alias','On', ...
	'tab',tabs{1}, ...
	'style','checkbox', ...
	'tooltip','Toggle threshold test' ...
);

control(end + 1) = control_create( ...
	'name','mask', ...
	'tab',tabs{2}, ...
	'space',1, ...
	'style','checkbox', ...
	'tooltip','Toggle template masking' ...
);

control(end + 1) = control_create( ...
	'name','mask_percentile', ...
	'alias','Percentile', ...
	'tab',tabs{2}, ...
	'space',1.25, ...
	'style','slider', ...
	'min',0, ...
	'max',1, ...
	'tooltip','Masking percentile' ...
);

%-------------------------
% SET VALUES
%-------------------------

% if ~isempty(context.ext.value)
% 	control = update_control_values(control, context.ext.value);
% end

control = update_control_values(control, parameter);


%-------------------------------------------
% UPDATE_CONTROL_VALUES
%-------------------------------------------

function control = update_control_values(control, source)

for k = 1:length(control)
	
	switch control(k).style
		
		% NOTE: we don't want to do these
		
		case {'buttongroup', 'header'}, continue;
			
		% NOTE: we don't know how to do these yet, we have string and value to work with
		
		case {'listbox', 'popup'}, continue;
			
		otherwise
			
			if isfield(source, control(k).name)
				control(k).value = source.(control(k).name);
			end
			
	end
	
end



