//  @import com.hedgehoglab.application.controller.BaseController;
//  @import com.hedgehoglab.solomon.model.ContactModel;

/**
 *
 * @param scope
 */
function ContactNTController(scope){
  BaseController.call(this,scope);
    
  // Proof of date parse format feature.
  var _contact = null;
  var _notes = null;
  var _tasks = null;
  var _users = null;
  var _this=this;
    

  scope.model.subscribe_to('model_changed',function(e){
    load_data(e.model);
  });



    scope.view.subscribe_to('on_delete_note',function(e){
        scope.notes_service.delete_note(e.note_id,function(response){
          e.paint();
        });
    });

    scope.view.subscribe_to('on_edit_note',function(e){
        scope.notes_service.save_note(e.note,function(response){
          e.paint();
        });
    });

  scope.view.subscribe_to('on_add_new_note',function(e){
    scope.notes_service.create_note_for_contact(e.contact.id,e.body,function(response){
      //load_data(_contact);
        _notes=(new Array(response)).concat(_notes);
        //  Add note to ntes
        e.paint(response,_notes);
    });
  });
  
    scope.view.subscribe_to('on_add_new_task',function(e){
        scope.tasks_service.add_task(e.task,function(response){
			
			_tasks.task.push(response);
            e.paint(response);
        });
    });

  scope.view.subscribe_to('on_task_complete_change',function(e){
	var _task_index=null;
	$.each(_tasks.task,function(i,n){
		if(n.id==e.task.id){
			_task_index=i;
			n.complete=(n.complete=="true")?"false":"true";
		}
	});
	
	scope.tasks_service.update_task(_tasks.task[_task_index],function(f){
		_tasks.task[_task_index]=f;
		e.paint();
	});

  });

  function load_data(event){
      _contact=event;

      var req_count=0;
	// alert("LOADING DATA FOR NT");
	
      Async.multi_thread_load([
          //  Load contact notes
          function(callback){
              scope.notes_service.get_notes_for_contact(_contact.id,function(data){
                  
                  if(data!=null){
                      data=data.note;
                      _notes=data;
                          _notes = _notes.sort(function(a,b){
                            if (a.createdAt == b.createdAt) return 0;
                            else return (a.createdAt > b.createdAt) ? -1 : 1;
                      });
                  }else{
                      _notes=null;
                  }

                  callback();
              });
          },
          //  Load tasks
          function(callback){
              scope.tasks_service.get_tasks_for_contact(_contact.id,function(data){
                _tasks=data;
				if(_tasks==null){_tasks={task:[]}};
                callback();
              });
          },
          //  Load all users
          function(callback){
              scope.user_service.get_all_users(function(data){
                  _users = data;
                  callback();
              });

          }
      ]

      //  Handle all loaded
      ,function(){
		// alert("DATA LOADED FOR NT");
         _this.raise_event('on_data_loaded',{});
      });
  };


    this.render_view=function(event){
        scope.view.render({contact:_contact,notes:_notes,tasks:_tasks,users:_users});
    };
};