%{ 
    gui.m
    Krystof Sara <xsarak00@stud.feec.vutbr.cz> 2019-2020

    Hlavni skript projektu v Matlabu. Predstavuje graficke uzivatelske
    prostredi pro praci s videozaznamem, jeho analyzu a ukladani hodnot TF 
    (HR) do souboru 'history.mat'. Tento soubor lze opet nacitat a zobrazovat
    tak vyvoj TF v ramci mnoha mereni. 
%}

function gui
close all; clearvars; clc

%% Globalni promenne

ui.show_data = false;
ui.ppg_hr_value = 0;
ui.ecg_hr_value = 0;
ui.processing_done = false;
ui.history_button_enable = 'off';
ui.history_file = false;
ui.history_button_down = false;
ui.show_tools = true;

% Debug mode
ui.debug = false;
ui.debug_ecg = false;

% Prepinac mezi prumernou a medianovou hodnotu TF
ui.show_average = false;

% Kontrola dostupnosti souboru 'history.mat'
if exist('history.mat', 'file') == 2
    ui.history_button_enable = 'on';
    ui.history_file = true;
end

% Nacteni zakladnich hodnot pro spravne vygenerovani a umisteni figury
obrazovka = get(0);
obrazovka = obrazovka.ScreenSize;
vyska = 700;
sirka = 800;

% Hlavni okno + parametry
ui.fig = figure('NumberTitle', 'off', 'Name', 'ABSE GUI',...
    'Position', [obrazovka(3)/2-(sirka/2) (obrazovka(4)/2)-vyska/2 sirka vyska],...
    'Color', [0.95 0.95 0.95], 'MenuBar', 'None', 'ToolBar', 'None', 'Units', 'Normal');

%% Ploty

ui.plots = gobjects(1, 3);

ui.plots(1) = axes('Position', [0.09 0.70 0.60 0.20]);
ui.plots(2) = axes('Position', [0.09 0.40 0.60 0.20]);
ui.plots(3) = axes('Position', [0.09 0.10 0.60 0.20]);

title(ui.plots(1), 'PPG Signal');
title(ui.plots(2), 'Intervalova Funkce');
title(ui.plots(3), 'EKG Signal')

xlabel(ui.plots(1), 'Vzorky [-]');
xlabel(ui.plots(2), 'Cas [s]');
xlabel(ui.plots(3), 'Vzorky [-]');

ylabel(ui.plots(1), 'Normalizovany signal PPG [-]');
ylabel(ui.plots(2), 'TF [min^{-1}]');
ylabel(ui.plots(3), 'Napeti [\muV]');

hold(ui.plots(1), 'on');
hold(ui.plots(2), 'on');
hold(ui.plots(3), 'on');

%% Pravy panel

ui.frame = uicontrol('Style', 'Frame', 'Position', [600 70 180 560],...
    'BackgroundColor', [0.85 0.85 0.85], 'Units', 'normalized');

ui.push_load = uicontrol('Style', 'Pushbutton', 'BackgroundColor', [0.85 0.85 0.85],...
    'Position', [650 550 80 30], 'Units', 'normalized', 'String', 'Nahrat Video', 'Callback', @load_data);

ui.push_ecg_load = uicontrol('Style', 'Pushbutton', 'BackgroundColor', [0.85 0.85 0.85],...
    'Position', [650 500 80 30], 'Units', 'normalized', 'String', 'Nahrat EKG', 'Callback', @load_ecg);

ui.push_tools = uicontrol('Style', 'Pushbutton', 'BackgroundColor', [0.85 0.85 0.85],...
    'Position', [650 200 80 30], 'Units', 'normalized', 'String', 'Nastroje', 'Callback', @show_tools);

% PPG HR
ui.text(1) = uicontrol('Style', 'Text', 'Visible', 'off', 'Position', [640 330 100 50],...
    'BackgroundColor', [0.7 0.7 0.7], 'FontSize', 15, 'Units', 'normalized');

% EKG HR
ui.text(2) = uicontrol('Style', 'Text', 'Visible', 'off', 'Position', [640 250 100 50],...
    'BackgroundColor', [0.7 0.7 0.7], 'FontSize', 15, 'Units', 'normalized');

ui.push_save = uicontrol('Style', 'Pushbutton', 'BackgroundColor', [0.85 0.85 0.85],...
     'Position', [650 150 80 30], 'Units', 'normalized', 'String', 'Ulozit', 'Callback', @save_history, 'Enable', 'off');

ui.push_history = uicontrol('Style', 'Pushbutton', 'BackgroundColor', [0.85 0.85 0.85],...
     'Position', [650 100 80 30], 'Units', 'normalized', 'String', 'Historie',...
     'Callback', @load_history, 'Enable', ui.history_button_enable);

%% Tlacitkove funkce

    % Nacteni videozaznamu PPG
    function load_data(~, ~)
        [file, folder] = uigetfile({'*.mov;*.3gp;*.mp4', 'Videozaznam [*.mov *.3gp *.mp4]'});
        
        if ~isfile([folder file]) || ~isfolder(folder)
            error('Not a valid file/folder.')
        end
        
        vidfile = fullfile(folder, file);
        cla(ui.plots(1));
        cla(ui.plots(2));
        set(ui.text(1), 'Visible', 'off');
        set(ui.push_save, 'Enable', 'off');
        title(ui.plots(2), 'Intervalova Funkce');
        xlabel(ui.plots(2), 'Cas [s]');
        ui = brightnessSignal(vidfile, ui);
        ui = processPPG(ui);
        if ui.processing_done
            set(ui.push_save, 'Enable', 'on');
        end
    end

    % Nacteni a zpracovani zaznamu EKG
    function load_ecg(~, ~)
        [file, folder] = uigetfile({'*.edf;*.EDF', 'Faros EKG Zaznam [*.EDF]'});
        
        ecgfile = fullfile(folder, file);
        
        if ~isfolder(folder) || ~isfile(ecgfile)
            return
        end
        
        cla(ui.plots(3));
        ui = loadECG(ecgfile, ui);
    end

    % Zobrazeni nastrojove listy
    function show_tools(~, ~)
        if ui.show_tools
            set(ui.fig, 'MenuBar', 'Figure', 'ToolBar', 'Figure');
            ui.show_tools = false;
        else
            set(ui.fig, 'MenuBar', 'None', 'ToolBar', 'None');
            ui.show_tools = true;
        end
    end

    % ULozeni historickych hodnot do souboru
    function save_history(~, ~)
        if ui.history_file
            temp = load('history.mat');
            history_values = temp.history_values;
            clear temp;
        else
            history_values = [];
        end
        
        % Debugging
        if ui.debug
            hr_video = ui.ppg_hr;
            framez = ui.framez;
            
            % Soubory nutne pro hodnoceni -> ecg_test.m
            save('hr_video.mat', 'hr_video');
            save('raw_framez.mat', 'framez');
            clear hr_video framez;
        end
        
        history_values = [history_values ui.ppg_hr_value];
        save('history.mat', 'history_values');
        clear history_values;
        ui.history_file = true;
        set(ui.push_save, 'Enable', 'off');
        set(ui.push_history, 'Enable', 'on');
    end

    % Nacteni historickych dat
    function load_history(~, ~)
        if ui.history_file && ~ui.history_button_down
            ui.history_button_down = true;
            set(ui.push_history, 'String', 'Int. Funkce');
            temp = load('history.mat');
            history_values = temp.history_values;
            clear temp;
            title(ui.plots(2), 'Predchozi Hodnoty');
            xlabel(ui.plots(2), 'Zaznam No. [-]');
            set(ui.text(1), 'String', sprintf('PPG TF\n  %.0f', mean(history_values)));
            set(ui.text(1), 'Visible', 'on');
            cla(ui.plots(2));
            xlim(ui.plots(2), [1 length(history_values)]);
            stem(history_values, 'Parent', ui.plots(2))
            
        elseif ui.history_button_down && ui.processing_done
            ui.history_button_down = false;
            set(ui.push_history, 'String', 'Historie');
            title(ui.plots(2), 'Intervalova Funkce');
            xlabel(ui.plots(2), 'Cas [s]');
            set(ui.text(1), 'String', sprintf('PPG TF\n  %.0f', ui.ppg_hr_value));
            set(ui.text(1), 'Visible', 'on');
            cla(ui.plots(2));
            stem(ui.peak_x(1:end-1)/ui.frate, ui.ppg_hr, '*', 'Parent', ui.plots(2))
            xlim(ui.plots(2), 'auto');
        end
    end
end