% WRITTEN BY CAGDAS
classdef AccountManager < handle
    %ACCOUNTMANAGER handles account management functions
    
    properties
    end
    
    methods
        % Adds user to database, requires Authenticating user 
        %as a high level user
        function result = addUser(AM,AuthenticatingUser, newUser)
            %result     0 successful
            %result     1 permission error
            result = 1;
            %first verify that user exist and can add user
            AuthenticatingUser =  AM.verifyUser(AuthenticatingUser);
            if(AuthenticatingUser.id >0 && AuthenticatingUser.type == 0) % he is highlevel user, so can add users
                result = DatabaseManager.addUser(newUser);
            end
        end
        % Deletes deleteUser from database, requires Authenticating user 
        %as a high level user
        function result = deleteUser(AM,AuthenticatingUser, deleteUser)
            %result =   0 successful
            %           1 Password error
            %           2 Permission error
            %           3 AuthenticatingUser couldn't be found
            %           4 deleteUser couldn't be found


            %first verify that user exist and can add user
            AuthenticatingUser =  AM.verifyUser(AuthenticatingUser);
            if(AuthenticatingUser.id == -1) % Authenticating user couldn't be found
                result = 3;
            elseif(AuthenticatingUser.id == 0) %password error
                result = 1;
            elseif(AuthenticatingUser.type ~= 0)% he is not a highlevel user 
                result = 2;
            else % he is highlevel user, so can delete users
                deleteUser = DatabaseManager.getUserFromName(deleteUser.name)
                if(deleteUser.id == -1)
                    result = 4; %couldn't find delete user
                else     % delete it
                     DatabaseManager.deleteUser(deleteUser.id);
                     result = 0;
                end
            end
        end
        % Retrieves all users from database, requires Authenticating user 
        %as a high level user
        function [ userNames, result ] = getAccounts(AM, authenticatingUser )
        %GETACCOUNTS Summary of this function goes here
        %   result = -1 incorrect user name
        %   result = 0 incorrect password
        %   result = 1 successful
        authenticatingUser = AM.verifyUser(authenticatingUser);
        if (authenticatingUser.id >0)
            result = 1;
            userNames = DatabaseManager.getAllUsers();
        else
            result = authenticatingUser.id ;
        end

        end
        % Checks whether a user exist and password given matches
        function result = verifyUser(AM,user)
        % result.id = -1 incorrect user name
        % result.id =  0 incorrect password
        % other wise result = matching user
         result = DatabaseManager.getUserFromName(user.name);
        if(result.id == -1 )
            result.id = -1 %user cannot be found
        elseif( ~strcmp(result.password,user.password))
             result.id = 0; %incorrect password
             result.password = user.password; % we cannot return result password :)
        end

        end
        function AM = AccountManager()
            
        end
    end
    
end

