function varargout = Skenery_vyhodnoceni(varargin)
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @Skenery_vyhodnoceni_OpeningFcn, ...
                   'gui_OutputFcn',  @Skenery_vyhodnoceni_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before Skenery_vyhodnoceni is made visible.
function Skenery_vyhodnoceni_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to Skenery_vyhodnoceni (see VARARGIN)

% Choose default command line output for Skenery_vyhodnoceni
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes Skenery_vyhodnoceni wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = Skenery_vyhodnoceni_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;

function [sortedResultsTable, status] = processData()
scannerDataTable = getappdata(0, 'scannerDataTable');
scannersCount = size(scannerDataTable, 1);
if scannersCount < 1
    errordlg('Vstupni soubor neobsahuje zadna data.', 'modal');
    sortedResultsTable = 0;
    status = 0;
    return;
end

scannerLimitsTable = getappdata(0, 'scannerLimitsTable');
limitsCount = size(scannerLimitsTable, 1);
if limitsCount < 1
    errordlg('Soubor s limity neobsahuje zadna data.', 'modal');
    sortedResultsTable = 0;
    status = 0;
    return;
end

resultsTable = getappdata(0, 'scannerDataTable');
scannerWeightsTable = getappdata(0, 'scannerWeightsTable');
criteriaCount = size(scannerWeightsTable, 2) - 1; % prvni sloupec jsou nazvy skeneru
if criteriaCount < 1
    errordlg('Nebyla zadana zadna kriteria.', 'modal');
    sortedResultsTable = 0;
    status = 0;
    return;
end

for i = 1:criteriaCount
    col = scannerDataTable(:,i+1); % v prvnim sloupci jsou nazvy
    min = scannerLimitsTable{1,i+1}; % v prvnim sloupci jsou nazvy
    weight = scannerWeightsTable{1,i+1}; % v prvnim sloupci jsou nazvy
    criteriaRank = processSingleCriteria(col, min, weight);
    rank(:,i) = criteriaRank;
    resultsTable{:,i + 1} = criteriaRank; % v prvnim sloupci jsou nazvy
end
total = sum(rank,2); % scitam radek
resultsTable{:,criteriaCount + 2} = total; % +2 protoze +1 za nazvy souboru 
                                           % a +1 za pridani sloupce na konec
resultsTable.Properties.VariableNames(criteriaCount + 2) = {'Celkem'};
sortedResultsTable = sortrows(resultsTable, size(resultsTable, 2),'descend');
status = 1;
    

function rank = processSingleCriteria(criteriaColumn, criteriaMin, criteriaWeight)
columnMin = criteriaMin;
realColumnMin = min(criteriaColumn{:,1});
columnMax = max(criteriaColumn{:,1});
dataRange = columnMax - columnMin;
realDataRange = columnMax - realColumnMin;

for i = 1:size(criteriaColumn, 1)
    if realDataRange == 0
        rank(i,1) = 0;
    else
        % redukce k minimu
        % vydeleni rozsahem - dostanu cislo od 0 do 1
        % *9 - dostanu cislo od 0 do 9
        % +1 - od 1 do 10
        % *vaha
        rank(i,1) = round(criteriaWeight * ((criteriaColumn{i,1} - columnMin)/dataRange*9 + 1));
    end
end


% --- Executes on button press in resultGenerator.
function resultGenerator_Callback(hObject, eventdata, handles)
% hObject    handle to resultGenerator (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

[resultsTable, status] = processData();
if status == 1
    fig = uifigure('Position',[100 100 800 300]);
    uitable(fig,'Data',resultsTable,'Position',[25 25 700 200]);
end

% smazani aplikacnich dat
%if isappdata(0, 'scannerDataTable')
%    rmappdata(0, 'scannerDataTable')
%end
%if isappdata(0, 'scannerWeightsTable')
%    rmappdata(0, 'scannerWeightsTable')
%end
%if isappdata(0, 'scannerLimitsTable')
%    rmappdata(0, 'scannerLimitsTable')
%end
    


% --- Executes on button press in loadData.
function loadData_Callback(hObject, eventdata, handles)
% hObject    handle to loadData (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
[FILENAME, PATHNAME] = uigetfile('*.csv');
scannerDataFilePath = [PATHNAME, FILENAME];
%scannerDataFilePath = 'c:\Users\kristi\Documents\MATLAB\Skenery_data.csv';
%FILENAME = 'Skenery_data.csv';
filenameParts = strsplit(FILENAME, '.');
suffix = filenameParts{1, end};
if strcmp(suffix, 'csv')
    opts = detectImportOptions(scannerDataFilePath);
    scannerDataTable = readtable(scannerDataFilePath, opts);
    %msgbox('Vstupní data načtena.');
    setappdata(0, 'scannerDataTable', scannerDataTable);  
else
    errordlg('Zvoleny soubor ma chybny format.', 'modal')
end


% --- Executes on button press in loadWeights.
function loadWeights_Callback(hObject, eventdata, handles)
% hObject    handle to loadWeights (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
[FILENAME, PATHNAME] = uigetfile('*.csv');
scannerWeightsFilePath = [PATHNAME, FILENAME];
filenameParts = strsplit(FILENAME, '.');
suffix = filenameParts{1, end};
if strcmp(suffix, 'csv')
    opts = detectImportOptions(scannerWeightsFilePath);
    scannerWeightsTable = readtable(scannerWeightsFilePath, opts);
    %msgbox('Vahy načteny.');
    setappdata(0, 'scannerWeightsTable', scannerWeightsTable);  
else
    errordlg('Zvoleny soubor ma chybny format.', 'modal')
end


% --- Executes on button press in loadLimits.
function loadLimits_Callback(hObject, eventdata, handles)
% hObject    handle to loadLimits (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
[FILENAME, PATHNAME] = uigetfile('*.csv');
scannerLimitsFilePath = [PATHNAME, FILENAME];
filenameParts = strsplit(FILENAME, '.');
suffix = filenameParts{1, end};
if strcmp(suffix, 'csv')
    opts = detectImportOptions(scannerLimitsFilePath);
    scannerLimitsTable = readtable(scannerLimitsFilePath, opts);
    %msgbox('Limity načteny.');
    setappdata(0, 'scannerLimitsTable', scannerLimitsTable);  
else
    errordlg('Zvoleny soubor ma chybny format.', 'modal')
end
