<?php

//namespace Neteller;

/*use Neteller\Link;
use Neteller\Request;*/

class Payment implements \JsonSerializable{
    private $route = "/paymenthub/v1/payments/";
    private $id;
    private $paymentType;
    private $paymentHandleToken;
    private $merchantRefNum;
    private $currencyCode;
    private $settleWithAuth;
    private $txnTime;
    private $status;
    private $gatewayReconciliationId;
    private $amount;
    private $liveMode;
    private $updatedTime;
    private $statusTime;
    private $gatewayResponse;
    private $availableToSettle;
    private $neteller;
    private $dupCheck = true;
    private $availableToRefund;
    private $customerIp;
    private $description;
    private $links; 

    /*use SetterTrait;
	use GetterTrait;
    use JsonSerializeTrait;*/
    
    protected function setId($id){
		$this->id = $id;
	}
	
	protected function setPaymentType($paymentType){
		$this->paymentType = $paymentType;
    }
    
    protected function setPaymentHandleToken($paymentHandleToken){
		$this->paymentHandleToken = $paymentHandleToken;
    }
    
    protected function setMerchantRefNum($merchantRefNum){
		$this->merchantRefNum = $merchantRefNum;
    }
    
    protected function setCurrencyCode($currencyCode){
		$this->currencyCode = $currencyCode;
    }
    
    protected function setAmount($amount){
		$this->amount = $amount;
    }
    
    protected function setDescription($description){
		$this->description = $description;
    }
  
    //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->payments[0]));
	}

    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;
    }

    protected function updateSelfData($data){
        $jsonData = json_decode($data, true);

        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);
                        array_push($this->$key, $newLink);
                        //$this->$key[] = new Link($val);
                    }
                }else{
                    $classname =  __NAMESPACE__ . "\\" . ucfirst($key);
                    $this->$key = new $classname($value);
                }
            }
            else{
                $this->__set($key, $value);
            }
        }
    }

    public function create($apiKey, $apiUrl){
        require_once $_SERVER["DOCUMENT_ROOT"]."/protected/config/Neteller2/Neteller/Request.php";
        $request = new Request($apiKey);
        var_dump($apiKey);die();
        $response = $request->makeRequest("POST", $apiUrl . $this->route , json_encode($this));
        $this->updateSelfData($response['body']);
    }

    public function lookupById($apiKey, $apiUrl){
        $request = new Request($apiKey);
        $response = $request->makeRequest("GET", $apiUrl . $this->route . $this->id, null);
        $this->updateSelfData($response['body']);
    }
}

