% This program is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
% GNU General Public License for more details.
% You should have received a copy of the GNU General Public License
% along with this program.  If not, see <https://www.gnu.org/licenses/>

% Author: Ilya Belevich, University of Helsinki (ilya.belevich @ helsinki.fi)
% part of Microscopy Image Browser, http:\\mib.helsinki.fi 
% Date: 25.04.2023

function mibShowMilestoneDialog(obj, mode)
% function mibShowMilestoneDialog(obj, mode)
% show a milestone dialog with user performance and stats
%
% Parameters:
% mode: [string], optional, when 
% @li 'currentStats' - show the current stats
% @li 'milestoneReached' - show congratulations with the milestone reach (default)
%
% Return values
%

% Updates
% 
global mibPath;

if nargin < 2; mode = 'milestoneReached'; end

currTime = datetime('now');
elapsedTime = currTime - obj.mibModel.preferences.Users.Tiers.logStartDate;

statsBlock = sprintf(['     Loaded datasets: %d\n' ...
                      '     Image filtered: %d\n' ...
                      '     Batch processings: %d\n' ...
                      '     Snaps and movies: %d\n' ...
                      '     Shortcuts pressed: %d\n\n' ...
                      'Tools:\n' ...
                      '     3D balls: %d\n', ...
                      '     3D lines: %d\n', ...
                      '     Annotations: %d\n', ...
                      '     BW thresholdings: %d\n', ...
                      '     Drag-and-drop materials: %d\n', ...
                      '     Lassos: %d\n', ...
                      '     Magic wands: %d\n', ...
                      '     Object pickers: %d\n', ...
                      '     Membrane trackers: %d\n', ...
                      '     Segment anything: %d\n', ...
                      '     Spots: %d\n', ...
                      '     Graphcuts: %d\n', ...
                      '     Trained CNN networks: %d\n', ...
                      '     Inferenced CNN datasets: %d\n', ...
                      '     Measurements placed: %d\n', ...
                      '     Statistics calculated: %d\n', ...
                      ], ...
                      obj.mibModel.preferences.Users.Tiers.numberOfLoadedDatasets, ...
                      obj.mibModel.preferences.Users.Tiers.numberOfImageFilterings, ...
                      obj.mibModel.preferences.Users.Tiers.numberOfBatchProcessings, ...
                      obj.mibModel.preferences.Users.Tiers.numberOfSnapAndMovies, ...
                      obj.mibModel.preferences.Users.Tiers.numberOfKeyShortcuts, ...
                      obj.mibModel.preferences.Users.Tiers.numberOfBall3D, ...
                      obj.mibModel.preferences.Users.Tiers.numberOfLine3D, ...
                      obj.mibModel.preferences.Users.Tiers.numberOfAnnotations, ...
                      obj.mibModel.preferences.Users.Tiers.numberOfBWThresholdings, ...
                      obj.mibModel.preferences.Users.Tiers.numberOfDragDropMaterials, ...
                      obj.mibModel.preferences.Users.Tiers.numberOfLassos, ...
                      obj.mibModel.preferences.Users.Tiers.numberOfMagicWands, ...
                      obj.mibModel.preferences.Users.Tiers.numberOfObjectPickers, ......
                      obj.mibModel.preferences.Users.Tiers.numberOfMembraneClickTrackers, ...
                      obj.mibModel.preferences.Users.Tiers.numberOfSAMclicks, ...
                      obj.mibModel.preferences.Users.Tiers.numberOfSpots, ...
                      obj.mibModel.preferences.Users.Tiers.numberOfGraphcuts, ...
                      obj.mibModel.preferences.Users.Tiers.numberOfTrainedDeepNetworks, ...
                      obj.mibModel.preferences.Users.Tiers.numberOfInferencedDeepNetworks, ...
                      obj.mibModel.preferences.Users.Tiers.numberOfMeasurements, ...
                      obj.mibModel.preferences.Users.Tiers.numberOfGetStats);

switch mode
    case 'milestoneReached'
        % update the MIB title
        title = ['Microscopy Image Browser ' obj.mibVersion];
        if isdeployed; title = [title ' deployed version'];  end
        title = [title '    level ' obj.mibModel.preferences.Users.tierLevelRanks{min(obj.mibModel.preferences.Users.Tiers.tierLevel, numel(obj.mibModel.preferences.Users.tierLevelRanks))}];
        obj.mibView.gui.Name = title;
        
        dlgTitle = 'Milestone!';

        options.Title = sprintf( ...
            ['You have reached a milestone!!!\n\n' ...
            'Congratulations!\n' ...
            'You just unlocked a new tier:\n' ...
            '                    Level %s\n\n' ...
            'You have been\n' ...
            '   - brushing with MIB for %.3f kilometers\n' ...
            '   - moving mouse in MIB for %.3f kilometers\n' ...
            'and it took you approximately %s\n\n' ...
            'Stats:\n' ...
            '%s'], ...
            obj.mibModel.preferences.Users.tierLevelRanks{min(obj.mibModel.preferences.Users.Tiers.tierLevel, numel(obj.mibModel.preferences.Users.tierLevelRanks))}, ...
            obj.mibModel.preferences.Users.Tiers.brushTravelDistance/1000, ...
            obj.mibModel.preferences.Users.Tiers.mouseTravelDistance/1000, ...
            duration(elapsedTime, 'Format', 'd'), ...
            statsBlock);
        options.TitleLines = 33;                   % [optional] make it twice tall, number of text lines for the title
        options.WindowWidth = 1.8;
        options.WindowStyle = 'modal';
    case 'currentStats'
        rng('shuffle');
        dlgTitles = {'Work hard, succeed big!'; ...
                     'No pain, no gain!'; ...
                     'Push yourself, achieve more!'; ...
                     'Sweat, conquer, repeat!'; ...
                     'Dream big, work harder!'};
        dlgTitle = dlgTitles{randi(numel(dlgTitles))};

        if obj.mibModel.preferences.Users.Tiers.tierLevel == 1
            currentTierPoints = 0;
        else
            currentTierPoints = obj.mibModel.preferences.Users.tierPointsCoef * 2^(obj.mibModel.preferences.Users.Tiers.tierLevel-1);
        end
        collectedCurrentTierPoints = obj.mibModel.preferences.Users.Tiers.collectedPoints - currentTierPoints;
        nextTierPoints = obj.mibModel.preferences.Users.tierPointsCoef * 2^(obj.mibModel.preferences.Users.Tiers.tierLevel);
        toNextTierPoints = nextTierPoints - currentTierPoints;

        options.Title = sprintf( ...
            ['You current tier: %s\n' ...
            'Current score: %.3f (%.2f%% progression until next tier [%d]) \n\n' ...
            'You have been\n' ...
            '   - brushing with MIB for %.3f kilometers\n' ...
            '   - moving mouse in MIB for %.3f kilometers\n' ...
            'and it took you approximately %s\n\n' ...
            'Stats:\n' ...
            '%s'], ...
            obj.mibModel.preferences.Users.tierLevelRanks{min(obj.mibModel.preferences.Users.Tiers.tierLevel, numel(obj.mibModel.preferences.Users.tierLevelRanks))}, ...
            obj.mibModel.preferences.Users.Tiers.collectedPoints, collectedCurrentTierPoints/toNextTierPoints*100, nextTierPoints, ...
            obj.mibModel.preferences.Users.Tiers.brushTravelDistance/1000, ...
            obj.mibModel.preferences.Users.Tiers.mouseTravelDistance/1000, ...
            duration(elapsedTime, 'Format', 'd'), ...
            statsBlock);
        options.TitleLines = 30;                   % [optional] make it twice tall, number of text lines for the title
        options.WindowWidth = 1.8;
        options.WindowStyle = 'normal';
end
options.okBtnText = 'OMG!';
options.msgBoxOnly = true; % [optional] mimic message box without input fields

options.Icon = 'celebrate'; % [optional] icon to use: "question" (default), "celebrate"

mibInputMultiDlg({mibPath}, [], [], dlgTitle, options);

end
