//  @import com.hedgehoglab.application.service.BaseService;
//  @import com.hedgehoglab.solomon.service.exceptions.NoContactsFoundException;

function UsersService(scope){
    BaseService.call(this,scope);
    var _this = this;

    this.get_current_user=function(callback){
        var USER_ID = null;
        var user_id=$("meta[name='current_user_id']").attr('content');

        var user={
            id:user_id
        };

        callback(user);
    };

    this.get_all_users=function(callback){
        $.getJSON("/api/users",{},function(data){
            callback(data);
        });  
    };

	this.search_for_user=function(term,callback){
		var query={};

        if(term!=null){query.q=term}
        _this.request({
            uri:"/api/users?q="+query.q
            ,data:null
            ,type:"GET"
            ,callback:function(e){
                callback(e);
            }
        });
	}
    
    this.get_empty_user=function(callback){
      var user={
			"id":null,
			"username":"",
			"openid":"",
			"firstName":"",
			"lastName":"",
			"email":"",
			"phoneNumber":"",
			"website":"",
			"enabled":"true"
		};
	//	,"roles":{"name":"ROLE_USER"}
	
		callback(user);
		return user;
    };
    
	this.is_admin=function(user){
		return user.roles.role[0].id=="1";
	};
    this.save_user=function(user,callback){
//        normalize(contact);                             //  Resets arrays to null
		// callback(user);
		// 		return false;

        var meth="PUT";
        var user_id=null;
        var params = "?";

		if(user.id==null){
			meth="POST";
			user_id="";
			if (!user.role) user.role="ROLE_USER";
		}else{
			user_id=user.id;
		};
        

        if (user.role) params+="role="+user.role+"&";
		
        if (user.password) params+="password="+user.password;

        // params = params.substring(0,params.length-1);

        //  Remove objects we dont want to send
        // _c=$.extend({},contact);
        // _c.note=null;delete _c.note;
        // _c.tag=null;delete _c.tag;
        // _c.tasks=null;delete _c.task;
        
		//	Remove these values as they arent going to get saved
        delete user.password;
        delete user.roles;
		delete user.role;
		
        //  Make the request
        _this.request({
             uri:"/api/users/"+user_id+params
             ,data:user
             ,type:meth
             ,callback:function(data){
                 if(callback){
	                 callback(data);
                 }
             }
         });
    };
    
        this.delete_user=function(user_id,callback){
            _this.request({
                uri:"/api/users/"+user_id+""
                ,data:null
                ,type:"DELETE"
                ,callback:function(data){
                    if(callback){
                            callback(data);
                    }
                }
            });
        };

//    this.get_contacts=function(callback){
//
//        //$.ajaxSetup({async:false});
//        $.getJSON("/api/contacts",{},function(data){
//            callback(data);
//        });
//
//    }
//
//    this.get_contact_counts=function(search_term,callback){
//        $.getJSON("/api/backend/search-results-tag-panel",{"q":search_term},function(data){
//            callback(data);
//        });
//
//    }
};