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

require_once(APPPATH . "third_party/einvoice/interfaces/EinvoiceInterface.php");
require_once(APPPATH . "third_party/einvoice/BaseEinvoice.php");

class EinvoiceLoader
{
	protected $countryCode;

	protected $providerRoot;

	protected $providerPath;

	public $provider = null;

	public function __construct($params)
	{
		$this->countryCode = strtoupper($params["countryCode"]);

		// $this->countryCode = "GH";

		$this->providerPath	= APPPATH . 'third_party/einvoice/countries/' . $this->countryCode . '/';

		$this->load_provider();

		if (!empty($params["configValues"]))
		{
			$this->assign_config_values($params["configValues"]);
		}
	}

	protected function load_provider()
	{
		if (!is_dir($this->providerPath))
		{
			throw new Exception("Provider path not available for country " . $this->countryCode);
			return false;
		}

//		$dirs = glob($this->providerPath . '*/', GLOB_ONLYDIR);
/*		foreach ($dirs as $dir)
		{
			$manifest = $dir . 'manifest.json';
			if (file_exists($manifest))
			{
				$data = json_decode(file_get_contents($manifest), true);
				$this->providers[$data['code']] = $data;
			}
		}
*/
		$manifest = $this->providerPath . 'manifest.json';

		if (!file_exists($manifest))
		{
			throw new Exception("Provider manifest not available for country " . $this->countryCode);
			return false;
		}

		$manifest = json_decode(file_get_contents($manifest), true);

		$manifest["providerPath"] = $this->providerPath;

		// $this->provider = $manifest;
		
		$className = ucfirst(strtolower($manifest["class"]));

		$classPath = $this->providerPath . '/' . $className . '.php';

		if (file_exists($classPath))
		{
			require_once $classPath;

			$objEinv = new $className($manifest);

			$this->provider = $objEinv;
		}
	}

	public function assign_config_values($configValues=array())
	{
		$fieldsByKey = array();
		foreach($this->provider->manifest["fields"] as &$grp)
		{
			foreach($grp["fields"] as &$field)
			{
				if (!isset($field["value"]))
				{
					$field["value"] = "";
				}

				$fieldsByKey[$field["key"]] = &$field;
			}
		}

		$configVal = array();
		foreach($configValues as $val)
		{
			$configVal[$val["code"]] = $val["value"];
			if (!empty($fieldsByKey[$val["code"]]))
			{
				$fieldsByKey[$val["code"]]["value"] = $val["value"];
			}
		}

		$this->provider->config = $configVal;
	}

	public function basic_config($select=array("name", "code", "description", "country", "version", "fields"), $tType="")
	{
		if (!$this->provider || !$this->provider->manifest)
		{
			return null;
		}

		if ($tType && !in_array($tType, $this->provider->manifest["tTypes"])) {
			return null;
		}

		$result = array();

		// $select = array("name", "code", "description", "country", "version", "fields");

		foreach($select as $field)
		{
			$result[$field] = $this->provider->manifest[$field];
		}

		return $result;
	}

	public function post($data)
	{
		// $provider = $this->get_provider();
		return $this->provider->post($data);
	}
}

