var services = angular.module('mindinkServices', ['ngResource', 'restangular']).
	value('version', '0.1');

services.factory('userFactory', ['$rootScope', 'Restangular', function($rootScope, Restangular){
    return {
        //  user/create_email_account
        emailSignUp: function(firstName, lastName, email, password) {
            var emailAccount = Restangular.one('user');

            emailAccount.post("create_email_account", 'firstName=' + firstName + '&lastName=' + lastName + '&email=' + email + '&password=' + password)
                .then(function(response){
                    if (response && response.status == "SUCCESS") {
                        $rootScope.$broadcast("email_signup_success", {'status': response.status, 'userId': response.userId});
                    }
                    else {
                        $rootScope.$broadcast('email_signup_fail', {'status': response.status});
                    }
                }, function(){
                    //
                    $rootScope.$broadcast('email_signup_fail');
                });
        },

        //  user/activate_user
        activateUser: function(generatedid) {
            var emailAccount = Restangular.one('user');

            emailAccount.post("activate_user", 'generatedid=' + generatedid)
                .then(function(response){
                    if (response && response.status == "SUCCESS") {
                        $rootScope.$broadcast("activate_user_success", {'status': response.status});
                    }
                    else {
                        $rootScope.$broadcast('activate_user_fail', {'status': response.status});
                    }
                }, function(){
                    //
                    $rootScope.$broadcast('activate_user_fail');
                });
        },

        //  user/signin_using_email
        emailSignin: function(email, password) {
            var emailAccount = Restangular.one('user');

            emailAccount.post("signin_using_email", 'email=' + email + '&password=' + password)
                .then(function(response){
                    if (response && response.status == "SUCCESS") {
                        $rootScope.$broadcast("email_signin_success", {'status': response.status, 'userId': response.userId});
                    }
                    else {
                        $rootScope.$broadcast('email_signin_fail', {'status': response.status});
                    }
                }, function(){
                    //
                    $rootScope.$broadcast('email_signin_fail');
                });
        },

        //  user/signout_using_email
        emailSignout: function() {
            return Restangular.one('user', 'signout').get();
            
        },

        //  user/request_new_password
        requestNewPassword: function(email) {
            var retrievePassword = Restangular.one('user');

            retrievePassword.getList('request_new_password', {'email': email})
                .then(function(response){
                    if (response && response.status == "SUCCESS") {
                        $rootScope.$broadcast("request_new_password_success", {'status': response.status});
                    }
                    else {
                        $rootScope.$broadcast('request_new_password_fail', {'status': response.status});
                    }
                }, function(){
                    //
                    $rootScope.$broadcast('request_new_password_fail');
                });
        },

        //  user/set_new_password
        resetPassword: function(generatedid, password) {
            var emailAccount = Restangular.one('user');

            emailAccount.post("set_new_password", 'generatedid=' + generatedid + '&password=' + password)
                .then(function(response){
                    if (response && response.status == "SUCCESS") {
                        $rootScope.$broadcast("reset_password_success", {'status': response.status});
                    }
                    else if (response && response.status == "RESET_PASSWORD_FAIL") {
                        $rootScope.$broadcast('reset_password_fail', {'status': response.status});
                    }
                    else if (response && response.status == "SIGNIN_FAIL") {
                        $rootScope.$broadcast('email_signin_fail', {'status': response.status});
                    }
                }, function(){
                    //
                    $rootScope.$broadcast('reset_password_fail');
                });
        },


        //  user/get_registered_subjects
        getRegisteredSubjects: function(uniId) {
            var registeredSubjects = Restangular.one('user');
            return registeredSubjects.getList('get_registered_subjects', {'uniId': uniId});
        },

        // user/add_subject
        addSubjectBySubjectId: function(subjectId) {
            var subject = Restangular.one('user');

            return subject.getList('add_subject', {'subjectId': subjectId});
        },

        // user/delete_subject
        deleteSubjectBySubjectId: function(subjectId) {
            var subject = Restangular.one('user');

            return subject.getList('delete_subject', {'subjectId': subjectId});
        },

        //  user/get_registered_books
        getRegisteredBooks: function() {
            var registeredBooks = Restangular.one('user');

            return registeredBooks.getList('get_registered_books');
        },


        //  get_all_subjects_by_uniid
        getAllSubjectsByUniId: function(uniId) {
            var allSubjects = Restangular.one('');

            return allSubjects.getList('get_all_subjects_by_uniid', {'uniId': uniId});
        },

        // search_book
        searchBooks: function(searchText) {
            var books = Restangular.one('');

            return books.getList('search_books', {'searchText': searchText});
        },

        // user/add_book
        addBookByBookId: function(bookId) {
            var book = Restangular.one('user');

            return book.getList('add_book', {'bookId': bookId});
        },

        // user/delete_book
        deleteBookByBookId: function(bookId) {
            var book = Restangular.one('user');

            return book.getList('delete_book', {'bookId': bookId});
        },

        ///get_all_universities 
        getUniversities: function() {
        	var unis = Restangular.one('');
        	return unis.getList('get_all_universities');
        },
        
        
        
    //  user/update_full_name
        updateFullName: function(firstName, lastName) {
            var updateFullName = Restangular.one('user/');

            return updateFullName.getList('update_full_name', {'firstName': firstName, 'lastName': lastName});
        },
        
        //user/get_user_info
        getUserInfo: function (){
            return Restangular.one('mindinkuser').get();
        },

      ///get_registered_universities
        getRegisteredUniversities: function() {
        	var unis = Restangular.one('');
        	
        	return unis.getList('get_registered_universities');
        },
        //user/delete_university
        deleteUniversity: function(uniId) {
        	var deleteUniversity = Restangular.one('user');

        	return deleteUniversity.getList('delete_university', {'uniId': uniId});
        },
        
        //user/add_university
        addUniversity: function(uniId) {
        	var addUniversity = Restangular.one('user');

        	return addUniversity.getList('add_universities', {'uniId': uniId});
        },

        // bookpurchase/withouttoken
        purchaseBook: function(bookId, firstName, lastName, address, suburb, state, postcode){
            var bookPurchase = Restangular.one('bookpurchase');

            return bookPurchase.getList('withouttoken', {'book_id': bookId, 'first_name': firstName, 'last_name': lastName,
                                            'address': address, 'suburb': suburb, 'state': state, 'postcode': postcode})
        },

        // bookpurchase/withouttokenresult/{access_code}
        purchaseSendAccessCode: function(accessCode) {
            var bookPurchase = Restangular.one('bookpurchase');

            return bookPurchase.getList('withouttoken/' + accessCode);
        },

        // bookpurchase/token
        purchaseBookWithToken: function(bookId) {
            var bookPurchase = Restangular.one('bookpurchase');

            return bookPurchase.getList('token', {'book_id': bookId});
        },

        // api/user/mark_onboarded
        markAsOnboarded: function() {
            return Restangular.one('user').post("mark_onboarded");
        }
}}]);

services.service('mindinkUser', ['Restangular', function(Restangular){
    return{
        getCurrentUser: function(){
            return Restangular.one('mindinkuser').get();
        }
    };
}]);
