//  @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 _deals = null;

	//	Added new code to handle lookups for contacts tasks
	var _contacts_tasks_lookup={};
	var _users_lookup={};
	var _task_types_lookup={};
		
  var _this=this;
	var _note_page_size=10;
	var _task_page_size=10;
	var _note_page=1;

  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_note_clicked',function(e){
	scope.notes_service.create_note_for_contact_and_deal(e.contact,e.deal,e.body,function(f){
		if(_notes==null) _notes=[];

        _notes=_notes.concat((new Array(f)));
		e.update(f);
		_this.raise_event('on_deal_associated',e.deal);
	});
  });
  
	scope.view.subscribe_to('on_load_more_notes',function(e){
		_note_page++;
		scope.notes_service.get_notes_for_contact(e.contact.id,_note_page_size,_note_page,function(data){
			if(data){
				data.note=data.note.reverse();
				$.each(data.note,function(i,n){
					e.notes.push(n);
				});
			}
			e.update(data,e.notes,_note_page_size);
        });
	});
	
	scope.view.subscribe_to('on_load_more_notes',function(e){
		// _note_page++;
		// scope.notes_service.get_notes_for_contact(e.contact.id,_note_page_size,_note_page,function(data){
		// 	if(data){
		// 		data.note=data.note.reverse();
		// 		$.each(data.note,function(i,n){
		// 			e.notes.push(n);
		// 		});
		// 	}
		// 	e.update(data,e.notes,_note_page_size);
		//         });
	});
	
    scope.view.subscribe_to('on_add_new_task',function(e){
        e.task.contact=[""+_contact.id];
		scope.tasks_service.add_task(e.task,function(f){
            e.paint(f);
			_this.raise_event('on_deal_associated',f.deal);
        });
	});

	scope.view.subscribe_to('on_task_complete_change',function(e){
		
		e.task.complete=(e.task.complete=="true")?"false":"true";
		scope.tasks_service.update_task(e.task,function(f){
			e.paint(f);
		});
	});



  function load_data(event){
	_contact=event;

	var req_count=0;
	_note_page=1;
	
	Async.multi_thread_load([
		
          //  Load contact notes
          function(callback){
              scope.notes_service.get_notes_for_contact(_contact.id,_note_page_size,_note_page,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();
              //               });
			scope.tasks_service.get_tasks_lookup_for_contact(_contact.id,function(data){
				_contacts_tasks_lookup[_contact.id]=data;
				callback();
			});
          },
          //  Load all users
          // function(callback){
          //     scope.user_service.get_all_users(function(data){
          //         _users = data;
          //         callback();
          //     });
          // 
          // },
			//	Load users lookup
			function(callback){
				scope.user_service.get_all_users_lookup(function(lookup,users){
					_users_lookup=lookup;
					_users=users
					callback();
				});
			},
          //load all deals
          function(callback){
              scope.deals_service.get_deals("",function(data){
                 _deals = data;
                 callback(); 
              });
          },
//	Load task types
		function(callback){
			scope.tasks_service.get_task_types_lookiup(function(task_types){
				_task_types_lookup=task_types;
				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:_contacts_tasks_lookup[_contact.id],
			users:_users_lookup,
			pages:{note:_note_page_size,task:_task_page_size},
			deals:_deals,
			task_types:_task_types_lookup			
		});
    };
};