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

function UsersService(scope){
    BaseService.call(this,scope);
    var _this = this;
	var _lookup={};
	
    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.get_all_users_lookup=function(callback){
		_this.get_all_users(function(e){
			var _users={};
			var _lookup={};
			
			if(e && e.user){
				_users=e;
				$.each(e.user,function(i,n){
					_lookup[n.id]=n;
				});
			}
			callback(_lookup,_users);
		});
	};
	
	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.get_server_url=function(callback){
		$.ajax( {
            url:"/api/backend/application-url-without-protocol/",
            type: 'get',
            processData: false,
            timeout: 10000,
			cache: false,
			async:true
            ,success:function(res){
				callback(res);
            },
            error:function(e) {
                return;
            }
        });
	};
	
	this.get_user_setting=function(user_id,key,callback){
		_this.request({
             uri:"/api/users/"+user_id+"/settings/"+key
             ,data:[]
             ,type:'get'
             ,callback:function(data){
                 if(callback){
	                 callback(data);
                 }
             }
         });
	};
	
	this.reset_user_setting=function(user_id,key,callback){
		$.ajax( {
            url:"/api/users/"+user_id+"/settings/"+key,
            type: 'post',
            processData: false,
            timeout: 10000,
			cache: false,
			async:true
            ,success:function(res){
				callback($.parseJSON(res));
            },
            error:function(e) {
                return;
            }
        });
	};
	
    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);
//        });
//
//    }
};