function frame_gui(data1,SeqType)
% Function frame_gui helps user to choose the ideal frame for T1 mapping.
%% INITIALIZATION
global handles
global data
global DataSize
global setFrame
if isempty(setFrame)
    setFrame = [1 1];
end
q = setFrame(2);
s = setFrame(1);
data = data1;
imNo1 = length(data1);
imNo2 = size(data1{1});
%% MAIN WINDOW
DataSize = [imNo1 imNo2];
dispSize = get(0,'ScreenSize');
figSize = [800, 500];
MainW = figure('pos', [(dispSize(3)-dispSize(1))/10 ...
    (dispSize(4)-dispSize(2))/10 figSize(1) figSize(2)]);
set(MainW,'Name','Setting the slice', 'Color',[0.9 0.9 0.9],...
    'NumberTitle','off');
%% AXES
axes('Units', 'Normalized', 'Position', [.001 .1 .8 .8]);
imshow(data1{1}{q,s},[])
%% CONTROLS
minusF_button = uicontrol('Style','Pushbutton','Units','Normalized','Position',[.8 .8 .02 .03],...
    'String','-','CallBack',@minusF_callback,'BackgroundColor',[0.015, 0.149, 0.250],...
    'ForegroundColor',[1 1 1]);
if q==1
    set(minusF_button,'Enable','off')
end
plusF_button = uicontrol('Style','Pushbutton','Units','Normalized','Position',[.87 .8 .02 .03],...
    'String','+','CallBack',@plusF_callback,'BackgroundColor',[0.015, 0.149, 0.250],...
    'ForegroundColor',[1 1 1]);
if q == DataSize(2)
    set(plusF_button,'Enable','off')
end
minusM_button = uicontrol('Style','Pushbutton','Units','Normalized','Position',[.8 .7 .02 .03],...
    'String','-','CallBack',@minusM_callback,'BackgroundColor',[0.015, 0.149, 0.250],...
    'ForegroundColor',[1 1 1]);
if s==1
    set(minusM_button,'Enable','off')
end
plusM_button = uicontrol('Style','Pushbutton','Units','Normalized','Position',[.87 .7 .02 .03],...
    'String','+','CallBack',@plusM_callback,'BackgroundColor',[0.015, 0.149, 0.250],...
    'ForegroundColor',[1 1 1]);
if s == DataSize(3)
    set(plusM_button,'Enable','off')
end
minusS_button = uicontrol('Style','Pushbutton','Units','Normalized','Position',[.8 .6 .02 .03],...
    'String','-','CallBack',@minusS_callback,'BackgroundColor',[0.015, 0.149, 0.250],...
    'ForegroundColor',[1 1 1],'Enable','off');
plusS_button = uicontrol('Style','Pushbutton','Units','Normalized','Position',[.87 .6 .02 .03],...
    'String','+','CallBack',@plusS_callback,'BackgroundColor',[0.015, 0.149, 0.250],...
    'ForegroundColor',[1 1 1],'Enable','on');

F = uicontrol('Style','Edit','Units','Normalized','FontUnits','Normalized', ...
    'Position', [.83 .8 .03 .03],'String',num2str(q),'FontSize',0.8,'CallBack',@F_callback,...
    'BackgroundColor',[1 1 1],'ForegroundColor',[0.015, 0.149, 0.250]);
M = uicontrol('Style','Edit','Units','Normalized','FontUnits','Normalized', ...
    'Position', [.83 .7 .03 .03],'String',num2str(s),'FontSize',0.8,'CallBack',@M_callback,...
    'BackgroundColor',[1 1 1],'ForegroundColor',[0.015, 0.149, 0.250]);
S = uicontrol('Style','Edit','Units','Normalized','FontUnits','Normalized', ...
    'Position', [.83 .6 .03 .03],'String','1','FontSize',0.8,'CallBack',@S_callback,...
    'BackgroundColor',[1 1 1],'ForegroundColor',[0.015, 0.149, 0.250]);

uicontrol('Style','Text','Units','Normalized','FontUnits','Normalized', ...
    'Position', [.68 .8 .07 .03],'String','Slice','FontSize',0.8,...
    'BackgroundColor',[.9 .9 .9],'ForegroundColor',[0.015, 0.149, 0.250],...
    'HorizontalAlignment','left');
uicontrol('Style','Text','Units','Normalized','FontUnits','Normalized', ...
    'Position', [.68 .7 .1 .03],'String','Cardiac slice','FontSize',0.8,...
    'BackgroundColor',[.9 .9 .9],'ForegroundColor',[0.015, 0.149, 0.250],...
    'HorizontalAlignment','left');
S_t = uicontrol('Style','Text','Units','Normalized','FontUnits','Normalized', ...
    'Position', [.68 .6 .1 .03],'FontSize',0.8,...
    'BackgroundColor',[.9 .9 .9],'ForegroundColor',[0.015, 0.149, 0.250],...
    'HorizontalAlignment','left');

if SeqType == 1
    set(S_t,'String','TI')
else
    set(S_t,'String','FA')
end

uicontrol('Style','Pushbutton','Units','Normalized','Position',[.7 .5 .2 .05],...
    'String','Set this frame for T1 mapping','CallBack',@set_callback,'BackgroundColor',[0.015, 0.149, 0.250],...
    'ForegroundColor',[1 1 1],'Enable','on');

handles.frame = struct('minusM_button',minusM_button,'minusF_button',minusF_button,...
    'plusF_button',plusF_button,'plusM_button',plusM_button,'F',F,'M',M,'S',S,...
    'minusS_button',minusS_button,'plusS_button',plusS_button,'MainW',MainW);
end
%% CALLBACK FUNCTIONS
function plusM_callback(hObject,eventdata)
% Function plusM_callback is a callback function of plus bushbutton.
global handles
global DataSize
global data
Mvalue = str2double(get(handles.frame.M,'String'));
Fvalue = str2double(get(handles.frame.F,'String'));
Svalue = str2double(get(handles.frame.S,'String'));
MvalueNew = num2str(Mvalue+1);
% setting the new value in the edit field
set(handles.frame.M,'String',MvalueNew)
% if the old value was 1, enabling the minus button
if strcmp(MvalueNew,'2')
    set(handles.frame.minusM_button,'Enable','on')
end
% if the new value is the same as the data size, disabling the minus button
if strcmp(MvalueNew,num2str(DataSize(3)))
    set(handles.frame.plusM_button,'Enable','off')
end
imshow(data{Svalue}{Fvalue,str2double(MvalueNew)},[])
end

function minusM_callback(hObject,eventdata)
% Function minusM_callback is a callback function of minus bushbutton.
global handles
global DataSize
global data
Mvalue = str2double(get(handles.frame.M,'String'));
Fvalue = str2double(get(handles.frame.F,'String'));
Svalue = str2double(get(handles.frame.S,'String'));
MvalueNew = num2str(Mvalue-1);
% setting the new value in the edit field
set(handles.frame.M,'String',MvalueNew)
% if the new value is 1, disabling the minus button
if strcmp(MvalueNew,'1')
    set(handles.frame.minusM_button,'Enable','off')
end
% if the new value is the same as the data size, enabling the plus button
if strcmp(MvalueNew,num2str(DataSize(3)-1))
    set(handles.frame.plusM_button,'Enable','on')
end
imshow(data{Svalue}{Fvalue,str2double(MvalueNew)},[])
end

function plusF_callback(hObject,eventdata)
% Function plusF_callback is a callback function of plus bushbutton.
global handles
global DataSize
global data
Mvalue = str2double(get(handles.frame.M,'String'));
Fvalue = str2double(get(handles.frame.F,'String'));
Svalue = str2double(get(handles.frame.S,'String'));
FvalueNew = num2str(Fvalue+1);
% setting the new value in the edit field
set(handles.frame.F,'String',FvalueNew)
% if the old value was 1, enabling the minus button
if strcmp(FvalueNew,'2')
    set(handles.frame.minusF_button,'Enable','on')
end
% if the new value is the same as the data size, disabling the minus button
if strcmp(FvalueNew,num2str(DataSize(2)))
    set(handles.frame.plusF_button,'Enable','off')
end
imshow(data{Svalue}{str2double(FvalueNew),Mvalue},[])
end

function minusF_callback(hObject,eventdata)
% Function minusF_callback is a callback function of minus bushbutton.
global handles
global DataSize
global data
Mvalue = str2double(get(handles.frame.M,'String'));
Fvalue = str2double(get(handles.frame.F,'String'));
Svalue = str2double(get(handles.frame.S,'String'));
FvalueNew = num2str(Fvalue-1);
% setting the new value in the edit field
set(handles.frame.F,'String',FvalueNew)
% if the new value is 1, disabling the minus button
if strcmp(FvalueNew,'1')
    set(handles.frame.minusF_button,'Enable','off')
end
% if the new value is the same as the data size, enabling the plus button
if strcmp(FvalueNew,num2str(DataSize(2)-1))
    set(handles.frame.plusF_button,'Enable','on')
end
imshow(data{Svalue}{str2double(FvalueNew),Mvalue},[])
end

function plusS_callback(hObject,eventdata)
% Function plusS_callback is a callback function of plus bushbutton.
global handles
global DataSize
global data
Mvalue = str2double(get(handles.frame.M,'String'));
Fvalue = str2double(get(handles.frame.F,'String'));
Svalue = str2double(get(handles.frame.S,'String'));
SvalueNew = num2str(Svalue+1);
% setting the new value in the edit field
set(handles.frame.S,'String',SvalueNew)
% if the old value was 1, enabling the minus button
if strcmp(SvalueNew,'2')
    set(handles.frame.minusS_button,'Enable','on')
end
% if the new value is the same as the data size, enabling the plus button
if strcmp(SvalueNew,num2str(DataSize(1)))
    set(handles.frame.plusS_button,'Enable','off')
end
imshow(data{str2double(SvalueNew)}{Fvalue,Mvalue},[])
end

function minusS_callback(hObject,eventdata)
% Function minusS_callback is a callback function of minus bushbutton.
global handles
global DataSize
global data
Mvalue = str2double(get(handles.frame.M,'String'));
Fvalue = str2double(get(handles.frame.F,'String'));
Svalue = str2double(get(handles.frame.S,'String'));
SvalueNew = num2str(Svalue-1);
% setting the new value in the edit field
set(handles.frame.S,'String',SvalueNew)
% if the new value is 1, disabling the minus button
if strcmp(SvalueNew,'1')
    set(handles.frame.minusS_button,'Enable','off')
end
% if the new value is the same as the data size, enabling the plus button
if strcmp(SvalueNew,num2str(DataSize(1)-1))
    set(handles.frame.plusS_button,'Enable','on')
end
imshow(data{str2double(SvalueNew)}{Fvalue,Mvalue},[])
end

function S_callback(hObject,eventdata)
% Function S_callback is a callback function of S edit field.
global handles
global DataSize
global data
Mvalue = str2double(get(handles.frame.M,'String'));
Fvalue = str2double(get(handles.frame.F,'String'));
Svalue = str2double(get(handles.frame.S,'String'));
Svec = 1:DataSize(1);
% checking for validity of entered value
if isempty(find(Svec==Svalue,1))
    errordlg(['Entered value is not valid. Pick an integer from 1 to ' num2str(DataSize(1))], ...
        'Error!')
else
    imshow(data{Svalue}{Fvalue,Mvalue},[])
    if Svalue==1
        set(handles.frame.minusS_button,'Enable','off')
    else
        set(handles.frame.minusS_button,'Enable','on')
    end
    if Svalue==DataSize(1)
        set(handles.frame.plusS_button,'Enable','off')
    else
        set(handles.frame.plusS_button,'Enable','on')
    end
end
end

function M_callback(hObject,eventdata)
% Function S_callback is a callback function of S edit field.
global handles
global DataSize
global data
Mvalue = str2double(get(handles.frame.M,'String'));
Fvalue = str2double(get(handles.frame.F,'String'));
Svalue = str2double(get(handles.frame.S,'String'));
Mvec = 1:DataSize(3);
% checking for validity of entered value
if isempty(find(Mvec==Mvalue,1))
    errordlg(['Entered value is not valid. Pick an integer from 1 to ' num2str(DataSize(3))], ...
        'Error!')
else
    imshow(data{Svalue}{Fvalue,Mvalue},[])
    if Mvalue==1
        set(handles.frame.minusM_button,'Enable','off')
    else
        set(handles.frame.minusM_button,'Enable','on')
    end
    if Mvalue==DataSize(3)
        set(handles.frame.plusM_button,'Enable','off')
    else
        set(handles.frame.plusM_button,'Enable','on')
    end
end
end

function F_callback(hObject,eventdata)
% Function S_callback is a callback function of S edit field.
global handles
global DataSize
global data
Mvalue = str2double(get(handles.frame.M,'String'));
Fvalue = str2double(get(handles.frame.F,'String'));
Svalue = str2double(get(handles.frame.S,'String'));
Fvec = 1:DataSize(2);
% checking for validity of entered value
if isempty(find(Fvec==Fvalue,1))
    errordlg(['Entered value is not valid. Pick an integer from 1 to ' num2str(DataSize(2))], ...
        'Error!')
else
    imshow(data{Svalue}{Fvalue,Mvalue},[])
    if Fvalue==1
        set(handles.frame.minusF_button,'Enable','off')
    else
        set(handles.frame.minusF_button,'Enable','on')
    end
    if Fvalue==DataSize(2)
        set(handles.frame.plusF_button,'Enable','off')
    else
        set(handles.frame.plusF_button,'Enable','on')
    end
end
end

function set_callback(hObject,eventdata)
% Function set_callback saves the choosen slice and closes the window.
global handles
global setFrame
Mvalue = str2double(get(handles.frame.M,'String'));
Fvalue = str2double(get(handles.frame.F,'String'));
setFrame = [Mvalue Fvalue];
clear global data
clear global DataSize
delete(handles.frame.MainW)
end