classdef Xb3PointLabeling < handle
    
    properties
        loader;
        points;
        data_file;
        h_past;
    end
    
    methods
        function o = Xb3PointLabeling(main_path,data_file)
            o.loader = Xb3Loader(main_path);
            o.points = Xb3HandCalibrationPoints();
            o.data_file = data_file;
            
            o.points.load(o.data_file);
        end
        
        function callbackKeyPressed(o,src,eventData)
%             disp(eventData.Key)
            
            if strcmp(eventData.Key,'rightarrow') 

                if strcmp(eventData.Modifier,'shift')
                    o.loader.increment(10);
                elseif strcmp(eventData.Modifier,'control')
                    o.loader.increment(100);
                else
                    o.loader.increment(1);
                end

                o.loader.load();
                o.draw();
            elseif strcmp(eventData.Key,'leftarrow')
                if strcmp(eventData.Modifier,'shift')
                    o.loader.increment(-10);
                elseif strcmp(eventData.Modifier,'control')
                    o.loader.increment(-100);
                else
                    o.loader.increment(-1);
                end

                o.loader.load();
                o.draw();
                
            elseif strcmp(eventData.Key,'a')
                
%                 disp('add current selected point')
                h = findobj(gca,'Tag','pt');
                
                if isempty(h)
                    warndlg('No point selected','Failed to add new point')
                end
                
%                 keyboard
                
                point = [get(h,'xdata') get(h,'ydata')];
                
                label = inputdlg('Label for point','Add new point');
                
                if ~isempty(label)
                    o.points.add(point,label,o.loader.current_frame);
                    o.draw();
                end
            elseif strcmp(eventData.Key,'s')
                o.points.save(o.data_file);
            elseif strcmp(eventData.Key,'l')
                o.points.load(o.data_file);
                o.draw();
            end
        end
        
        function callbackImagePressed(o,src,eventData)
            % keyboard

            point = get(gca, 'CurrentPoint');
            point = point(1,:); % get only the first line
            point = point -3;

            h = findobj(gca,'Tag','pt'); % try to find the old point

            if isempty(h) % if it's the first click (i.e. no previous point to delete)

                % highlight the selected point
                hold on
                h = plot(point(1), point(2), 'r'); 
                set(h,'marker','+','linestyle','none','markersize',10);
                set(h,'MarkerFaceColor',[1 0 0],'markeredgecolor',[1 0 0])
                set(h,'Tag','pt'); % set its Tag property for later use   
            %     set(h,'UserData',tags{pointCloudIndex}); % set its Tag property for later use   

            else % if it is not the first click

                delete(h); % delete the previously selected point

                % highlight the newly selected point
                h = plot(point(1), point(2), 'r'); 
                set(h,'marker','+','linestyle','none','markersize',10);
                set(h,'MarkerFaceColor',[1 0 0],'markeredgecolor',[1 0 0])
                set(h,'Tag','pt');  % set its Tag property for later use
            %     set(h,'UserData',tags{pointCloudIndex}); % set its Tag property for later use   
            end

        end
        
        function draw(o)
            try
                delete(o.h_past);
            catch
            end
            
            h_new=[];
            
            h_new(end+1) = image(o.loader.current_image);
%             set(gca,'LooseInset',get(gca,'TightInset'))
            set(gca,'LooseInset',[0.05 0.05 0.05 0.05])
            axis equal
            set(gca,'visible','off')
            h_new(end+1) = text(10,15,['Frame: ' num2str(o.loader.current_frame)]);
            set(h_new(end),'FontSize',12,'FontWeight','bold','color',[1 1 1])
            
            %Draw all labels in the current frame
%             keyboard
            try
                data = o.points.points(o.loader.current_frame);
                
                hold on
                for i=1:length(data)
                    h_new(end+1)=plot(data(i).position(1),data(i).position(2));
                    set(h_new(end),'marker','+','linestyle','none','markersize',10);
                    set(h_new(end),'MarkerFaceColor',[0 0 1],'markeredgecolor',[0 0 1])
                    h_new(end+1)=text(data(i).position(1)+8,data(i).position(2)+8,data(i).label);
%                     set(h_new(end),'color',[1 1 1],'fontweight','bold')
                    set(h_new(end),'color',[1 1 1])
                end
                
%                 hold off
            catch
            end
            
            o.h_past = h_new;
        end
        
    end
    
    
end