<?php
require_once(dirname(__FILE__).'/../../../config/config.inc.php');

// SOAP DEFINES URL
if(!defined('_ZUGARA_GET_ACCOUNT_STATUS_'))
	define('_ZUGARA_GET_ACCOUNT_STATUS_', 'http://prestashop.webcamsocialshopper.com/index.php/getAccountStatus');

if (!defined('_ZUGARA_CREATE_GARMENT_URL_'))
	define('_ZUGARA_CREATE_GARMENT_URL_', 'http://prestashop.webcamsocialshopper.com/index.php/createGarment');

if (!defined('_ZUGARA_CREATE_GARMENT_COLOR_URL_'))
	define('_ZUGARA_CREATE_GARMENT_COLOR_URL_', 'http://prestashop.webcamsocialshopper.com/index.php/createGarmentColor');

if (!defined('_ZUGARA_UPDATE_GARMENT_URL_'))
	define('_ZUGARA_UPDATE_GARMENT_URL_', 'http://prestashop.webcamsocialshopper.com/index.php/updateGarment');

if (!defined('_ZUGARA_DELETE_GARMENT_URL_'))
	define('_ZUGARA_DELETE_GARMENT_URL_', 'http://prestashop.webcamsocialshopper.com/index.php/deleteGarment');
	
if (!defined('_ZUGARA_DELETE_GARMENT_COLOR_URL_'))
	define('_ZUGARA_DELETE_GARMENT_COLOR_URL_', 'http://prestashop.webcamsocialshopper.com/index.php/deleteGarmentColor');

function 				deactivateWSSAccount($domain)
{
	$code = 2;
	if ($q = Db::getInstance()->getValue('SELECT id_module  FROM `'._DB_PREFIX_.'module` WHERE name LIKE \'%webcamsocialshopper%\''))
		{
		if ($domain == $_SERVER['HTTP_HOST']){
			if(Db::getInstance()->Execute('UPDATE `'._DB_PREFIX_.'module` SET `active`= 0  WHERE `id_module` = '.$q.'')){
					$code = 0;
				return $code;
			}
		} else
			$code = 1;
		}
    return $code;
}

class			WssSc
{
	public		function __construct($domain)
	{
	}

	
	// SOAP : Gets the account status
	static public 	function	getAccountStatus()
	  { 
		  $client = new soapclient(_ZUGARA_GET_ACCOUNT_STATUS_);
		  $res = array();
		  $res['0'] = $client->getStatus($_SERVER['HTTP_HOST']);
		  $res['1'] = '';
		  return ($res);	
	  }
	
	
	  
	// SOAP : Create a new Garment to Zugara DB
	static public function 		createGarment($sku, $name, $skuurl, $ScalingValue = 1.0, $VoValue = 0)
	{
		/* SOAP INSTANCE */	
		$client = new soapclient(_ZUGARA_CREATE_GARMENT_URL_);
		$res = array();
		$data = array(
				'Domain' => $_SERVER['HTTP_HOST'],
				'GarmentName' => $name, //(optional)
				'SKU' => $sku,
				'Colors' => array(
					0 => array(
						'SKUUrl' => $skuurl,
						'ScalingValue' => $ScalingValue, //(optional, default: 1.0)
						'VerticalOffsetValue' => $VoValue //(optional, default: 0)
					),                
				)
			);
		$res['0'] = $client->createGarment($data);
		$res['1'] = '';
		return ($res);
	}
	
	static public function			createGarmentColor($sku, $name, $skuurl, $ScalingValue = 1.0, $VoValue = 0)
	{
	$client = new SoapClient(_ZUGARA_CREATE_GARMENT_COLOR_URL_);
	$data = array(
					'Domain' => $_SERVER['HTTP_HOST'],
					'GarmentName' => $name,// (optional)
					'SKU' => $sku,
					'SKUUrl' => $skuurl,
				   'ScalingValue' => $ScalingValue, //(optional, default: 1.0)
				   'VerticalOffsetValue' => $VoValue //(optional, default: 0)          
				);
	$res = $client->createGarmentColor($data);
	 return $res;
	}
	
	static public 			function deleteGarmentColor($sku, $skuurl)
	{	  
		 $client = new soapclient(_ZUGARA_DELETE_GARMENT_COLOR_URL_);
		 $data = array(
					  'Domain' => $_SERVER['HTTP_HOST'],
					  'SKU' => $sku,
					  'SKUUrl' => $skuurl, //(optional)
				  );
		 $res = array();
		 $res['0'] = $client->deleteGarmentColor($data);
		 $res['1'] = '';	 
		return ($res);
	}
	
	//SOAP : Update the Garment to Zugara DB
	static public 		function updateGarment($sku, $name, $odlurl, $skuurl, $ScalingValue = 1.0, $VoValue = 0)
	{
		$client = new soapclient(_ZUGARA_UPDATE_GARMENT_URL_);
		$res = array();
		$data = array(
            'Domain' => $_SERVER['HTTP_HOST'],
            'GarmentName' => $name,//(optional)
            'SKU' => $sku,
            'Colors' => array(
                0 => array(
		 			'Old_SKUUrl' => $odlurl, //(optional)
                    'SKUUrl' => $skuurl,// (optional)
                    'ScalingValue' => $ScalingValue, //(optional)
                    'VerticalOffsetValue' => $VoValue, //(optional)
                ),                
            )
        );
		$res['0'] = $client->updateGarment($data);
		$res['1'] = '';
		return ($res);
	}
	
	static public		function deleteGarment($sku)
	{
		$client = new soapclient(_ZUGARA_DELETE_GARMENT_URL_);
		$res = array();
		$data = array(
			'Domain' => $_SERVER['HTTP_HOST'],
			'SKU' => $sku);
		$res['0'] = $client->deleteGarment($data);
		$res['1'] = '';
		return $res;
	}
	
	static public 		function deactivateAccount()
	{
		ini_set("soap.wsdl_cache_enabled", "0"); 
		$server = new SoapServer('http://'.$_SERVER['HTTP_HOST']._MODULE_DIR_.'webcamsocialshopper/wss.wsdl');
  		$server->addFunction("deactivateWSSAccount"); 
   		$server->handle();
	}
}
?>