function T1map_gui(T1_lsf)
global handles
global ColorAxis
global map
global folder
map = T1_lsf;

%% MAIN WINDOW
% Creating the main window, setting dimensions, color, etc.
dispSize = get(0,'ScreenSize');
figSize = [1000, 500];
mainW = figure('pos', [(dispSize(3)-dispSize(1))/10 ...
    (dispSize(4)-dispSize(2))/10 figSize(1) figSize(2)]);
set(mainW,'Name','T1 map', 'Color', [.9 .9 .9],...
    'NumberTitle','off');

%% AXES
% Plotting the created T1 map, setting the color axis.
map_axes = axes('Units', 'Normalized', 'Position', [.001 .1 .6 .8]);
imshow(T1_lsf,[])
h = colorbar;
colormap('jet')
ColorAxis = [500 2500];
caxis(ColorAxis)
ylabel(h,'T_1 [ms]')

%% CONTROLS
% Creating controls and their properties
uicontrol('Units', 'Normalized','FontUnits','Normalized',...
    'Style', 'Text', ...
    'Position',[.7 .85 .15 .03],'FontSize', 0.7,...
    'String', 'Select limits for the colormap', 'BackgroundColor',[0.9 0.9 0.9],...
    'ForegroundColor',[0.015, 0.149, 0.250],'HorizontalAlignment', 'left');
uicontrol('Units', 'Normalized','FontUnits','Normalized',...
    'Style', 'Text', ...
    'Position',[.7 .78 .08 .03],'FontSize', 0.6,...
    'String', 'minimum [ms]', 'BackgroundColor',[0.9 0.9 0.9],...
    'ForegroundColor',[0.015, 0.149, 0.250],'HorizontalAlignment', 'left');
uicontrol('Units', 'Normalized','FontUnits','Normalized',...
    'Style', 'Text', ...
    'Position',[.8 .78 .08 .03],'FontSize', 0.6,...
    'String', 'maximum [ms]', 'BackgroundColor',[0.9 0.9 0.9],...
    'ForegroundColor',[0.015, 0.149, 0.250],'HorizontalAlignment', 'left');
Cmin = uicontrol('Units', 'Normalized','FontUnits','Normalized',...
    'Style', 'Edit', ...
    'Position',[.7 .74 .08 .04],'FontSize', 0.8,...
    'String',num2str(ColorAxis(1)), 'BackgroundColor',[1 1 1],...
    'ForegroundColor',[0.015, 0.149, 0.250],'CallBack', @Cmin_callback);
Cmax = uicontrol('Units', 'Normalized','FontUnits','Normalized',...
    'Style', 'Edit', ...
    'Position',[.8 .74 .08 .04],'FontSize', 0.8,...
    'String',num2str(ColorAxis(2)), 'BackgroundColor',[1 1 1],...
    'ForegroundColor',[0.015, 0.149, 0.250],'CallBack', @Cmax_callback);

SaveM = uibuttongroup('visible','on', 'Units', 'Normalized', ...
    'Position',[.6 .5 .25 .15], 'BackgroundColor',[.9 .9 .9],...
    'BorderType','none');
uicontrol('Units', 'Normalized','FontUnits','Normalized',...
    'Style', 'Radio','parent',SaveM,'Tag','1',...
    'Position',[.1 .2 .4 .5],'FontSize', 0.3,...
    'String', 'as an image', 'BackgroundColor',[0.9 0.9 0.9],...
    'ForegroundColor',[0.015, 0.149, 0.250],'HorizontalAlignment', 'left');
uicontrol('Units', 'Normalized','FontUnits','Normalized',...
    'Style', 'Radio','parent',SaveM,'Tag','2',...
    'Position',[.5 .2 .4 .5],'FontSize', 0.3,...
    'String', 'as a matrix', 'BackgroundColor',[0.9 0.9 0.9],...
    'ForegroundColor',[0.015, 0.149, 0.250],'HorizontalAlignment', 'left');
uicontrol('Units', 'Normalized','FontUnits','Normalized',...
    'Style', 'Text','parent',SaveM,...
    'Position',[.05 .7 .5 .2],'FontSize', 0.8,...
    'String', 'Save the T1 map', 'BackgroundColor',[0.9 0.9 0.9],...
    'ForegroundColor',[0.015, 0.149, 0.250],'HorizontalAlignment', 'left');

uicontrol('Units', 'Normalized','FontUnits','Normalized',...
    'Style', 'Text', ...
    'Position',[.65 .5 .08 .03],'FontSize', 0.8,...
    'String', 'New file name', 'BackgroundColor',[0.9 0.9 0.9],...
    'ForegroundColor',[0.015, 0.149, 0.250],'HorizontalAlignment', 'left');

name = uicontrol('Units', 'Normalized','FontUnits','Normalized',...
    'Style', 'Edit', ...
    'Position',[.75 .5 .08 .03],'FontSize', 0.8,...
    'String', 'T1map', 'BackgroundColor',[1 1 1],...
    'ForegroundColor',[0.015, 0.149, 0.250]);

uicontrol('Units', 'Normalized','FontUnits','Normalized',...
    'Style', 'Text', ...
    'Position',[.65 .45 .08 .03],'FontSize', 0.8,...
    'String', 'Path', 'BackgroundColor',[.9 .9 .9],...
    'ForegroundColor',[0.015, 0.149, 0.250],'HorizontalAlignment','le');
uicontrol('Units', 'Normalized','FontUnits','Normalized',...
    'Style', 'PushButton', ...
    'Position',[.76 .45 .06 .035],'FontSize', 0.8,...
    'String', 'Select', 'BackgroundColor',[0.015, 0.149, 0.250],...
    'ForegroundColor',[1 1 1],'CallBack',@SelectPath);

path = uicontrol('Units', 'Normalized','FontUnits','Normalized',...
    'Style', 'Text', 'HorizontalAlignment','left',...
    'Position',[.65 .4 .3 .03],'FontSize', 0.8,...
    'String', folder, 'BackgroundColor',[1 1 1],...
    'ForegroundColor',[0.015, 0.149, 0.250]);

uicontrol('Units', 'Normalized','FontUnits','Normalized',...
    'Style', 'PushButton', ...
    'Position',[.89 .33 .06 .04],'FontSize', 0.7,...
    'String', 'SAVE', 'BackgroundColor',[0.015, 0.149, 0.250],...
    'ForegroundColor',[1 1 1],'CallBack',@SaveIm);

uicontrol('Units', 'Normalized','FontUnits','Normalized',...
    'Style', 'PushButton', ...
    'Position',[.75 .65 .1 .05],'FontSize', 0.6,...
    'String', 'Analyse ROI', 'BackgroundColor',[0.015, 0.149, 0.250],...
    'ForegroundColor',[1 1 1],'CallBack',@AnalyseROI);

handles.T1map = struct('Cmin', Cmin, 'Cmax', Cmax, 'name', name, ...
    'path', path, 'map_axes', map_axes, 'SaveM', SaveM);


%%CALLBACK FUNCTIONS
function AnalyseROI(hObject,eventdata)
% Function AnalyseROI is a callback function of Analyse ROI pushbutton. The
% function allows setting the ROI and from the selected ROI does the
% statistical analysis and displays box plot from the ROI.
global map
ROIa = roipoly;
ROIa = map(ROIa);
dispSize = get(0,'ScreenSize');
figSize = [700, 500];
mainW = figure('pos', [(dispSize(3)-dispSize(1))/10 ...
    (dispSize(4)-dispSize(2))/10 figSize(1) figSize(2)]);
set(mainW,'Name','Analysis of T1 in selected ROI', ...
    'NumberTitle','off', 'Color', [.9 .9 .9]);
axes('Units', 'Normalized', 'Position', [.001 .1 .6 .8]);
boxplot(ROIa)
set(gca,'XTickLabel',{' '})
ylabel('T_1 [ms]')
% uicontrol('Units', 'Normalized','FontUnits','Normalized',...
%     'Style', 'Text', ...
%     'Position',[.7 .9 .15 .07],'FontSize', 0.7,...
%     'String', 'T1 analysis', 'BackgroundColor',[0.9 0.9 0.9],...
%     'ForegroundColor',[0.015, 0.149, 0.250],'HorizontalAlignment', 'left');
uicontrol('Units', 'Normalized','FontUnits','Normalized',...
    'Style', 'Text', ...
    'Position',[.65 .8 .2 .05],'FontSize', 0.5,...
    'String', 'mean value [ms]', 'BackgroundColor',[0.9 0.9 0.9],...
    'ForegroundColor',[0.015, 0.149, 0.250],'HorizontalAlignment', 'left');
uicontrol('Units', 'Normalized','FontUnits','Normalized',...
    'Style', 'Text', ...
    'Position',[.65 .7 .2 .05],'FontSize', 0.5,...
    'String', 'standart deviation [ms]', 'BackgroundColor',[0.9 0.9 0.9],...
    'ForegroundColor',[0.015, 0.149, 0.250],'HorizontalAlignment', 'left');
uicontrol('Units', 'Normalized','FontUnits','Normalized',...
    'Style', 'Text', ...
    'Position',[.65 .6 .2 .05],'FontSize', 0.5,...
    'String', 'minimal value [ms]', 'BackgroundColor',[0.9 0.9 0.9],...
    'ForegroundColor',[0.015, 0.149, 0.250],'HorizontalAlignment', 'left');
uicontrol('Units', 'Normalized','FontUnits','Normalized',...
    'Style', 'Text', ...
    'Position',[.65 .5 .2 .05],'FontSize', 0.5,...
    'String', 'maximal value [ms]', 'BackgroundColor',[0.9 0.9 0.9],...
    'ForegroundColor',[0.015, 0.149, 0.250],'HorizontalAlignment', 'left');
uicontrol('Units', 'Normalized','FontUnits','Normalized',...
    'Style', 'Text', ...
    'Position',[.85 .8 .05 .05],'FontSize', 0.6,...
    'String', num2str(round(mean(ROIa))), 'BackgroundColor',[0.9 0.9 0.9],...
    'ForegroundColor',[0.015, 0.149, 0.250],'HorizontalAlignment', 'right');
uicontrol('Units', 'Normalized','FontUnits','Normalized',...
    'Style', 'Text', ...
    'Position',[.85 .7 .05 .05],'FontSize', 0.6,...
    'String', num2str(round(std(ROIa))), 'BackgroundColor',[0.9 0.9 0.9],...
    'ForegroundColor',[0.015, 0.149, 0.250],'HorizontalAlignment', 'right');
uicontrol('Units', 'Normalized','FontUnits','Normalized',...
    'Style', 'Text', ...
    'Position',[.85 .6 .05 .05],'FontSize', 0.6,...
    'String', num2str(round(min(ROIa))), 'BackgroundColor',[0.9 0.9 0.9],...
    'ForegroundColor',[0.015, 0.149, 0.250],'HorizontalAlignment', 'right');
uicontrol('Units', 'Normalized','FontUnits','Normalized',...
    'Style', 'Text', ...
    'Position',[.85 .5 .05 .05],'FontSize', 0.6,...
    'String', num2str(round(max(ROIa))), 'BackgroundColor',[0.9 0.9 0.9],...
    'ForegroundColor',[0.015, 0.149, 0.250],'HorizontalAlignment', 'right');

function Cmin_callback(hObject,eventdata)
% Function Cmin_callback is a callback function of Cmin edit control. When
% the value is changed, function checks if the value is valid and then
% changes the color axis of T1 map.
global handles
global ColorAxis
OldMin = ColorAxis(1);
ColorMin = str2double(get(handles.T1map.Cmin,'String'));
ColorAxis(1) = ColorMin;
if ColorAxis(2)>ColorAxis(1) 
    caxis(ColorAxis)
elseif isnan(ColorMin); % if there is no entered value
    warndlg('Please enter the value of the minimum.','Warning')
else % if the entered value is bigger than the enetered maximum value
    warndlg('The value of the maximum has to be bigger than minimum.','Warning')
    set(handles.T1map.Cmax,'String',num2str(OldMin));
end

function Cmax_callback(hObject,eventdata)
% Function Cmax_callback is a callback function of Cmax edit control. When
% the value is changed, function checks if the value is valid and then
% changes the color axis of T1 map.
global handles
global ColorAxis
OldMax = ColorAxis(2);
ColorMax = str2double(get(handles.T1map.Cmax,'String'));
ColorAxis(2) = ColorMax;
if ColorAxis(2)>ColorAxis(1)
    caxis(ColorAxis)
elseif isnan(ColorMax); % if there is no entered value
    warndlg('Please enter the value of the minimum.','Warning')
else % if the entered value is smaller than the enetered minimum value
    warndlg('The value of the maximum has to be bigger than minimum.','Warning')
    set(handles.T1map.Cmax,'String',num2str(OldMax));
end

function SaveIm(hObject,eventdata,T1_lsf)
% Function SaveIm is a callback function of SAVE push button. Function
global handles
global ColorAxis
global map
style = get(get(handles.T1map.SaveM,'SelectedObject'),'Tag');
folder = get(handles.T1map.path,'String');
name = get(handles.T1map.name,'String');
if isempty(name) || strcmp(name,'')
    warndlg('Please enter the name of new file.','Warning')
else
    if strcmp(style,'1')
        F=getframe(handles.T1map.map_axes); %select axes in GUI
        figure(); %new figure
        image(F.cdata); %show selected axes in new figure
        axis off
        colorbar
        caxis(ColorAxis)
        saveas(gcf, [folder  name], 'tiff'); %save figure
        close(gcf); %and close it
    else
        save([folder '\' name '.mat'], 'map')
    end
end


function SelectPath(hObject,eventdata)
global folder
global handles
folder = uigetdir(folder,'Choose a folder:');
if folder ~= 0
    set(handles.T1map.path,'String',folder)
end