function fig = finalcheck(hObject, eventdata, Empty_Button, Buttons, data, lcs)

fig = figure(102);
pause(.1);
set(0,'DefaultAxesTitleFontWeight','normal');
scrsz = get(0,'ScreenSize');

set(fig,'Position',[1/10*scrsz(3:4) 2/3*scrsz(3:4)]);

pause(.5);
figpos = get(fig,'Position');

set(fig,'Name',['Selection of automatically calculated spectra'],'NumberTitle','off','MenuBar','none','Toolbar','figure','color','white');

if ispc
    basicfsz = 4.5;
    basichi = 0.025;
else
    basicfsz = 5.5;
    basichi = 0.025;
end

Styles = {'bitstream charter';'clean';'Courier';'courier 10 pitch';'frank ruehl';'Helvetica';'Helvetica-Narrow';'itc bookman';'urw bookman l'};
sty = Styles{6};
bgc = 0.95*ones(3,1);
bgw = [1, 1, 1];

AxesSOL = axes('Fontsize',2*basicfsz,'Units','normalized','FontName',sty,'Position',[0.1, 0.575, 0.55, 0.375]);
plot(AxesSOL,1,0,'w');

AxesREJ = axes('Fontsize',2*basicfsz,'Units','normalized','FontName',sty,'Position',[0.1, 0.075, 0.55, 0.375]);
plot(AxesREJ,1,0,'w');

if data.noc == 0
    mid = min(min(data.ainter));
    mad = max(max(data.ainter));
else
    mid = 0;
    mad = 1;
    for i=1:data.noc
        mid = min(mid,min(data.SOL(i).a));
        mad = max(mad,max(data.SOL(i).a));
    end
    mid = min(mid,min(min(data.ainter)));
    mad = max(mad,max(max(data.ainter)));
end

nc = size(lcs,1);
lgntxt = {};
oldtxt = {};
Ages = [];
for i=1:data.noc
    Ages = [Ages; data.SOL(i).a];
end
for i=1:data.noc
    if data.SOL(i).xbd(1) == data.SOL(i).xbd(2)
        oldtxt{i} = num2str(round(data.SOL(i).xbd(1)));
    else
        oldtxt{i} = [num2str(round(data.SOL(i).xbd(1))),' - ',num2str(round(data.SOL(i).xbd(2)))];
    end
    lgntxt{i} = oldtxt{i};
end
newtxt = {};
for i=1:length(data.final)
    newtxt{i} = num2str(round(10*data.x(data.final(i)))/10);
    lgntxt{end+1} = newtxt{i};
end
for i=1:length(data.final)
    Ages = [Ages; data.ainter(i,:)];
end

cov_level = 0.9;
no = size(Ages,1);
cov = zeros(no,no);
for i=1:no-1
    for j=i+1:no
        cov(i,j) = Ages(i,:)*Ages(j,:)'/norm(Ages(i,:),2)/norm(Ages(j,:),2);
        cov(j,i) = cov(i,j);
    end
end
% cov
% corrcoef(Ages')

TextSelect(2) = uicontrol('Style','text','String','New proposed spectra','HorizontalAlignment','left','Fontsize',2.2*basicfsz,'BackgroundColor','white',...
    'Units','normalized','FontName',sty,'Position',[0.675, 0.55, 0.2, 2*basichi]);

if data.noc > 0
    TextSelect(1) = uicontrol('Style','text','String','Already accepted spectra','HorizontalAlignment','left','Fontsize',2.2*basicfsz,'BackgroundColor','white',...
    'Units','normalized','FontName',sty,'Position',[0.675, 0.85, 0.2, 2*basichi]);
    
    TextSelect(4) = uicontrol('Style','text','String','show','HorizontalAlignment','left','Fontsize',1.75*basicfsz,'BackgroundColor','white',...
        'Units','normalized','FontName',sty,'Position',[0.79, 0.835, 0.05, 1.5*basichi]);
end

TextSelect(6) = uicontrol('Style','text','String','okay','HorizontalAlignment','left','Fontsize',1.75*basicfsz,'BackgroundColor','white',...
    'Units','normalized','FontName',sty,'Position',[0.79, 0.535, 0.05, 1.5*basichi]);

TextSelect(6) = uicontrol('Style','text','String','correlations','HorizontalAlignment','left','Fontsize',1.75*basicfsz,'BackgroundColor','white',...
    'Units','normalized','FontName',sty,'Position',[0.825, 0.535, 0.07, 1.5*basichi]);

chaccOld = [];
i = 0;
for i=1:data.noc
%     TextOld(i) = uicontrol('Style','text','String',['(',num2str(i),') ',oldtxt{i}],'HorizontalAlignment','left','Fontsize',1.5*basicfsz,'BackgroundColor','white',...
    TextOld(i) = uicontrol('Style','text','String',oldtxt{i},'HorizontalAlignment','left','Fontsize',1.75*basicfsz,'BackgroundColor','white',...
    'Units','normalized','FontName',sty,'Position',[0.695, 0.818-i*0.03, 0.08, 2.0*basichi]);
    
    chaccOld(i) = uicontrol('Style','checkbox','String','','Units','normalized','BackgroundColor','white',...
        'Position',[0.795, 0.835-i*0.03, 0.02, 1.75*basichi],'Fontsize',1.8*basicfsz,'FontWeight','bold','FontName',sty,...
        'HandleVisibility','off','Value',1);
end

chaccNew = [];
i0 = data.noc;
for i=1:length(data.final)
    TextNew(i) = uicontrol('Style','text','String',newtxt{i},'HorizontalAlignment','left','Fontsize',1.75*basicfsz,'BackgroundColor','white',...
    'Units','normalized','FontName',sty,'Position',[0.695, 0.518-i*0.03, 0.08, 2.0*basichi]);
    
    covs = find(cov(i0+i,:) > cov_level);
    txt = [];
    for j=covs
        if isempty(txt)
            txt = lgntxt{j};
        else
            txt = [txt,', ',lgntxt{j}];
        end
    end
    CorNew(i) = uicontrol('Style','text','String',txt,'HorizontalAlignment','left','Fontsize',1.75*basicfsz,'BackgroundColor','white',...
    'Units','normalized','FontName',sty,'Position',[0.83, 0.518-i*0.03, 0.1, 2.0*basichi]);
    
    chaccNew(i) = uicontrol('Style','checkbox','String','','Units','normalized','HorizontalAlignment','center',...
        'Position',[0.795, 0.535-i*0.03, 0.02, 1.75*basichi],'Fontsize',1.8*basicfsz,'FontWeight','bold','FontName',sty,...
        'HandleVisibility','off','Value',1,'BackgroundColor','white');
    
    if length(covs)>=1 && min(covs)<i0+i
        set(chaccNew(i),'Value',0);
    end
end

subploPGA(hObject, eventdata, AxesSOL, AxesREJ, data, lgntxt, mid, mad, lcs, chaccOld, chaccNew);

set(chaccOld,'Callback',{@subploPGA, AxesSOL, AxesREJ, data, lgntxt, mid, mad, lcs, chaccOld, chaccNew});
set(chaccNew,'Callback',{@subploPGA, AxesSOL, AxesREJ, data, lgntxt, mid, mad, lcs, chaccOld, chaccNew});

Button(1) = uicontrol('Style','pushbutton','UserData',0,'String','Accept','HorizontalAlignment','left','Fontsize',2*basicfsz,'FontWeight','bold',...
    'Units','normalized','FontName',sty,'Position',[0.7, 0.125, 0.13, 1.5*basichi],...
    'TooltipString', sprintf('Accept the results.'),...
    'Callback',{@subbuttonacceptPGA, Empty_Button, Buttons, data, chaccOld, chaccNew, fig});

Button(2) = uicontrol('Style','pushbutton','UserData',0,'String','Cancel','HorizontalAlignment','center','Fontsize',2*basicfsz,'FontWeight','bold',...
    'Units','normalized','FontName',sty,'Position',[0.85, 0.125, 0.13, 1.5*basichi],...
    'Callback',{@innerbuttoncancel, fig});

function subploPGA(hObject, eventdata, AxesSOL, AxesREJ, data, lgntxt, mid, mad, lcs, chaccOld, chaccNew)
nc = size(lcs,1);
lgnSOL = {};
lgnREJ = {};
for i=1:data.noc
    if get(chaccOld(i),'Value')
        plot(AxesSOL, data.x, data.SOL(i).a, 'k', 'Linewidth',1);
        lgnSOL{end+1} = lgntxt{i};
        hold(AxesSOL,'on');
    else
        plot(AxesREJ, data.x, data.SOL(i).a, 'k', 'Linewidth',1);
        lgnREJ{end+1} = lgntxt{i};
        hold(AxesREJ,'on');
    end
end
for i=1:length(data.final)
    if get(chaccNew(i),'Value')
        plot(AxesSOL, data.x(data.final(i))*[1 1], [mid mad], 'color',lcs(mod(i-1,nc)+1,:),'Linewidth',2);
        hold(AxesSOL,'on');
    else
        plot(AxesREJ, data.x(data.final(i))*[1 1], [mid mad], 'color',lcs(mod(i-1,nc)+1,:),'Linewidth',2);
        hold(AxesREJ,'on');
    end
end
for i=1:length(data.final)
    if get(chaccNew(i),'Value')
        plot(AxesSOL, data.x, data.ainter(i,:), 'color',lcs(mod(i-1,nc)+1,:), 'Linewidth',1);
        lgnSOL{end+1} = lgntxt{data.noc+i};
    else
        plot(AxesREJ, data.x, data.ainter(i,:), 'color',lcs(mod(i-1,nc)+1,:), 'Linewidth',1);
        lgnREJ{end+1} = lgntxt{data.noc+i};
    end
end
hold(AxesSOL,'off');
hold(AxesREJ,'off');
if length(lgnSOL) > 0
    lgnSOL = legend(AxesSOL,lgnSOL);
    set(lgnSOL,'fontsize',10);
else
    plot(AxesSOL,1,0,'w');
end
if length(lgnREJ) > 0
    lgnREJ = legend(AxesREJ,lgnREJ);
    set(lgnREJ,'fontsize',10);
else
    plot(AxesREJ,1,0,'w');
end

title(AxesSOL,'New profiles to accept');
xlabel(AxesSOL,'channel');
set(AxesSOL,'Fontsize',12);
axis(AxesSOL,'tight');

title(AxesREJ,'Repeated detections');
xlabel(AxesREJ,'channel');
set(AxesREJ,'Fontsize',12);
axis(AxesREJ,'tight');

function subbuttonacceptPGA(hObject, eventdata, Empty_Button, Buttons, data, chaccOld, chaccNew, parentfig)
newdel = [];
for i=1:length(chaccNew)
    if get(chaccNew(i),'Value') == 0
        newdel = [newdel;i];
    end
end

% if ~isempty(newdel)
% if length(newdel)~=length(chaccNew)
    newacc = setdiff(1:length(data.final),newdel);
    data.final = data.final(newacc);
    data.ainter = data.ainter(newacc,:);
    data.tinter = data.tinter(newacc,:);
% %     data.yinter = data.yinter(mod(newacc,6),:);         %length(data.yinter) = 6, index error for 7+
    data.W = data.W(newacc,:);
% end
data.isautosol = 1;

set(Empty_Button,'UserData',data);

close(parentfig);

function innerbuttoncancel(hobject, eventdata, parentfig)
close(parentfig);
