%WRITTEN BY CAGDAS

%ACCOUNT MANAGEMENT SCRREN FOR HIGH LEVEL USERS

%startup
function varargout = GUI_showAccountManagementScreen(varargin)
% GUI_SHOWACCOUNTMANAGEMENTSCREEN MATLAB code for gui_showaccountmanagementscreen.fig
%      GUI_SHOWACCOUNTMANAGEMENTSCREEN, by itself, creates a new GUI_SHOWACCOUNTMANAGEMENTSCREEN or raises the existing
%      singleton*.
%
%      H = GUI_SHOWACCOUNTMANAGEMENTSCREEN returns the handle to a new GUI_SHOWACCOUNTMANAGEMENTSCREEN or the handle to
%      the existing singleton*.
%
%      GUI_SHOWACCOUNTMANAGEMENTSCREEN('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in GUI_SHOWACCOUNTMANAGEMENTSCREEN.M with the given input arguments.
%
%      GUI_SHOWACCOUNTMANAGEMENTSCREEN('Property','Value',...) creates a new GUI_SHOWACCOUNTMANAGEMENTSCREEN or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before gui_showaccountmanagementscreen_openingfcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to gui_showaccountmanagementscreen_openingfcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help gui_showaccountmanagementscreen

% Last Modified by GUIDE v2.5 12-Dec-2013 18:16:32

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @GUI_showAccountManagementScreen_OpeningFcn, ...
                   'gui_OutputFcn',  @GUI_showAccountManagementScreen_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 gui_showaccountmanagementscreen is made visible.
function GUI_showAccountManagementScreen_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 gui_showaccountmanagementscreen (see VARARGIN)
handles.ctrl = varargin{1};
populateAccounts(handles);
% Choose default command line output for gui_showaccountmanagementscreen
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

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


% --- Outputs from this function are returned to the command line.
function varargout = GUI_showAccountManagementScreen_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;


% --- Executes on selection change in popAccounts.
function popAccounts_Callback(hObject, eventdata, handles)
% hObject    handle to popAccounts (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = cellstr(get(hObject,'String')) returns popAccounts contents as cell array
%        contents{get(hObject,'Value')} returns selected item from popAccounts


% --- Executes during object creation, after setting all properties.
function popAccounts_CreateFcn(hObject, eventdata, handles)
% hObject    handle to popAccounts (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in btDelete.
function btDelete_Callback(hObject, eventdata, handles)
% hObject    handle to btDelete (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
user = handles.ctrl.getUser();
i = get(handles.popAccounts,'Value');
names = get(handles.popAccounts,'String');
deleteUser.name = names{i}; 
result = handles.ctrl.accountManager.deleteUser(user, deleteUser);
switch(result)
    case 0
        msgbox('Successful');
        populateAccounts(handles);
    case 1
        msgbox('Password error');
        
    case 2
        msgbox('Permission error');
        
    case 3
        msgbox('Authenticating user coulnd''nt be found');
        
    case 4
        msgbox('User couldn''t be found');
end



function txtUserName_Callback(hObject, eventdata, handles)
% hObject    handle to txtUserName (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of txtUserName as text
%        str2double(get(hObject,'String')) returns contents of txtUserName as a double


% --- Executes during object creation, after setting all properties.
function txtUserName_CreateFcn(hObject, eventdata, handles)
% hObject    handle to txtUserName (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function txtPassword_Callback(hObject, eventdata, handles)
% hObject    handle to txtPassword (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of txtPassword as text
%        str2double(get(hObject,'String')) returns contents of txtPassword as a double


% --- Executes during object creation, after setting all properties.
function txtPassword_CreateFcn(hObject, eventdata, handles)
% hObject    handle to txtPassword (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in btnAdd to ADD user.
function btnAdd_Callback(hObject, eventdata, handles)
% hObject    handle to btnAdd (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
newUser.name  = get(handles.txtUserName,'String');
newUser.password = get(handles.txtPassword,'String');
newUser.type = get(handles.popType,'Value') - 1;
user = handles.ctrl.getUser();
result = handles.ctrl.accountManager.addUser(user,newUser);
if(result==0)
    msgbox('Successful');
else
    msgbox('Permission error');
end
populateAccounts(handles);

%populates accounts in combobox
function populateAccounts(handles)
user = handles.ctrl.getUser();
[ userNames, result ] = handles.ctrl.accountManager.getAccounts(user);
if(result ==1 )
    set(handles.popAccounts,'Value',1);
    set(handles.popAccounts,'String',userNames);
else
    close all
end


% --- Executes on selection change in popType.
function popType_Callback(hObject, eventdata, handles)
% hObject    handle to popType (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = cellstr(get(hObject,'String')) returns popType contents as cell array
%        contents{get(hObject,'Value')} returns selected item from popType


% --- Executes during object creation, after setting all properties.
function popType_CreateFcn(hObject, eventdata, handles)
% hObject    handle to popType (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in btnBack to navigate back in User screen.
function btnBack_Callback(hObject, eventdata, handles)
% hObject    handle to btnBack (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
close all
GUI_showMainScreen(handles.ctrl);


% --- Executes on button press in pushbutton4 to Act As another user.
function pushbutton4_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton4 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
i = get(handles.popAccounts,'Value');
names = get(handles.popAccounts,'String');
handles.ctrl.actAs(names{i});