%% Aim: turn on/off diary file
% keep a record of everything printed to the screen as a backup/explanation

function [diaryName,suffix]=setDiaryFile(varargin)
global setD

%% Inputs
setD.inputSize=length(varargin);
if setD.inputSize>3
    display('Too many inputs!')
    return
else
    switch setD.inputSize
        case 3
            name=varargin{1};
            saveLocation=varargin{2};
            dateOption=varargin{3};
    end
end
%% Get: diary status
display('Checking to see if diary is on or off...');
setD.status=get(0,'Diary');
display(setD.status);
if strcmp(setD.status,'on')
    %% -----------------
    %% Task: turn Diary Off
    %% -----------------
    try
        %% Set: Off messages
        setD.offMessages={'----------------------------------',...
            '------  Turning Diary Off  -------',...
            horzcat('Closing: ',setD.name),...
            'Location:',...
            setD.saveLocation,'...'};
        %         disp('<a href="matlab:magic(4)">Go to data directory</a>')
        %% Show: Off messages
        for message=1:length(setD.offMessages);
            display(setD.offMessages{message});
        end
        diary; % turns diary off.
        display(' ====> Done.');
    catch msg
        setD.msg=msg;
        disp('Toggeling off...');
        diary;
    end
end
if and(strcmp(setD.status,'off'),nargin==0)
    %% Already off but trying to close
    setD.offMessages={'----------------------------------',...
        '------  Diary already Off -------',...
        'Can''t close... this might be a problem'};
    
    for message=1:length(setD.offMessages);
        display(setD.offMessages{message});
    end
    
elseif or(and(strcmp(setD.status,'on'),nargin>0),strcmp(setD.status,'off'))
    %% Diary on but wanting to start or just off
    % either, close current and start new or just start new
    %     if and(strcmp(setD.status,'on'),nargin>0)
    %         display('...diary on... Turning off');
    %         diary; % should toggle off
    %     end
    
    % else % off, start up the diary
    %% Set: Inputs
    setD.name=name;
    setD.saveLocation=saveLocation;
    setD.option=dateOption;
    %% -----------------
    %% Task: Turn Diary On
    %% -----------------
    %% Set: Full File Name + location
    display('Setting file Suffix:');
    % choosing from set of options for labelling of file
    switch setD.option
        case 'Date'
            setD.suffix=saveDateTime(5); % day month year
        case 'Time'
            setD.suffix=saveDateTime(4); % hours minutes
        case 'DateTime'
            setD.suffix=saveDateTime(1); % day month year
        otherwise
            setD.suffix=''; % nothing
    end
    display(horzcat('===> ',setD.suffix));
    
    setD.fullName=horzcat(setD.name,'Diary_',setD.suffix,'.dat');
    if ~exist(setD.saveLocation,'dir')
        mkdir(setD.saveLocation);
    end
    setD.saveLocationFile=horzcat(setD.saveLocation,setD.fullName);
    
    %% Set: On messages
    setD.onMessages={'----------------------------------',...
        '------  Turning Diary On  -------',...
        horzcat('Opening: ',setD.fullName),...
        'Location:',...
        setD.saveLocation,'...'};
    %% Show: On messages
    for message=1:length(setD.onMessages);
        display(setD.onMessages{message});
    end
    
    %% Do: Diary on
    diary(setD.saveLocationFile); % turns diary on.
    
    %% Set: end Messages
    setD.space='    ';
    setD.endMessages={' ====> Diary On.',...
        'Recording command window displayed info to - ',...
        'File Name:',...
        horzcat(setD.space,setD.fullName),...
        'Location:',...
        horzcat(setD.space,setD.saveLocation),...
        '----------------------------------','  '};
    %% Show: end Messages
    for message=1:length(setD.endMessages);
        display(setD.endMessages{message});
    end
end
%% -----------------
%% Set: output
    diaryName=setD.fullName;
    suffix=setD.suffix;
    
    % create go to data directory string as a hyperlink and allow this to open
    % data directory - borrowed code from matlab but there's another function
    cd(setD.saveLocation);
    disp('<a href="matlab:winExplorer">Go to data directory</a>')

if or(and(strcmp(setD.status,'off'),nargin==0),... % if off and no inputs (trying to close)
        and(strcmp(setD.status,'on'),nargin==0)) % On and no inputs (tring to close)
    %         ~and(strcmp(setD.status,'on'),nargin>0)),... % NOT on but trying to start
    fprintf('\n%s\n','Clearing diary globals');
    clear global setD
    clear setD
end