//  @import com.hedgehoglab.application.service.BaseService;

function DealsService(scope){
	BaseService.call(this,scope);
	var _this=this;
	var _deals_lookup={};
	var _deals_indeces={};
	
	var _deals_array={deals:[]};
	var _lookups={
		"deals":{},
		"contacts":{},
		"companies":{},
		"notes":{},
		"tasks":{}
	};

	this.get_deals=function(query,callback){
	   _this.request({
              uri:"/api/deals/?q="+query,
              type:"GET",
              callback:callback
          });
    };
    
    this.get_deal=function(id,callback){
	   _this.request({
              uri:"/api/deals/"+id,
              type:"GET",
              callback:callback
          });
    };

	this.get_deals_lookup=function(query,callback){
		if(!query) query="";
		_this.get_deals(query,function(e){
			_deals_lookup={};
			_deals_array={deals:[]};
			
			if(e){
				$.each(e.deal,function(i,n){
					_deals_indeces[n.id]=i;
					_deals_lookup[n.id]=n;
				});
				_deals_array.deals=e.deal;
			}
			
			callback(_deals_lookup,_deals_array);
		});
    };

	this.get_companies_for_deal=function(deal_id,callback){
		_this.request({
			uri:"/api/deals/"+deal_id+"/companies",
			type:"GET",
			callback:callback
		});
	};

	this.get_companies_lookup_for_deal=function(deal_id,callback){
		_this.get_companies_for_deal(deal_id,function(e){
			var company_lookup={};
			if(e){
				$.each(e.company,function(i,n){
					company_lookup[n.id]=n;
				});
			}
			callback(company_lookup);
		});
    };	

	this.get_contacts_for_deal=function(deal_id,callback){
		_this.request({
		    uri:"/api/deals/"+deal_id+"/contacts",
		    type:"GET",
		    callback:callback
		});
    };

	this.get_contacts_lookup_for_deal=function(deal_id,callback){
		_this.get_contacts_for_deal(deal_id,function(e){
			var contact_lookup={};
			// contact_lookup[n.id]={};
			if(e){
				$.each(e.contact,function(i,n){
					contact_lookup[n.id]=n;
				});
			}else{
				// contact_lookup={}
			}
			callback(contact_lookup);
		});
    };

	this.get_users_for_deal=function(deal_id,callback){
		_this.request({
		    uri:"/api/deals/"+deal_id+"/users",
		    type:"GET",
		    callback:callback
		});
    };

	this.get_users_lookup_for_deal=function(deal_id,callback){
		_this.get_users_for_deal(deal_id,function(e){
			var user_lookup={};
			if(e){
				$.each(e.user,function(i,n){
					user_lookup[n.id]=n;
				});
			}
			callback(user_lookup);
		});
    };

	this.get_notes_for_deal=function(deal_id,callback){
		_this.request({
		    uri:"/api/deals/"+deal_id+"/notes",
		    type:"GET",
		    callback:callback
		});
    };

	this.get_notes_lookup_for_deal=function(deal_id,callback){
		_this.get_notes_for_deal(deal_id,function(e){
			_lookups['notes']={};
			if(e){
				$.each(e.note,function(i,n){
					_lookups['notes'][n.id]=n;
					// _lookups['deals'][deal_id].notes_lookup.push(n.id);
				});
			}else{
				_lookups['notes']=[];
			}
			callback(_lookups['notes']);
		});
    };

	this.get_tasks_for_deal=function(deal_id,callback){
		_this.request({
		    uri:"/api/deals/"+deal_id+"/tasks",
		    type:"GET",
		    callback:callback
		});
    };

	this.get_tasks_lookup_for_deal=function(deal_id,callback){
		_this.get_tasks_for_deal(deal_id,function(e){
			_lookups['tasks']={};
			if(e){
				$.each(e.task,function(i,n){
					_lookups['tasks'][n.id]=n;
					// _lookups['deals'][deal_id].tasks_lookup.push(n.id);
				});
			}
			callback(_lookups['tasks']);
		});
    };

	this.get_new_deal=function(callback){
		var _deal={
			amount:0,
			name:"",
			assignedTo:null,
			createdAt:null,
			createdBy:null,
			description:"",
			id:null,
			probability:0,
			status:"IN_PROGRESS",
			expectedClose:null
		};
		
		callback(_deal);
	}
	
    this.save_deal=function(deal,callback){
        // normalize(contact);                             //  Resets arrays to null
        var meth="PUT";
        var deal_id=null;

        if(deal.id==null){
            meth="POST";
            deal_id="";
        }else{deal_id=deal.id};

        //  Remove objects we dont want to send
        _c=$.extend({},deal);
        // _c.note=null;delete _c.note;
        // _c.tag=null;delete _c.tag;
        // _c.tasks=null;delete _c.task;


		// if(contact.companies!=null){
		// 	if(contact.companies.company!=null){
		// 		$.each(contact.companies.company,function(i,n){
		// 			n.id=null;
		// 		});
		// 	}
		// }
        //  Make the request
        _this.request({
            uri:"/api/deals/"+deal_id+""
            ,data:_c
            ,type:meth
            ,callback:function(data){
				if(deal_id==""){
					_deals_array.deals.push(data);

					// _deals_lookup={};
					_deals_indeces={};
					$.each(_deals_array.deals,function(i,n){
						_deals_indeces[n.id]=i;
						_deals_lookup[n.id]=n;
					});
					
					// _deals_lookup[deal_id]=null;
					// 					delete _deals_lookup[deal_id];
					
				}
                if(callback){
					//	Store the deal back into the lookup
					// _deals_lookup[data.id]=data;
					// if(deal_id){
					// 						//	This needs to be rebuilt!
					// 						_conl=_lookups['deals'][deal_id].contacts_lookup;
					// 						_coml=_lookups['deals'][deal_id].companies_lookup;
					// 						_notl=_lookups['deals'][deal_id].notes_lookup;
					// 						_tasl=_lookups['deals'][deal_id].tasks_lookup;
					// 						
					// 						_lookups['deals'][deal_id]=data;
					// 
					// 						_lookups['deals'][deal_id].contacts_lookup=_conl;
					// 						_lookups['deals'][deal_id].companies_lookup=_coml;
					// 						_lookups['deals'][deal_id].notes_lookup=_notl;
					// 						_lookups['deals'][deal_id].tasks_lookup=_tasl;
					// 						
					// 					}
					callback(data);
                }
            }
        });
    };

	this.delete_deal=function(deal,callback){
		deal_id=deal.id
		//	Make the reques
		_this.request({
			
            uri:"/api/deals/"+deal_id+""
            ,data:{}
            ,type:"delete"
            ,callback:function(e){
			
			
			
			// _deals_array.deals.splice(_deals_indeces[deal_id])
			// 			_deals_array.deals[_deals_indeces[deal_id]]=null;
			// 			delete _deals_array.deals[_deals_indeces[deal_id]];
			// 
			// 			_deals_indeces[deal_id]=null;
			// 			delete _deals_indeces[deal_id];
			// 			
			// 			
			// 			_deals_lookup={};
			// 			
			// 			$.each(_deals_array.deals,function(i,n){
			// 				_deals_indeces[n.id]=i;
			// 				_deals_lookup[n.id]=n;
			// 				// _lookups['deals'][n.id]=n;
			// 				// 					_lookups['deals'][n.id].contacts_lookup=[];
			// 				// 					_lookups['deals'][n.id].companies_lookup=[];
			// 				// 					_lookups['deals'][n.id].notes_lookup=[];
			// 				// 					_lookups['deals'][n.id].tasks_lookup=[];
			// 			});
			// 			_deals_array.deals=e.deal;
			//	Use splice to remove the item from the DB
			//	and create teh indeces!
			
			_deals_array.deals.splice(_deals_indeces[deal_id],1);

			_deals_lookup={};
			_deals_indeces={};
			$.each(_deals_array.deals,function(i,n){
				_deals_indeces[n.id]=i;
				_deals_lookup[n.id]=n;
			});
			
			_deals_lookup[deal_id]=null;
			delete _deals_lookup[deal_id];

			callback();
					//	Remove all lookup references
					
				}
			});
	};
	
	this.reassign_deal=function(deals,user_id,callback){
		//	Loop through and get id's
		var deal_ids=[];
		var d=deals;
		if(!$.isArray(deals)) d=[deals];
		
		$.each(d,function(i,n){
			_deals_lookup[n.id].assignedTo=user_id;
		});
		
		_this.request({
			//	http://localhost:8080/api/backend/deals/1,2/reassignto/2
            uri:"/api/backend/deals/"+(deal_ids.toString())+"/reassignto/"+user_id
            ,data:null
            ,type:"PUT"
            ,callback:function(e){
				$.each(d,function(i,n){
					_deals_lookup[n.id].assignedTo=user_id;
				});
				callback();
			}
		});
	};
	
	this.delete_deal=function(deals,callback){
		//	Loop through and get id's
		var deal_ids=[];
		var d=deals;
		if(!$.isArray(deals)) d=[deals];
		
		$.each(d,function(i,n){
			deal_ids.push(n.id);
		});
		//	DELETE /api/backend/contacts/massdelete/10,6,2
        _this.request({
            uri:"/api/backend/deals/massdelete/"+(deal_ids.toString())
            ,data:null
            ,type:"DELETE"
            ,callback:function(e){
                //  Remove the item from the list

				// NEED TO REMOVE THE DEALS FROM THE LIST!!!!!!
				

				
                callback(e);
				
				
				
				// _deals_array.deals.splice(_deals_indeces[deal_id],1);
				// 
				// 				_deals_lookup={};
				// 				_deals_indeces={};
				// 				$.each(_deals_array.deals,function(i,n){
				// 					_deals_indeces[n.id]=i;
				// 					_deals_lookup[n.id]=n;
				// 				});
				// 
				// 				_deals_lookup[deal_id]=null;
				// 				delete _deals_lookup[deal_id];
				// 
				// 				callback();
				// 						//	Remove all lookup references
				// 
				// 					}
				// 				});
            }
        });
    };
}