<?php
/*
* 2007-2014 Addonline
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License (AFL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/afl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
*  @author Addonline <contact@addonline.fr>
*  @copyright  2007-2014 Addonline
*  @license    http://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
*/


/*
 * File called by ajax. It's like a controller, you have to send the
 * method name of the webservice and implement it.
 * Each Name method allow to instanciate an object containing
 * methods to manage correctly the data and name fields
 */

// Clean displayed content for Admin ajax query
if (ob_get_contents()) ob_clean();

// Front Ajax query, need the front cookie and GLS class
// When it's back query, the PS core made the work
if (!defined('_PS_ADMIN_DIR_'))
{
	require_once(realpath(dirname(__FILE__).'/../../config/config.inc.php'));
	require_once(realpath(dirname(__FILE__).'/../../init.php'));
	require_once(dirname(__FILE__).'/gls.php');
}

$gls = new Gls();

// Can't use Tools at this time... Need to know if _PS_ADMIN_DIR_ has to be defined
$method = Tools::getValue('method');

$params = array();
$result = array();

// Method name allow to instanciate his object to properly call the
// implemented interface method and do his job
switch($method)
{
	case 'getGLSRelayCarrierId':
		break;
	case 'getGLSRelayPointsForZipCode':
		$params['zipcode'] = Tools::getValue('zipcode');
		$params['street'] = Tools::getValue('street');
		$params['city'] = Tools::getValue('city');
		break;
	case 'getDefaultZipCode':
		break;
	case 'saveDeliveryDetails':
		$params['name'] = Tools::getValue('name');
		$params['address'] = Tools::getValue('address');
		$params['city'] = Tools::getValue('city');
		$params['zipcode'] = Tools::getValue('zipcode');
		$params['relayId'] = Tools::getValue('relayId');
		$params['phone'] = Tools::getValue('phone');
		$params['warnbyphone'] = Tools::getValue('warnbyphone');
		break;
	case 'getChoosenRelay':
		break;
	case 'resetDeliveryDetails':
		break;
	default:
		exit;
}

// Try to instanciate the method object name and call the necessaries method
try
{
	if (method_exists($gls,$method))
	{
		$result = call_user_func(array($gls,$method),$params);
	}
}
catch(Exception $e)
{
	echo GLSTools::jsonEncode(array('other' => array('error' => array($e->getMessage()))));
	exit(-1);
}

echo GLSTools::jsonEncode($result);
exit(0);
