function get_parts_2(db_file)
    % get_parts(db_file)
    close all hidden
    conn=sqlite(db_file);
    sql="SELECT time FROM images;";
    res=fetch(conn,sql);
    close(conn);
    res=rowfun(@(x) str2num(x), res,'OutputVariableNames',{'time'});
    res_m=table2array(rowfun(@(x) mean(x), res));
    idx_lim=find(diff(res_m)>0.5e5);
    idx_lim=[1;idx_lim+1;length(res_m)];
    cb_data=generate_checkbox(db_file,idx_lim);
    idx_i=[];
    for idx=1:length(res_m)
        idx_i=[idx_i,get_part_id(idx_lim,idx)];
    end
    handles=show_all(db_file,idx_lim);
    figure(1);
    sub_time=[];
    for i=1:max(idx_i)-1
        sub_time=[sub_time,...
            plot([idx_lim(i):idx_lim(i+1)],res_m([idx_lim(i):idx_lim(i+1)]))];
        set(sub_time(end),'ButtonDownFcn',{@display_map,i,handles(i),cb_data.D(i)},...
            'LineWidth',1);
        hold on
    end
    hold off
    data.limits=idx_lim;
    data.db_file=db_file;
    data.sub_time=sub_time;
    data.handles=handles;
    data.cb_data=cb_data;
    data.yaw1=zeros(max(idx_i)-1);
    data.yaw2=zeros(max(idx_i)-1);
    set(groot,"UserData",data);
    read_state();
end


function ref=get_part_id(limits,id)
    ref1=find((limits+1)<=id);
    ref=[];
    if ~isempty(ref1) 
        ref=ref1(end);
    elseif id==limits(end)
        ref=length(limits)-1;
    end
end


function handles=show_all(db_file,limits)
    handles=[];
    figure(2)
    for lim=1:length(limits)-2
        sql="SELECT * FROM images WHERE %d<=cnt_id AND cnt_id<%d;";
        conn=sqlite(db_file);
        res=fetch(conn,sprintf(sql,limits(lim),limits(lim+1)));    
        close(conn);
        lalo_p=rowfun(@(x,y) [jsondecode(x),jsondecode(y)], res(:,["lat","lon"]),'OutputFormat', 'cell');
        yaw_p=rowfun(@(x) [jsondecode(x)], res(:,["yaw"]),'OutputFormat', 'cell');
        time_p=rowfun(@(x) [jsondecode(x)], res(:,["time"]),'OutputFormat', 'cell');
        angv_z_p=rowfun(@(x) [jsondecode(x)], res(:,["angv_z"]),'OutputFormat', 'cell');
        yaw=cell2mat(yaw_p);
        lalo=cell2mat(lalo_p);
        h=geoplot(lalo(:,1),lalo(:,2),'x'); 
        h.Visible="off";
        udata.lalo_p=lalo_p;
        udata.yaw_p=yaw_p;
        udata.time_p=time_p;
        udata.angv_z_p=angv_z_p;
        udata.dd=[];
        udata.dd_tot=1:length(lalo_p);
        set(h,'UserData',udata);
        hold on;
        handles=[handles,h];
    end
    hold off;
end

% % function s1_change(src,evt,handle,maxv,nr)
% %     dd=handle.UserData.dd;
% %     d_temp=[dd(1).Value,dd(2).Value,dd(3).Value,dd(4).Value];
% %     d_temp=sort(d_temp);
% %     df=d_temp;
% %     if d_temp(2)==0
% %         df(1)=d_temp(3);
% %         df(3)=0;
% %     elseif d_temp(1)==0
% %         df(1)=d_temp(2);
% %         df(2)=d_temp(3);
% %         df(3)=0;
% %     else
% %         if d_temp(1)==d_temp(2)
% %             df(2)=0;
% %         end
% %         if d_temp(3)==d_temp(4)
% %             df(3)=0;
% %         end        
% %     end
% %     
% %     for idx=1:4
% %         dd(idx).Value=df(idx);
% %     end
% % 
% %     
% %     to_del=[];
% %     if df(1)>1
% %         to_del=[1:df(1)-1];
% %     end
% %     if (df(4)<maxv)
% %         to_del=[to_del,df(4)+1:maxv];
% %     end
% %     if df(2)~=0 & df(3)~=0 & df(1)<df(2) & df(3)<df(4)
% %         to_del=[to_del,df(2):df(3)];
% %     end
% % 
% %     idx=setdiff(1:maxv,to_del);
% %     handle.UserData.dd_tot=idx;
% %     lalo=cell2mat(handle.UserData.lalo_p(idx));
% %     handle.LatitudeData=lalo(:,1)';
% %     handle.LongitudeData=lalo(:,2)';
% % 
% % end


function display_map(src,eventdata,idx,handle,cb,~) 
% %     if eventdata.Button==3
% %         ca=axis;
% %         p=get(1,'Position');
% %         parent=uifigure("Position",[p(1),p(2)+300,p(3),p(4)-200]);
% %         maxv=length(handle.UserData.lalo_p);
% %         dd=[];
% %         for nr=0:3
% %             dd=[dd,uidropdown(parent,'FontSize',15,'Position',[10,180-32*nr,150,30],...
% %             'ValueChangedFcn',{@s1_change,handle,maxv,nr+1})];            
% %             if ismember(nr,[0,3])
% %                dd(end).ItemsData= 1:maxv;
% %                dd(end).Items= arrayfun(@(x) string(num2str(x)), 1:maxv);
% %                if nr==0
% %                    dd(end).Value=1;
% %                elseif nr==3
% %                    dd(end).Value=maxv;
% %                end
% %             else
% %                dd(end).ItemsData= 0:maxv;
% %                dd(end).Items= arrayfun(@(x) string(num2str(x)), 0:maxv);
% %                dd(end).Items(1)={'-'};
% %                dd(end).Value=0;
% %             end
% %         end
% %        udata=handle.UserData;
% %        udata.dd=dd;
% %        set(handle,'UserData',udata);
% %        return
% %    end
% %    if isgraphics(50)
% %        close(50);
% %    end
    if handle.Visible=="on"
        handle.Visible="off";
        src.LineWidth=1;
        cb.Value=0;
        close_hist();
    else
        handle.Visible="on";
        src.LineWidth=3;
        cb.Value=1;
        udata=get(groot,'UserData');
        [udata.yaw1(idx),udata.yaw2(idx)]=show_hist(handle);
        set(groot,'UserData',udata);
    end
    save_state();
end

function cb_data=generate_checkbox(db_file,limits)
    conn=sqlite(db_file);
    sql="SELECT id FROM images;";
    res=fetch(conn,sql);
    close(conn);
    cb_data=[];
    a=uifigure("Position",[180,550,300,600]);
    text='Path %d';
    uilabel(a,"Text","D","Position",[10,580,20,20]);
    uilabel(a,"Text","P","Position",[30,580,20,20]);
    uilabel(a,"Text","S","Position",[50,580,20,20]);
    P=[];
    D=[];
    S=[];
    for w=1:length(limits)-2
        t1=res.id(limits(w)).split("_");
        t2=res.id(limits(w+1)).split("_");
        datum=extractBetween(t1(2),7,8)+"."+...
          extractBetween(t1(2),5,6)+"."+...
          extractBetween(t1(2),1,4);
        t_start=extractBetween(t1(3),1,2)+":"+...
          extractBetween(t1(5),3,4)+":"+...
          extractBetween(t1(3),5,6);
        t_end=extractBetween(t2(5),1,2)+":"+...
          extractBetween(t2(5),3,4)+":"+...
          extractBetween(t2(5),5,6);
        l=datum+" "+t_start+"-"+t_end;
        D=[D,uicheckbox(a,"Text","","Position",[10 580-20*w 20 20], ...
          "ValueChangedFcn",{@DisplayCB,w})];
        P=[P,uicheckbox(a,"Text","","Position",[30 580-20*w 20 20],...
          "ValueChangedFcn",{@CheckPrimary,w})];
        S=[S,uicheckbox(a,"Text",l,"Position",[50 580-20*w 200 20],...
            "ValueChangedFcn",{@CheckSecondary})];
    end
    cb_data.D=D;
    cb_data.P=P;
    cb_data.S=S;
end

function  DisplayCB(app,event,idx)
    data=get(groot,"UserData");
    if event.Value
        data.handles(idx).Visible="on";
        data.sub_time(idx).LineWidth=3;
        [data.yaw1(idx),data.yaw2(idx)]=show_hist(data.handles(idx));
    else
        data.handles(idx).Visible="off";
        data.sub_time(idx).LineWidth=1;
        close_hist();
    end
    set(groot,"UserData",data);
    save_state()
end

function  CheckPrimary(app,event,idx)
    data=get(groot,"UserData");
    if event.Value
        for jdx=1:length(data.cb_data.P)
            if idx==jdx
                continue
            end
            data.cb_data.P(jdx).Value=0;
        end
        data.cb_data.S(idx).Value=0;
    end
    save_state()
end

function  CheckSecondary(app,event)
    save_state()
end


function save_state()
end

function read_state()
    % save state
    data=get(groot,"UserData");
    conn=sqlite(data.db_file);
    res=fetch(conn,"SELECT name FROM sqlite_schema WHERE name='state';");
    close(conn);
    if isempty(res)
        return
    end
    conn=sqlite(data.db_file);
    state_data=fetch(conn,"SELECT * FROM state;");    
    close(conn);
    D=table2array(state_data([state_data.pri_sec==3],4));
    Dy=table2array(state_data([state_data.pri_sec==3],[5,6]));
    for idx=1:length(D)
        data.cb_data.D(D(idx)).Value=1;
        data.handles(D(idx)).Visible="on";
        data.sub_time(D(idx)).LineWidth=3;
        data.yaw1(D(idx))=Dy(idx,1);
        data.yaw2(D(idx))=Dy(idx,2);

    end
    S=table2array(state_data([state_data.pri_sec==0],4));
    Sy=table2array(state_data([state_data.pri_sec==0],[5,6]));
    for idx=1:length(S)
        data.cb_data.S(S(idx)).Value=1;
        data.yaw1(S(idx))=Sy(idx,1);
        data.yaw2(S(idx))=Sy(idx,2);

    end
    P=table2array(state_data([state_data.pri_sec==1],4));
    Py=table2array(state_data([state_data.pri_sec==1],[5,6]));
    if ~isempty(P)
        data.cb_data.P(P(1)).Value=1;
        data.yaw1(P(1))=Py(1);
        data.yaw2(P(1))=Py(2);
    end

    set(groot,'UserData',data);
end

function [yaw1,yaw2]=show_hist(handle)
    figure(50);
    angv_z_=cell2mat(handle.UserData.angv_z_p(handle.UserData.dd_tot));
    ti=cell2mat(handle.UserData.time_p(handle.UserData.dd_tot));
    [tim,idx,reps]=unique(ti(:));
    angv_z=angv_z_(idx);
    [~,tidx]=sort(tim);
    angv_z=angv_z(tidx);
    plot(angv_z,'x');
    grid on;
    yaw1=2;
    yaw2=1;
end

function close_hist()
    if ishandle(50)
        close(50);  
    end
end