function open_gui()
global handles
global mainW
global folder
global setFrame
% initial parameters
setFrame = [1 1];
folder = 'C:\';
addpath(genpath(pwd));
%% MAIN WINDOW
% Creating the main window, setting dimensions, color, etc.
dispSize = get(0,'ScreenSize');
figSize = [800, 600];
mainW = figure('pos', [(dispSize(3)-dispSize(1))/10 ...
    (dispSize(4)-dispSize(2))/10 figSize(1) figSize(2)]);
set(mainW,'Name','Set the data file path', ...
    'NumberTitle','off','Color',[0.9 0.9 0.9]);
%% CONTROLS
% Creating controls and their properties
text1 = uicontrol('Units', 'Normalized','FontUnits','Normalized');
set (text1, 'Style', 'Text', ...
    'Position', [.1 .9 .1 .03], 'FontSize', 0.6,...
    'String', 'Selected path', 'BackgroundColor',[0.9 0.9 0.9],...
    'ForegroundColor',[0.015, 0.149, 0.250],'HorizontalAlignment', 'left');
SelPath = uicontrol('Units', 'Normalized');
set (SelPath, 'Style', 'Text', 'FontUnits','Normalized', ...
    'Position', [.1 .86 .5 .03], 'FontSize', 0.8, 'ForegroundColor',[0.015, 0.149, 0.250], ...
    'String', 'C:\', 'HorizontalAlignment', 'left', 'BackgroundColor',[1 1 1]);
select_button = uicontrol('Style','Pushbutton', 'Units', 'Normalized');
set(select_button, 'FontUnits','Normalized', 'String', 'Select a path', ...
    'Position', [.65 .85 .1 .05], 'Callback',@SelectPath,'FontSize', 0.4, ...
    'Visible', 'on', 'Enable', 'on', 'ForegroundColor',[1 1 1],...
    'BackgroundColor',[0.015, 0.149, 0.250],'FontWeight','bold')
uicontrol('Style','Pushbutton', 'Units', 'Normalized',...
    'FontUnits','Normalized', 'String', 'Check scans', ...
    'Position', [.65 .7 .1 .05], 'Callback',@CheckScans,'FontSize', 0.4, ...
    'Visible', 'on', 'Enable', 'on', 'ForegroundColor',[1 1 1],...
    'BackgroundColor',[0.015, 0.149, 0.250],'FontWeight','bold')
text2 = uicontrol('Units', 'Normalized');
set (text2, 'Style', 'Text','FontUnits','Normalized','FontSize',0.6,...
    'Position', [.1 .75 .2 .03], 'ForegroundColor',[0.015, 0.149, 0.250], ...
    'String', 'Write the numbers of scans', 'BackgroundColor',[0.9 0.9 0.9],...
    'HorizontalAlignment', 'left');
SelScan = uicontrol('Units', 'Normalized');
set (SelScan, 'Style', 'Edit',  'ForegroundColor',[0.015, 0.149, 0.250],...
    'Position', [.1 .7 .3 .04], 'String', '','BackgroundColor',[1 1 1],...
    'FontUnits','Normalized','FontSize',0.6);
text3 = uicontrol('Units', 'Normalized');
set (text3, 'Style', 'Text', 'HorizontalAlignment', 'left', 'FontUnits', ...
    'Normalized','FontSize',0.6, ...
    'Position', [.1 .63 .125 .03], 'ForegroundColor',[0.015, 0.149, 0.250], ...
    'String', 'Type of Sequence', 'BackgroundColor',[.9 .9 .9]);
SeqPop = uicontrol('Units','Normalized', 'Style', 'Popupmenu', 'BackgroundColor',[1 1 1],...
    'FontUnits','Normalized', 'ForegroundColor',[0.015, 0.149, 0.250],'UserData','0',...
    'FontSize',0.6, 'String', {'IR FLASH - TI';'IntraGate - FA'}, 'CallBack',@ParChange);
set (SeqPop, 'Position', [.37 .58 .2 .03])

text4 = uicontrol('Units', 'Normalized');
set (text4, 'Style', 'Text', 'HorizontalAlignment', 'left', 'FontUnits', ...
    'Normalized','FontSize',0.6, 'ForegroundColor',[0.015, 0.149, 0.250],...
    'BackgroundColor',[.9 .9 .9],'Position', [.35 .63 .18 .03], 'Parent', mainW, ...
    'String', 'Parameters of scanning');

paraG = uibuttongroup('visible','on', 'Units', 'Normalized', ...
    'Position',[.1 .51 .25 .1],'BackgroundColor',[.9 .9 .9],...
    'ShadowColor',[0.015, 0.149, 0.25]);
uicontrol('Style','Radio','String','manually', 'Units', 'Normalized',...
    'BackgroundColor',[.9 .9 .9],'pos',[.1 .25 .4 .5],'parent',...
    paraG,'HandleVisibility','off','ForegroundColor',[0.015, 0.149, 0.250]);
uicontrol('Style','Radio','String','from file', 'Units', 'Normalized',...
    'BackgroundColor',[.9 .9 .9],'pos',[.5 .25 .4 .5],'parent',...
    paraG,'HandleVisibility','off','ForegroundColor',[0.015, 0.149, 0.250]);
set(paraG,'SelectionChangeFcn',@SelPar,  'SelectedObject', []);

text5 = uicontrol('Units', 'Normalized');
set (text5, 'Style', 'Text', ...
    'Position', [.1 .4 .1 .03], 'ForegroundColor',[0.015, 0.149, 0.25], ...
    'String', 'TR [ms]', 'BackgroundColor',[.9 .9 .9]);
TR = uicontrol('Units', 'Normalized');
set (TR, 'Style', 'Edit', 'BackgroundColor',[1 1 1], 'ForegroundColor',[0.015, 0.149, 0.25],...
    'Position', [.2 .4 .05 .03]);

text6 = uicontrol('Units', 'Normalized');
set (text6, 'Style', 'Text', 'BackgroundColor',[.9 .9 .9], 'ForegroundColor',[0.015, 0.149, 0.25],...
    'Position', [.3 .4 .1 .03], 'Tag', 'title', 'String', 'TI [ms]');
TI = uicontrol('Units', 'Normalized');
set (TI, 'Style', 'Edit', 'BackgroundColor',[1 1 1], 'ForegroundColor',[.015, .149, .25],...
    'Position', [.4 .4 .3 .03]);

para_button = uicontrol('Style','Pushbutton', 'Units', 'Normalized');
set(para_button, 'String', 'Get parameters', 'Position', [.45 .5 .15 .05], 'Callback',@GetPar, ...
    'Visible', 'on', 'Enable', 'off','ForegroundColor',[1 1 1],'FontUnits','Normalized',...
    'BackgroundColor',[.015, .149, .25],'FontSize',.5)

ROI_button = uicontrol('Style','Pushbutton', 'Units', 'Normalized');
set(ROI_button, 'String', 'Set the ROI', 'Position', [.1 .3 .15 .05], 'Callback',@getROI, ...
    'Visible', 'on', 'Enable', 'on','ForegroundColor',[1 1 1],'FontUnits','Normalized',...
    'BackgroundColor',[.015, .149, .25],'FontSize',.5)

Frame_button = uicontrol('Style','Pushbutton', 'Units', 'Normalized');
set(Frame_button, 'String', 'Set the slice', 'Position', [.28 .3 .15 .05], 'Callback',@getFrame, ...
    'Visible', 'on', 'Enable', 'on','ForegroundColor',[1 1 1],'FontUnits','Normalized',...
    'BackgroundColor',[.015, .149, .25],'FontSize',.5)

analyze_button = uicontrol('Style','Pushbutton', 'Units', 'Normalized');
set(analyze_button, 'String', 'Get T1 map', 'Position', [.1 .2 .15 .05], 'Callback',@getT1map, ...
    'Visible', 'on', 'Enable', 'on','ForegroundColor',[1 1 1],'FontUnits','Normalized',...
    'BackgroundColor',[.015, .149, .25],'FontSize',.5,'FontWeight','bold')

handles = struct('SelPath', SelPath, 'SelScan', SelScan, ...
    'SeqPop', SeqPop, 'TI', TI, 'text6', text6, ...
    'TR', TR, 'para_button', para_button);

%%CALLBACK FUNCTIONS
function GetPar(hObject,eventdata)
% Function GetPar is a callback function of Get Parameters button. This
% function retrievers aquizition parameters from MRI data. This function
% calls function get_para.
global handles
global info
% retrieving user's defined data
SeqType = get(handles.SeqPop,'Value');
folder = get(handles.SelPath,'String');
scan = str2num(get(handles.SelScan,'String'));
err = 1;
% precautions
if strcmp(scan,'') || strcmp(folder,'') || isempty(scan) || isempty(folder)
    warndlg('Select a path and number of scans!','Warning')
else
    ind = length(scan);
    for i = 1:ind
        if ~isdir([folder '\' sprintf('%0.1d',scan(i))])
            err = err*0;
        end
    end
    % when the scan numbers and folder is not valid
    if ~err
        warndlg('Selected scans are invalid! Write down the right scans!','Warning')
    else
        %         calling function get_para to get the info structure
        info = get_para(folder,scan,SeqType);
        %         writing down the paramaters
        set(handles.TR,'String',info.acq.TR(1),'Style','Text')
        if SeqType == 1
            set(handles.TI,'String',num2str(info.acq.TI),'Style','Text')
        else
            set(handles.TI,'String',num2str(info.acq.FA),'Style','Text')
        end
    end
end

function CheckScans(hObject,eventdata)
% Function CheckScans is a callback function of Check Scans pushbutton. It
% controls if the folder and scan numbers are valid.
global handles
folder = get(handles.SelPath,'String');
scan = str2num(get(handles.SelScan,'String'));
% when the folder is not selected
if  strcmp(folder,'')
    warndlg('Select a correct path!','Warning')
    % when the scan numbers are incorrectly entered
elseif strcmp(scan,'') || isempty(scan)
    warndlg('Selected scans are not valid!','Warning')
else
    ind = length(scan);
    err = 1;
    for i = 1:ind
        %         cheking if the directory exist
        if ~isdir([folder '\' sprintf('%0.1d',scan(i))])
            err = err*0;
        end
    end
    if ~err
        warndlg('Selected scans are not valid!','Warning')
        set(handles.SeqPop,'UserData','0')
    else
        msgbox('Selected scans are valid.','OK');
        set(handles.SeqPop,'UserData','1')
    end
    
end

function SelPar(hObject,eventdata)
% Function SelPar is a callback function of a Select Parameter pop-up menu.
% It changes the properties of Get Parameters pushbutton and TR and TI edit
% text.
global handles
% if the sequence is FISP FID
if strcmp(get(eventdata.NewValue, 'String'),'from file')
    set(handles.para_button, 'Enable', 'on')
    set(handles.TR,'Style','Text')
    set(handles.TI,'Style','Text')
    % if the sequence is Intragate FLASH
else
    set(handles.para_button, 'Enable', 'off')
    set(handles.TR,'Style','Edit')
    set(handles.TI,'Style','Edit')
end

function getT1map(hObject,eventdata)
% Function getT1map is a callback function of Get T1 map pushbutton. This
% function calls analyzeT1 function that perform T1 analysis and creates
% the T1 map of ROI. The T1 map is visualized in T1 map window.
global info
global handles
global data1
global rectROI
global setFrame
SeqType = get(handles.SeqPop,'Value');
folder = get(handles.SelPath,'String');
scan = str2num(get(handles.SelScan,'String'));
err = 1;
% procautions
if strcmp(scan,'') || strcmp(folder,'') || isempty(scan)
    warndlg('Set the path and scan numbers!','Warning')
else
    ind = length(scan);
    if ind < 2
        warndlg('For analysis at least 2 scans are needed!','Warning')
    else
        for i = 1:ind
            if ~isdir([folder '\' sprintf('%0.1d',scan(i))])
                err = err*0;
            end
        end
        if ~err
            warndlg('Selected scans are invalid! Write down the right scans!','Warning')
        else
            data1 = get_data(folder,scan);
        end
        if isempty(info) || err
            TRvalue = get(handles.TR,'String');
            TIvalue = get(handles.TI,'String');
            if strcmp(TRvalue,'')
                warndlg('Enter the value of TR or get the parameters from file','Warning')
                err = err*0;
            elseif length(str2num(TRvalue))>1
                warndlg('The TR parameter has to be set just to one value!','Warning')
                err = err*0;
            else
                info.acq.TR = str2num(TRvalue);
            end
            if SeqType == 1
                if strcmp(TIvalue,'')
                    warndlg('Enter the values of TI or get them from file','Warning')
                    err = err*0;
                elseif length(str2num(TIvalue)) ~= length(data1)
                    warndlg('The number of scans and values of TI are not same!','Warning')
                    err = err*0;
                else
                    info.acq.TI = str2num(TIvalue);
                end
            else
                if strcmp(TIvalue,'')
                    warndlg('Enter the values of FA or get them from file','Warning')
                    err = err*0;
                elseif length(str2num(TIvalue)) ~= length(data1)
                    warndlg('The number of scans and values of FA are not same!','Warning')
                    err = err*0;
                else
                    info.acq.FA = str2num(TIvalue);
                end
            end
        end
        %  when everything checks out
        if err
            %         if ROI is not selected, analysis is performed for whole image
            if isempty(rectROI)
                ImSize = size(data1{1}{1});
                rectROI = ones(ImSize);
            end
            %         calling analyzeT1 function to get the T1 map
            T1_lsf = analyzeT1(data1,info,SeqType,rectROI,setFrame);
            %         calling T1map_gui function to visualize the map
            T1map_gui(T1_lsf)
            %         closing all opened files
            fclose('all');
        end
    end
end

function getROI(hObject,eventdata)
% Function getROI is a callback function of Get the ROI pushbutton. This
% function calls get_data function for retrieving image data and ROI_gui
% function for selecting rectangular region of interest.
global handles
global data1
err = 1;
folder = get(handles.SelPath,'String');
scan = str2num(get(handles.SelScan,'String'));
% procautions
if strcmp(scan,'') || strcmp(folder,'') || isempty(scan)
    warndlg('Set the path and scan numbers!','Warning')
else
    ind = length(scan);
    for i = 1:ind
        if ~isdir([folder '\' sprintf('%0.1d',scan(i))])
            err = err*0;
        end
    end
    if ~err
        warndlg('Selected scans are invalid! Write down the right scans!','Warning')
    else
        % calling function get_data that retrieves data1 with image data
        data1 = get_data(folder,scan);
        % calling function ROI_gui for ROI selection
        ROI_gui(data1);
    end
end

function getFrame(hObject,eventdata)
% Function getFrame is a callback function of Get the Frame pushbutton.
% This function checks all needed parameters and their validity and calls
% function frame_gui for the slice selection.
global handles
global data1
SeqType = get(handles.SeqPop,'Value');
err = 1;
folder = get(handles.SelPath,'String');
scan = str2num(get(handles.SelScan,'String'));
% procautions
if strcmp(scan,'') || strcmp(folder,'') || isempty(scan)
    warndlg('Set the path and scan numbers!','Warning')
else
    ind = length(scan);
    for i = 1:ind
        if ~isdir([folder '\' sprintf('%0.1d',scan(i))])
            err = err*0;
        end
    end
    if ~err
        warndlg('Selected scans are invalid! Write down the right scans!','Warning')
    else
        % if data1 is empty, calling get_data to retrieve it
        data1 = get_data(folder,scan);
        % calling frame_gui function for slice selection
        frame_gui(data1,SeqType);
    end
end

function SelectPath(hObject,eventdata)
% Function SelectPath is a callback function of Select the Path pushbutton.
% This function allows the user to select a path where scans are located.
global folder
global handles
% if the folder is not yet initialized, it is set to the C:\
if folder == 0
    folder = 'C:\';
end
folder = uigetdir(folder,'Choose a folder with scans:');
% writing the path to the edit text
if folder ~= 0
    set(handles.SelPath,'String',folder)
end

function ParChange(hObject, eventdata)
% Function ParChange is a callback function of sequence pop-up menu. This
% function changes the String property of text6 field.
global handles
Par = get(hObject,'Value');
if Par == 1
    set(handles.text6,'String','TI [ms]')
else
    set(handles.text6,'String','FA [°]')
end