//  @import com.hedgehoglab.application.controller.BaseController;

function UserListController(scope){

	BaseController.call(this,scope);
	var _this=this;
	var _term="";
	var _current_users=null;
	var _current_user=null;
	
	//	bind to search
    scope.user_findView.subscribe_to("on_search_term_changed",on_search_term_changed);		
	scope.user_findView.subscribe_to("on_search_term_empty",on_search_term_empty);
	scope.user_listView.subscribe_to("on_edit_user",on_edit_user);
	scope.user_editView.subscribe_to("on_done_edit_user",on_done_edit_user);
	scope.user_editView.subscribe_to("on_cancel_edit_user",on_cancel_edit_user);
	scope.user_editView.subscribe_to("on_username_changed",on_username_changed);	
	scope.user_actionsView.subscribe_to("on_action_clicked",on_action_clicked);
	
	function on_username_changed(event){

		//	Check the username service
		scope.user_service.search_for_user("",function(data){
			var _ok=true;
			if(data){
				$.each(data.user,function(i,n){
					if(event.val==n.username) _ok=false;
				});
			}
		
			event.update(_ok);
		});
	};
	
	function on_action_clicked(event){
		if(event.action_id=="enable-user-action"){
			_current_user.enabled=(_current_user.enabled=='true')?'false':'true';
			scope.user_service.save_user(_current_user,function(saved_user){
				_current_user=$.extend(_current_users,saved_user);
				scope.user_actionsView.set_toggle('enable-user-action',(_current_user.enabled!='true')?true:false);
			});
		}
		
		if(event.action_id=="email-user-action"){
			window.location ="mailto:"+_current_user.email;
		}
		
		if(event.action_id=="create-new-action"){
			scope.user_service.get_empty_user(function(e){

				on_edit_new_user(e);
			});
			//	Create a new user
			//	Get an empty user
		}
	};

	function on_edit_new_user(user){
		scope.user_listView.hide(function(e){
			scope.user_actionsView.set_state('new-user-state');
			scope.user_editView.render(user);
			scope.user_editView.show();
		});
	}
	
	
	function on_done_edit_user(event){

		scope.user_service.save_user(event,function(saved_user){
			if(saved_user!=null){
				//	Throw a message
				_current_user=$.extend(_current_users,saved_user);
				scope.user_actionsView.set_state('user-list-state');
				scope.user_editView.hide(function(e){
					update_contacts(_term);
					scope.user_listView.show();
				});
			}else{
				// message_service.
				var message = "A user with this name already exists.";
	            scope.message_service.show_message({
	                message: message,
	                type: "error"
	            });
				// scope.user_editView.show_username_error(function(e){
			}

		});
		
	};
	
	function on_cancel_edit_user(event){
		scope.user_editView.hide(function(e){
			scope.user_actionsView.set_state('user-list-state');
			scope.user_listView.show();
		});
	}
	
	function on_edit_user(event){

		scope.user_listView.hide(function(e){
			_current_user=_current_users.user[event.index];
						
			scope.user_actionsView.set_toggle('enable-user-action',(_current_user.enabled!='true')?true:false);
			
			scope.user_service.get_current_user(function(user){
				// //	If the current user is the selected we cant disable!
							if(!scope.user_service.is_admin(_current_user)){
								scope.user_actionsView.enable_action('enable-user-action');
							}else{
								scope.user_actionsView.disable_action('enable-user-action');
							}
				// 			
				// 			
							scope.user_actionsView.set_state('single-user-state');
				// 		
							scope.user_editView.render(_current_user,user);
							scope.user_editView.show();
			});		
		});
		
	};
	
    function on_search_term_empty(event){
        on_search_term_changed({"term":""});   
    };

	function on_search_term_changed(event){
		_term=event.term;
		update_contacts(_term);
	}

	function update_contacts(term){
		if(!term) term="";

		scope.user_service.search_for_user(term,function(data){
			_current_users=data;
			//	Rener the users
			// conso
			scope.user_listView.render(_current_users,term);
		});
	}
	
	update_contacts();
	
	//	Check for licence type and modify ui accordingly
	scope.licence_service.can_add_users(function(can_add_users,check){
		if(!can_add_users){
			//	Disable the add new user action
			scope.user_actionsView.disable_action('create-new-action',check.error);
		}else{
			
		}
	
	});
	
};