function so=setGetInputs(inList)
%% setGetInputs
%
% settings=setGetInputs(inList)
%
% function to set on/off inputs and place varargin info in a structure for
% use within a program
%
% * requires inList stucture:
% inList.turnOn
% inList.turnOff
% inList.settings
% inList.defaults - defaults for the settings
% - in format 'settings',value,...
% - e.g., .defaults={'tester','name','codeNumber',999}
%
% inList.varargin = varargin from calling function
%
% For example:
% egFunction(varargin)
%
%   egFunction inList
%       inList.turnOn={'test'};
%       inList.turnOff={'comment'};
%       inList.settings={'codeNumber','gender'};
%       inList.defaults={'codeNumber',999,'gender',1};
%       inList.varargin=varargin;

%       settings=setGetInputs(inList)
%
% - in the egFunction the setGetInputs will:
%      * set the 'test' value to 0 (i.e., starts off and is turned on)
%      * set the 'comment' value to 1 (i.e., starts on and is turned off)
%      * set 'codeNumber' and 'gender' values to 999 and 1 respectively
%      * then it will examine the inList.varargin variable for the presence
%       of the turnOn, turnOff, and settings strings.
%       - the presence of the turnOn or turnOff strings toggles their
%       values
%       - the settings values will be set to the i+1 value of varargin
%       e.g., inList.varargin={'codeNumber',555,'gender',2} results in:
%           codeNumber=555;
%           gender=2;
%       ==> this is similar to the defaults arrangements
%
so.tc=0; % temp comment
if so.tc
    fprintf('%s\n',['Running ',mfilename]);
end
%% set turnOn/Off values
if isfield(inList,'turnOn')
    if ~isempty(inList.turnOn)
        for i=1:length(inList.turnOn)
            so.(inList.turnOn{i})=0; % starts off
            if so.tc
                fprintf('%s\n',['Turn on - ',inList.turnOn{i}]);
            end
        end
    end
end
if isfield(inList,'turnOff')
    if ~isempty(inList.turnOff)
        for i=1:length(inList.turnOff)
            so.(inList.turnOff{i})=1; % starts on
            if so.tc
                fprintf('%s\n',['Turn off - ',inList.turnOff{i}]);
            end
        end
    end
end
% set default values for the settings
if isfield(inList,'defaults')
    if ~isempty(inList.defaults)
        tempSkip=0;
        for i=1:length(inList.defaults)
            if ~tempSkip
                so.(inList.defaults{i})=inList.defaults{i+1}; % starts on
                so.defaults.(inList.defaults{i})=inList.defaults{i+1}; % starts on
                if so.tc
                    fprintf('%s\n',['Turn off - ',inList.defaults{i}]);
                end
                tempSkip=1;
            else
                tempSkip=0;
            end
        end
    end
end
%% get inputs if there are any
so.list={};
if sum(isfield(inList,{'defaults','settings'}))==2
    nIn=length(inList.varargin);
    if nIn>0
        % check to see what sort of array we have
        if iscell(inList.varargin{1}(1))
            if so.tc
            display('cell array within a cell array - need to get it out')
            end
            for i=1:length(inList.varargin{1})
                try
                    inList.temp{i}=char(inList.varargin{1}(i));
                catch msg
                    inList.msg=msg;
                    inList.temp{i}=inList.varargin{1}{i};
                end
            end
            inList.varargin=inList.temp;
            if ~isnan(str2double(inList.temp));
                inList.varargin=str2double(inList.temp);
            end
            %             display(inList.varargin);
            nIn=length(inList.varargin);
        end
        tempSkip=0;
        for i=1:nIn
            if ~tempSkip
                if so.tc
                fprintf('%s\n',['varargin ',num2str(i),' = ',inList.varargin{i}]);
                end
                if sum(strcmp(inList.varargin{i},inList.turnOn))==1
                    so.(inList.varargin{i})=1; so.list{end+1}=inList.varargin{i};
                elseif sum(strcmp(inList.varargin{i},inList.turnOff))==1
                    so.(inList.varargin{i})=0; so.list{end+1}=inList.varargin{i};
                elseif sum(strcmp(inList.varargin{i},inList.settings))==1
                    so.(inList.varargin{i})=inList.varargin{i+1}; so.list{end+1}=inList.varargin{i};
                    %                 if ~isnan(str2double(inList.varargin{i+1}));
                    %                     so.(inList.varargin{i})=str2double(inList.varargin{i+1});
                    %                 end
                    try
                        if ~isnan(str2double(inList.varargin{i+1}));
                            so.(inList.varargin{i})=str2double(inList.varargin{i+1});
                        end
                    catch
                        if ~isstruct(inList.varargin{i+1})
                            if ~isnumeric(inList.varargin{i+1}); % is it a string
                                % and then, is can the string be converted to a number
                                if isnumeric(str2double(inList.varargin{i+1}))
                                    so.(inList.varargin{i})=str2double(inList.varargin{i+1});
                                end
                            end
                        end
                    end
                    tempSkip=1;
                end
            else
                tempSkip=0;
            end
        end
        
    end
else
    fprintf('\n%s\n\t%s\n',['Error in ',mfilename,...
        ': couldn''t find ''defaults'' and ''settings'' inputs.'],...
        'Make sure everything is spelt correctly.');
end
clear inList nIn tempSkip

