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

class BaseEinvoice
{
	public $manifest = [];

	public $config = [];

	protected $formatter = null;

	public function __construct($manifest)
	{
		$this->manifest = $manifest;

		// $this->fetch_config();
	}

	protected function fetch_config()
	{
		if (!$this->manifest)
		{
			return false;
		}

		$config = array();

		foreach($this->manifest["fields"] as $field)
		{
			if ($field["type"] == "group")
			{
				foreach($field["fields"] as $fld)
				{
					$config[$fld["key"]] = $fld["value"];
				}
			}
		}

		$this->config = $config;

		return $config;
	}

	protected function send_request($url, $payload, $headers = array())
	{
		$ch = curl_init();

		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_POST, true);
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($ch, CURLOPT_HTTPHEADER, array_merge(array(
			'Content-Type: application/json'
		), $headers));

		// curl_setopt_array($ch, array());

		if (empty($payload))
		{
			curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
		}
		else if (is_array($payload))
		{
			curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
			curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($payload));
		}

		// Timeout in seconds
		curl_setopt($ch, CURLOPT_TIMEOUT, 30);

		$response = curl_exec($ch);

		$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);

		curl_close($ch);

		return array(
			'status'	=> $status,
			'response'	=> $response
		);
	}

	private function _load_formatter($tType)
	{
		$formatter_path = $this->manifest["providerPath"] . "formatter/";

		$formatter = strtoupper($tType) . "_Formatter";

		if (file_exists($formatter_path . $formatter . ".php"))
		{
			require_once($formatter_path . $formatter . ".php");
			$this->formatter = new $formatter();
			return $this->formatter;
		}
		else
		{
			$this->formatter = null;
			throw new Exception("Formatter for '$tType' is not supported by this provider.");
		}
	}

	public function format($trans, $config=null)
	{
		// $trans = $data["trans"];

		$this->_load_formatter($trans["tType"]);

		$formattedData = $this->formatter->format($trans,$config);

		return $formattedData;
	}

	public function validate($data)
	{
	}

	public function post($data)
	{
		$formattedData = $this->format($data);
	}

	public function save($data)
	{
		if (empty($data))
		{
			return false;
		}

		$CI = & get_instance();
		$CI->db->where(array(
			'id' => $data['id'],
			'cId' => $data['cId']
		));
		$CI->db->set('info', json_encode($data['info']));
		$CI->db->set('isLocked', SEINV);
		$CI->db->update('trans');
	}

	public function get_qr($qrContent="", $file="")
	{
		require_once(APPPATH . "third_party/phpqrcode/phpqrcode.php");

		ob_clean();

		$levels = array('L','M','Q','H');
		$errorCorrectionLevel = $levels[0];
		$matrixPointSize = 5; // 1 - 10

		$file = tempnam('temp', "einvoice_qr_") . ".png";

		QRcode::png($qrContent, $file, $errorCorrectionLevel, $matrixPointSize, 2);

		// QRcode::png($qrContent, false, $errorCorrectionLevel, $matrixPointSize, 2);
		return $file;
	}
}
