<?php

//namespace Neteller;

class PaymentHandle implements \JsonSerializable{
	private $route = "/paymenthub/v1/paymenthandles/";
	private $id;
    private $paymentType = "NETELLER";
    private $paymentHandleToken;
    private $merchantRefNum;
    private $currencyCode;
    private $txnTime;
    private $status;
    private $links;
    private $liveMode;
    private $usage;
    private $action;
    private $executionMode;
    private $amount;
    private $timeToLiveSeconds;
    private $gatewayResponse;
    private $returnLinks;
    private $transactionType;
    private $updatedTime;
    private $statusTime;
    private $neteller;
    private $accountId;
    private $billingDetails;
    private $profile;
    private $customerIp;
	
	/*use SetterTrait;
	use GetterTrait;
	use JsonSerializeTrait;*/
	



	
	protected function setPaymentType($paymentType){
		$this->paymentType = $paymentType;
	}

	protected function setMerchantRefNum($merchantRefNum){
		$this->merchantRefNum = $merchantRefNum;
	}

	protected function setCurrencyCode($currencyCode){
		$this->currencyCode = $currencyCode;
	}	
	
	protected function setAmount($amount){
		$this->amount = $amount;
	}
	
	protected function setReturnLinks(Link $returnLinks){
		$this->returnLinks[] = $returnLinks;
	}

	protected function setTransactionType($transactionType){
		$this->transactionType = $transactionType;
	}

	protected function setNeteller($neteller){
		$this->neteller = $neteller;
	}

	protected function setBillingDetails($billingDetails){
		$this->billingDetails = $billingDetails;
	}

	protected function setProfile($profile){
		$this->profile = $profile;
	}

	protected function setCustomerIp($customerIp){
		$this->customerIp = $customerIp;
	}



	/*use UpdateSelfTrait;
    use CreateTrait;
    use LookupByIdTrait;*/
    
    public function lookupByMerchantRefNum($apiKey, $apiUrl){
		$request = new Request($apiKey);
		$response = $request->makeRequest("GET", $apiUrl . $this->route . "?merchantRefNum=" . $this->merchantRefNum, null);
		$response_obj = json_decode($response['body']);
		$this->updateSelfData(json_encode($response_obj->paymentHandles[0]));
	}

    protected function updateSelfData($data){
        $jsonData = json_decode($data, true);

        //var_dump($jsonData);die();
        //require_once $_SERVER["DOCUMENT_ROOT"]."/protected/config/Neteller2/Neteller/Link.php";
        require_once $_SERVER["DOCUMENT_ROOT"]."/protected/config/Neteller2/Neteller/GatewayResponse.php";
        require_once $_SERVER["DOCUMENT_ROOT"]."/protected/config/Neteller2/Neteller/BillingDetails.php";
        require_once $_SERVER["DOCUMENT_ROOT"]."/protected/config/Neteller2/Neteller/Link.php";
        require_once $_SERVER["DOCUMENT_ROOT"]."/protected/config/Neteller2/Neteller/Profile.php";
        require_once $_SERVER["DOCUMENT_ROOT"]."/protected/config/Neteller2/Neteller/DateOfBirth.php";

        foreach($jsonData as $key => $value){

            if(is_array($value)){

                if($key == "links" | $key == "returnLinks"){
                    $this->$key = array();
                    foreach($value as $val){
                        $newLink = new Link($val);
                        //var_dump($this->$key);
                        array_push($this->$key, $newLink);
                        //var_dump($key, $this->$key);die();
                        //$this->$key[] = new Link($val);

                    }
                //var_dump('111111');die();
                }else{
                    //$classname =  __NAMESPACE__ . "\\" . ucfirst($key);
                    $classname =  ucfirst($key);
                    $this->$key = new $classname($value);
                    //var_dump('111', $classname, $this->$key);die();
                }
            }
            else{
                $this->__set($key, $value);
            }
        }
        //var_dump('2222222');die();
    }

    public function create($apiKey, $apiUrl){
        require_once $_SERVER["DOCUMENT_ROOT"]."/protected/config/Neteller2/Neteller/Request.php";
        $request = new Request($apiKey);
        $response = $request->makeRequest("POST", $apiUrl . $this->route , json_encode($this));
        //var_dump('111');die();
        $this->updateSelfData($response['body']);
        //var_dump('111');die();
    }

    protected function setId($id){
        $this->id = $id;
    }
    public function lookupById($apiKey, $apiUrl){
        require_once $_SERVER["DOCUMENT_ROOT"]."/protected/config/Neteller2/Neteller/Request.php";
        $request = new Request($apiKey);
        $response = $request->makeRequest("GET", $apiUrl . $this->route . $this->id, null);
        $this->updateSelfData($response['body']);
    }


    public function __set($name, $value)
    {
        $setter = 'set'.$name;
        if (method_exists($this, $setter)) {
            $this->$setter($value);
        } else {
            $this->$name = $value;
        }
    }

    public function __get($name){
        return $this->$name;
    }

    public function jsonSerialize()
    {
        $vars = array_filter(get_object_vars($this), function($val){ return $val !== null; });
        return $vars;
    }
}
