classdef ImageLoader < handle
    
    properties
        main_path;
        files;
        ids;
        current_frame;
        current_image;
    end
    
    methods
        
        function o = ImageLoader(main_path)
            o.main_path = main_path;
            o.files = dir(fullfile(main_path,'*.png'));
            
            o.ids = zeros(length(o.files),1);
            
            for i=1:length(o.files)
                file_name = o.files(i).name;
                file_id = str2double(file_name(1:end-4));
                o.ids(i) = file_id;
            end
            
            o.ids = sort(o.ids);
            
            o.current_frame = o.min();
        end
        
        function jump(o,i)
            cpos = find(o.ids == o.current_frame,1);
            npos = cpos+i;
            
            if npos<=length(o.ids) && npos>0 && o.valid(o.ids(npos))
                o.current_frame = o.ids(npos);
            else
                warning(['Cannot jump, last ' num2str(o.ids(end)) ' first ' num2str(o.ids(1))]) 
            end
        end
        
        function next(o)
            cpos = find(o.ids == o.current_frame,1);
            npos = cpos+1;
            
            if npos<length(o.ids) && o.valid(o.ids(npos))
                o.current_frame = o.ids(npos);
            else
                warning(['No next image, last ' num2str(o.ids(end))]) 
            end
        end
        
        function previous(o)
            cpos = find(o.ids == o.current_frame,1);
            ppos = cpos-1;
            
            if ppos>0 && o.valid(o.ids(ppos))
                o.current_frame = o.ids(ppos);
            else
                warning(['No previous image, first ' num2str(o.ids(1))]) 
            end
        end
        
        function out = valid(o,pos)
            
            if isempty(pos)
                out = false;
                return
            end
            
            l = find(o.ids == pos,1);
            if isempty(l)
                out = false;
                return
            else
                out = true;
                return
            end
        end
        
        function v = min(o)
            v = min(o.ids);
        end
        
        function v = max(o)
            v = max(o.ids);
        end
        
        function setCurrentFrame(o,id)
            pos = find(o.ids == id,1);
            
            if isempty(pos)
                warning(['File with id ' num2str(id) ' does not exists.'])
                return
            end
            
            o.current_frame = id;
        end
        
        function result = increment(o,value)
            
            next_value = o.current_frame + value;
            
            %Check if the value exists in the list
            pos = find(o.ids == next_value,1);
            
            if ~isempty(pos)
                o.current_frame = next_value;
                result = true;
            else
                warning(['Frame id ' num2str(next_value) ' does not exists'])
                result = false;
            end
        end
        
        function im = load(o)
            
            pos = find(o.ids == o.current_frame,1);
            
            if isempty(pos)
                warning(['File with id ' num2str(o.current_frame) ' does not exists.'])
                o.current_image=[];
                im = o.current_image;
                return
            end
            
            file_name = o.files(pos).name;
            
            o.current_image = imread(fullfile(o.main_path,file_name));
            im = o.current_image;
        end
        
        function im = loadById(o,id)
            pos = find(o.ids == id,1);
            
            if isempty(pos)
                warning(['File with id ' num2str(id) ' does not exists.'])
                o.current_image=[];
                im = o.current_image;
                return
            end
            
            file_name = o.files(pos).name;
            
            im = imread(fullfile(o.main_path,file_name));
        end
    
    end
end
