<?php defined('BASEPATH') OR exit('No direct script access allowed');

class Evat extends BaseEinvoice implements EinvoiceInterface
{
	public function __construct($manifest)
	{
		parent::__construct($manifest);
	}

	public function validate($data)
	{
	}

	public function post($data)
	{
		$transId = $data["id"];

		$this->validate($data);

		$formattedData = $this->format($data);
// print_R($formattedData);exit;
		$result = $this->call_request("/invoice", $formattedData);
// print_R($result);
		if ($result && !empty($result["response"]))
		{
			// $result["status"]
			// $result["response"]
			$resp = json_decode($result["response"], TRUE);
// print_R($resp);
			if (!empty($resp["response"]))
			{
				$apiResp = $resp["response"];

				if ($apiResp["status"] == "SUCCESS")
				{
					require_once(APPPATH . "third_party/einvoice/TransIrnInfo.php");

					// Url of online verficiation link
					$qrContent = $apiResp["qr_code"];

					$info = array(
						"S_IRN" => new TransIrnInfo(array(
							"SignedInvoice" => $qrContent,
							"SignedQRCode" => $qrContent
						)),
						"rawResp" => $apiResp
					);
					$saveData = array(
						"id" => $data["id"],
						"cId" => $data["cId"],
						"info" => $info
					);

					$this->save($saveData);
					// $qr_image = $this->get_qr($apiResp["qr_code"]);
					return $info;
				}
			}
			else
			{
				$msg = "E-Vat error : ";
				$msg .= empty($resp["code"]) ? "" : ($resp["code"] . " - ");
				$msg .= empty($resp["message"]) ? "" : $resp["message"];
				$msg .= empty($resp["errors"]) ? "" : implode(PHP_EOL, $resp["errors"]);

				throw new Exception($msg);
			}
		}
		else
		{
			return false;
		}
	}

	public function cancel($invoiceId)
	{
		
	}

	public function get_status($invoiceId)
	{
		
	}

	public function get_details($invoiceId)
	{
		
	}

	public function get_taxNo_details($taxNo="")
	{
		try {

			$result = $this->call_request("/identification/tin/" . $taxNo, array());

			if ($result && !empty($result["data"]))
			{
				$taxNoContact = $result["data"];

				$taxNoContact["name"];
				$taxNoContact["address"];
			}
		} catch (Exception $e) {
			$this->message = $e->getMessage();
			return false;
		}
	}

	private function call_request($urlSuffix, $payload)
	{
		$manifest = $this->manifest;

		$config = $this->config;

		if (empty($config["env"]))
		{
			$config["env"] = "staging";
		}

		// Test Values from portal
		// $config["Company_Reference"] = "CXX000000YY-001";
		// $config["security_key"] = "Z60gftKe9sei3xOZhvvDa0StkVILKR3j5MBM9ygi1zg=";

		if (empty($config["Company_Reference"]))
		{
			throw new Exception("Company Reference required.");
			return false;
		}

		if (empty($config["security_key"]))
		{
			throw new Exception("Security key required.");
			return false;
		}

		$url = $manifest["api_url"][$config["env"]] . "/" . $config["Company_Reference"] . $urlSuffix;

		$headers = array(
			"security_key: " . $config["security_key"]
		);

		return $this->send_request($url, $payload, $headers);
	}
}

