classdef FileLoader < handle
    %FileLoader Loads a File given as a String
    %   Detailed explanation goes here
    
    properties
        currentfile
        directorypath
        interface
        dataqueue
        datatimer
    end
    
    methods
        function obj = FileLoader(interface, directory)
            obj.directorypath = directory;
            obj.interface = interface;
            if ~isempty(obj.directorypath)
                obj.currentfile = [obj.directorypath filesep datestr(now, 'yyyy-mm-dd HH-MM-SS') 'spr.txt'];
            else
                obj.currentfile = ['data' filesep datestr(now, 'yyyy-mm-dd HH-MM-SS') 'spr.txt'];
            end
            obj.dataqueue = {};
            obj.datatimer = timer('TimerFcn', {@queuetimer, obj}, 'StartDelay', 5);
        end
        
        function setDirectory(obj, string)
            obj.directorypath = string;
        end
        
        function dir = getDirectory(obj)
            dir = obj.directorypath;
        end
        
        function newfile(obj)
            if ~isempty(obj.directorypath)
                obj.currentfile = [obj.directorypath filesep datestr(now, 'yyyy-mm-dd HH-MM-SS') 'spr.txt'];
            else
                obj.currentfile = ['data' filesep datestr(now, 'yyyy-mm-dd HH-MM-SS') 'spr.txt'];
            end
        end
        
        function success = loadfile(obj, filename)
            [fid, msg] = fopen(filename, 'r');
            success = 0;
            if isempty(msg)
                data = fscanf(fid, ' %g ', [39, inf]);
                obj.interface.adddatalines(data);
                success = 1;
            else
                obj.interface.sendwarning(['File Error: ' msg], 3);
            end
%             if isempty(msg)
%                 line = fgetl(fid);
%                 while line ~= -1
%                     obj.interface.adddata(line);
%                     line = fgetl(fid);
%                     % This is a little too complicated. Occasionally, fgetl
%                     % returns a ''. This may be windows newlines having two
%                     % return characters /r/n or whatever. Anyway, because
%                     % of this, we need two separate checks, one for line ==
%                     % -1 and one for isempty(line) since we should always
%                     % stop in the former case and never in the latter case.
%                     % But line ~= -1 returns [] for an empty line which is
%                     % problematic to then use AND or OR with. To get the
%                     % lines to always pass this test, we simply make sure
%                     % we never get an empty line in the outside while loop.
%                     % Hence the confusing piece of code below.
%                     while isempty(line)
%                         line = fgetl(fid);
%                     end
%                 end
%                 fclose(fid);
%                 obj.currentfile = filename;
%             else
%                 obj.interface.sendwarning(['File: ' msg], 3);
%             end
        end
        
        % function data = read(obj, filename)
        %     [fid, msg] = fopen(filename, 'r');
        %     % count is the number of numbers on a line
        %     count = 0;
        %     firstline = [0 0];
        %     while count ~= 39 && firstline(2) ~= 2*count + 2
        %         placehold = fgetl(fid);
        %         [firstline, count] = sscanf(placehold, '%f');
        %         if count < 2
        %             firstline = [0 0];
        %         end
        %     end
        %     data = fscanf(fid, '%f', [count inf]);
        %     fclose(fid);
        % end
        
        function append(obj, data)
            if isempty(obj.dataqueue)
                obj.appendline(data);
            else
                obj.dataqueue = vertcat(obj.dataqueue, data);
            end
        end
        
        function succeed = appendline(obj, data)
            [fid, msg] = fopen(obj.currentfile, 'a');
            succeed = 0;
            if isempty(msg) && length(data) == 39
                fprintf(fid, ' %10.6g ', data);
                fprintf(fid, '\n');
                obj.interface.sendwarning('Data Saving Succesfully.', 3);
                succeed = 1;
                fclose(fid);
            elseif isempty(msg) && ischar(data)% We assume this is one line of data
                fprintf(fid, ' %s ', data);
                obj.interface.sendwarning('Data Saving Succesfully.', 3);
                succeed = 1;
                fclose(fid);
            else
                % disp('An error occurred saving the file');
                obj.interface.sendwarning(['Files: ' msg], 3);
                obj.dataqueue = vertcat(obj.dataqueue, data);
                if ~strcmp(get(obj.datatimer, 'Running'), 'on')
                    start(obj.datatimer);
                end
            end
        end
        
        function appendqueue(obj)
            for i = 1:size(obj.dataqueue, 1)
                if ~obj.appendline(obj.dataqueue{1})
                    break;
                elseif size(obj.dataqueue, 1) > 1
                    % The previous check appended the current line.
                    % The next loop will append the next line.
                    % We just want to pull that line off the top and leave
                    % the next line in space 1
                    obj.dataqueue = obj.dataqueue(2:end, :);
                end
            end
        end
        
        function write(obj, filename, data)
            [fid, msg] = fopen(filename, 'w');
            if isempty(msg)
                fprintf(fid, ' %5.3g ', data);
            else
                % disp('An error occurred saving the file');
                disp(message);
            end
            fclose(fid);
        end
    end
end

function queuetimer(~, ~, obj)
obj.appendqueue();
end

