function show_primary_sss(ref,fig_handle)
   first=imread(fullfile(ref.path,ref.id+".png"));
   axes(fig_handle);
   pim=imshow(first);
   set(pim,'ButtonDownFcn',{@(src,evt) insert_marker(src,evt,ref)});
   for idx=1:length(ref.rota)        
         axes(fig_handle);
        rectangle('Position',[1   ,ref.perc(ref.rota(idx))*1024,...
                              1023,0.0099*1024],...
                              'FaceColor',[0 1 0 0.25],'EdgeColor',[ 0 1 0 0.125]);
   end
   t=title("Ref: "+convert_to_title(ref.id), 'Interpreter', 'none','Color',[1 0 0]);
   show_rects_in_primary(ref)
end

function insert_marker(src,event,ref) %,im2)
    if event.Button == 1  
        po=event.IntersectionPoint;
        h = images.roi.Circle();
        addlistener(h,'MovingROI',@(src,evt) updateLabel(src,evt,ref)); %,im2));
        addlistener(h,'ROIClicked',@(src,evt) updateUnits(src,evt,ref));
        %cp = hAx.CurrentPoint;
        cp = po(1:2);
        h.beginDrawingFromPoint(cp);
    elseif event.Button == 3
            extend_primary(ref,9);
    end
end

function updateLabel(src,evt,ref)
end

function updateUnits(src,evt,ref)
    data=get(groot,'UserData');
    rect_data=get(src,'UserData');
    xy=evt.Source(1).Position;
    xol=xy(1)/1024; yol=xy(2)/1024;
    la=interp1(ref.perc,ref.lat,yol);
    lo=interp1(ref.perc,ref.lon,yol);
    de=interp1(ref.perc,ref.altitude,yol);
    ya=interp1(ref.perc,ref.yaw,yol);
    [laP,loP]=getGeo1024(xol,yol,ref.range,la,lo,1*de,ya,0);
    [scA,scO]=scircle1(laP,loP,0.000005);
    figure(1);
    hold on;
    if isempty(rect_data)
        rk=geoplot(scA,scO,'Color',[1 0 0],'LineWidth',2);
        rect_data.rk=rk;
        insert_in_rect(rk,laP,loP,de);
    else
        rect_data.rk.LatitudeData=scA;
        rect_data.rk.LongitudeData=scO;
        rect_data.de=de;
        modify_in_rect(rect_data.rk);
    end
    hold off
    set(src,'UserData',rect_data);
end


function insert_in_rect(rk,laP,loP,de)
    data=get(groot,'UserData');
    rect.la=laP;
    rect.lo=loP;
    rect.de=de;
    rect.show=true;
    rect.geo_list=[];
    if isempty(data.rect)
        rect.id=1;
        data.rects_id=[1];
        rect=insert_rect_in_check_list(data,rect);
        set(rk,'UserData',rect);
        data.rect=[rk];
    else
        rect.id=data.rects_id(end)+1;
        data.rects_id=[data.rects_id,rect.id];
        rect=insert_rect_in_check_list(data,rect);
        set(rk,'UserData',rect);
        data.rect=[data.rect,rk];
    end
    set(groot,'UserData',data);
end

function modify_in_rect(rk)
    data=get(groot,'UserData');
    udata=get(rk,'UserData');
    idx=find(data.rects_id==udata.id);
    if ~isempty(idx)
        data.rect(idx)=rk;
    end
    set(groot,'UserData',data);
end

