% clear; close all;

% Note to self: use train = 00-14; dev = 15-19

% toggle on if need to save data
% outs = struct([]);

%% RUN ALGO

train_directory = 'training/training/';
S = dir(train_directory); S = S([S(:).isdir]==1); S = S(~ismember({S(:).name},{'.','..'}));
j = 1:5; %16:20; %1:1:length(S); % used to be: 1`

%-----------------------------------------
% scores = []; longests = []; totals = [];

for i = j
    disp(strcat('running doc ', S(i).name, '!'))
    shred_directory = strcat(train_directory, S(i).name, '/'); % training set of shreds
    matFile = strcat('ground_truth/ground_truth/', S(i).name, '.mat'); % ground truth

    [order, orientation, time, recovered_page] = assemble_shreds(shred_directory); % run
    [score, longest, total] = eval_order_v3(order, orientation, matFile); % eval
    
    % save output
%     scores = [scores, score]; longests = [longests, longest]; totals = [totals, total];

    gt_order = load(matFile).order;
    gt_orientation = load(matFile).orientation;
    
    % cache
    outs(i).score = score;
    outs(i).longest = longest;
    outs(i).total = total;
    outs(i).recovered_page = recovered_page;
    outs(i).time = time;
    
end

%% PLOT

scores = [outs(:).score];
longests = [outs(:).longest];
totals = [outs(:).total];
times = [outs(:).time];
recovered_pages = {outs(:).recovered_page};

save('scores.mat', 'scores')
save('longest.mat', 'longest')
save('totals.mat', 'totals')
save('times.mat', 'times')
save('pages.mat', 'recovered_pages')

% % 
% h1 = figure('visible','off');
% bar(scores);
% % hold on;
% % plot(xlim,[mean(scores) mean(scores)], 'r')
% saveas(h1,'scores.png');
% 
% h2 = figure('visible','off');
% bar(longests);
% hold on;
% plot(xlim,[mean(longests) mean(longests)], 'r')
% saveas(h2,'longests.png');
% 
% h3 = figure('visible','off');
% bar(totals);
% hold on;
% plot(xlim,[mean(totals) mean(totals)], 'r')
% saveas(h3,'totals.png');
% 
% h4 = figure('visible','off');
% bar(times);
% hold on;
% plot(xlim,[mean(times) mean(times)], 'r')
% saveas(h4,'times.png');