/*
 *
 *	Class: phone.call
 *	Handles everything about calls
 *	
 *	TODO: Move call-button-stuff to phone.bottom
 *
 */
phone.call = {};
phone.call.participants = {owner:'0',target:'0'};
phone.call.status = 0;


/*
 *	Function: setup
 *	Initiate everything for call. Setup API, buttons, vars, etc.
 *	
 *	See Also:
 *	  <phone.history.setup>
 */
phone.call.setup = function() {

	phone.call.$before_call 	= $('#before_call');
	if(phone.call.$before_call.length > 0) {
		phone.call.$before_call.find('.button.yes').button(function(){phone.call.start(true)});
		phone.call.$before_call.find('.button.no').button(function(){phone.call.start(false)});
	}
	
	phone.call.$button_start 	= $('#phone_bottom .start_call').hide().click(function() {
		if(phone.call.$before_call.length > 0) {
			phone.call.showBeforeCall();
		} else {
			phone.call.start(false);
		}
	});

	phone.call.$button_stop 	= $('#phone_bottom .stop_call').hide().click(phone.call.stop);
	
	if($('#sponsor_window').length > 0) {
		phone.call.$sponsor_window = $('#sponsor_window');
	}
	
	if($('#sponsor_background').length > 0) {
		phone.call.$sponsor_background = $('#sponsor_background');
	}

	phone.call.api = new API({resource:'users/'+user.current.id+'/calls'});
};



/*
 *	Function: setOwner
 *	Update the owner of the call
 *	
 *	Parameters:
 *		string phoneNumber - Number to update	
 *	
 *	See Also:
 *		<setTarget>
 */
phone.call.setOwner = function(phoneNumber) {
	console.log('phone.call.setOwner('+phoneNumber+')');
	phone.call.participants.owner = phoneNumber;
	phone.call.updateCallButton();
};



/*
 *	Function: setTarget
 *	Update the number to call
 *	
 *	Parameters:
 *		string phoneNumber - Number to update	
 *
 *	See Also:
 *		<setOwner>
 */
phone.call.setTarget = function(phoneNumber) {
	console.log('phone.call.setTarget('+phoneNumber+')');
	phone.call.participants.target = phoneNumber;
	phone.call.updateCallButton();	
};



/*
 *	Function: start
 *	Update the owner of the call
 *	
 *	See Also:
 *		<API.call>
 */
phone.call.start = function() {
	console.log('phone.call.start()');
	var sponsored = ( phone.call.$sponsor_window || phone.call.$sponsor_background );

	phone.call.enableStopButton();
	phone.call.hideBeforeCall();
	phone.middle.close();
	
	if(!phone.call.participants.owner) {
		$popup.error('CALL_OWNER_MISSING');
		return false;
	}
	
	if(!phone.call.participants.target) {
		$popup.error('CALL_TARGET_MISSING');
		return false;
	}
	
	console.log('participants: ', phone.call.participants);
		
	phone.call.api.call({method:'GET', url_vars:'start/'+phone.call.participants.owner+'/'+phone.call.participants.target+'/'+(sponsored ? '1' : '0'), loading:'CALL_STARTING', error:"CALL_FAILED", success:function(data) {
		phone.call.current = data.call;
		phone.call.getStatus();
		phone.call.showDuringCall();
	}, complete:function(req, status) {
		if(req.status != 200) phone.call.showEnd();
	}});
};



/*
 *	Function: stop
 *	Stop the current call. For now this is only frontend and won't stop the actual call.
 */
phone.call.stop = function() {
	console.log('phone.call.stop()');
	
	if(phone.call.current) {
		phone.call.api.call({method:'GET', url_vars:'stop/'+phone.call.current.id, success:function(data) {
			phone.call.showEnd();
		}});
	} else {
		phone.call.showEnd();
	}
};



/*
 *	Function: getStatus
 *	Get the status of current call. Update the clock if necessary, show end or check again.
 *	
 *	See Also:
 *		<phone.top.showClock>
 *		<phone.top.updateClock>
 *		<showEnd>
 *		<API.call>
 *
 *	Todo:
 *		- Update userdata
 */
phone.call.getStatus = function() {
	phone.call.api.call({method:'GET', url_vars:'status/'+phone.call.current.id, error:'CALL_STATUS_FAILED', success:function(data) {	
		user.current = data.user;
		phone.call.current = data.call;
		//Started
		if(data.call.status < 2) 	phone.top.showClock();
		//Connected
		if(data.call.status == 2) phone.top.updateClock(data.call.duration);
		if(data.call.status < 3) {
			phone.call.status = data.call.status;	
			phone.call.status_interval = setTimeout(phone.call.getStatus,4000);
		} else {
			//Call done
			phone.call.showEnd();
		}
		//Update credit
		if(data.user.credit) {
			phone.top.updateCredit(data.user.credit);
		}
	}, complete: function(req,status) {
		if(req.status != 200) phone.call.showEnd();
	}});
}



/*
 *	Function: showEnd
 *	Bring the phone back to its original state and cleanup all vars/timers.
 *	
 *	See Also:
 *		<phone.top.hideClock>
 *		<phone.middle.open>
 *		<phone.contacts.reset>
 *		<phone.history.reset>
 */
phone.call.showEnd = function() {
	//Reset call
	clearInterval(phone.call.status_interval);	
	phone.call.status = 0;
	delete phone.call.current;
	phone.top.hideClock();
	phone.call.hideDuringCall();
	phone.call.updateCallButton();
	phone.call.hideBeforeCall();
	phone.call.setTarget(false);
	
	//Update history
	phone.history.setup();
	
	//Back to phone
	phone.contacts.reset();
	phone.history.reset();
	phone.middle.open();
};



/* PRIVATE HELPER METHODS */
phone.call.updateCallButton = function() {
	if(phone.call.status == 0) {
		//No call
		if(phone.call.participants.owner.length >= 10 && phone.call.participants.target.length >= 10) {
			phone.call.enableCallButton();
		} else {
			phone.call.disableCallButton();
		}
	} else {
		//Duringcall
		phone.call.enableStopButton();
	}
};

phone.call.enableCallButton = function() {
	phone.call.$button_start.fadeIn(200);
	phone.call.$button_stop.fadeOut(200);
};

phone.call.disableCallButton = function() {
	phone.call.$button_start.fadeOut(200);
	phone.call.$button_stop.fadeOut(200);
};

phone.call.enableStopButton = function() {
	phone.call.$button_start.fadeOut(200);
	phone.call.$button_stop.fadeIn(200);
};

phone.call.disableStopButton = function() {
	phone.call.$button_start.fadeOut(200);
	phone.call.$button_stop.fadeOut(200);
};

phone.call.showBeforeCall = function() {
	phone.call.enableStopButton();
	phone.call.$before_call.fadeIn(100).find('.inner').animate({'margin-top':85},300);
};

phone.call.hideBeforeCall = function() {
	phone.call.$before_call.fadeOut(200).find('.inner').animate({'margin-top':270},200);
};

phone.call.showDuringCall = function() {
	//Animate phone
	if(phone.call.$sponsor_background || phone.call.$sponsor_window) {
		phone.$container.animate({'left':'85%', 'top':'105%'}, 400);
	}
		
	//Show background
	if(phone.call.$sponsor_background) {
		$.backstretch(phone.call.$sponsor_background.find('img').attr('src'));
	}
	
	//Show window
	if(phone.call.$sponsor_window) {
		if(!IE && !IOS) {
			phone.call.$sponsor_window.fadeIn();
		} else {
			phone.call.$sponsor_window.show();
		}
	}
};

phone.call.hideDuringCall = function() {
	//Animate phone
	if(phone.call.$sponsor_background || phone.call.$sponsor_window) {
		phone.$container.animate({'top':'50%', 'left':'50%'}, 200);
	}
	
	//Revert to default
	if(phone.call.$sponsor_background) {
		$.backstretch('/images/bg.jpg');
	}

	//Hide window
	if(phone.call.$sponsor_window) {
		if(!IE && !IOS) {
			phone.call.$sponsor_window.fadeOut();
		} else {
			phone.call.$sponsor_window.hide();
		}
	}
};

phone.call.prefix = function(number) {
	if(number.substring(0,2) == '00') {
		number = number.substring(2,number.length);
	}
	if(number.substring(0,1) == '0') {
		number = '31' + number.substring(1,number.length);
	}
	return number;
}
