<?php
namespace ModulesAbstract\PluginReports\Report\Model;

class ErrorReport
{
	/**
	 * Stores file full path
	 * @var string
	 */
	protected $fileFullPath = null;

	/**
	 * Stores file handler
	 */
	private $fh = null;

	/**
	 * Stores the file header
	 * @var array
	 */
	protected $fileHeader = ['report', 'field', 'message', 'value', 'identifiers', 'context', ];

	/**
	 * Contructor
	 * @param string $directory
	 */
	public function __construct($path)
	{
		$this->fileFullPath = $path;
	}

	/**
	 * Create file
	 * @param string $directory
	 * @throws \Exception
	 */
	protected function openFile($fileHeader)
	{
		// open file
		$this->fh = fopen($this->fileFullPath, "w");
		if (!$this->fh) {
			throw new \Exception("Cannot open for writing {$this->fileFullPath}");
		}

		// write file header
		if (fputcsv($this->fh, $fileHeader, ",", "\"", "") === false) {
			throw new \Exception("Cannot write to {$this->fileFullPath}");
		}
	}

	/**
	 * Close file
	 * @throws \Exception
	 */
	protected function closeFile()
	{
		if ($this->fh) {
			if (!fclose($this->fh)) {
				throw new \Exception("Cannot close file {$this->fileFullPath}");
			}

			if (!\Engine\File::setDefaultPermissions($this->fileFullPath)) {
				throw new \Exception("Cannot set permissions for {$this->fileFullPath}");
			}
		}
	}

	/**
	 * Destroy main function
	 */
	public function destroy()
	{
		// close file
		$this->closeFile();
	}

	/**
	 * Log message
	 * @param string $level
	 * @param string $reportName
	 * @param string $field
	 * @param string $message
	 * @param string $value
	 * @param array $identifiers
	 * @param array $context
	 * @throws \Exception
	 */
	public function log($level, $reportName, $field, $message, $value, array $identifiers = [], array $context = [])
	{
		if (!in_array($level, ['error', 'info', 'skip'])) {
			throw new \Exception(sprintf("Invalid error level: %s", $level));
		}

		if (!$this->fh) {
			$this->openFile($this->fileHeader);
		}

		// compose message
		$errMessage = $this->composeErrorMessage($level, $reportName, $field, $message, $value, $identifiers, $context);

		// write to file
		if (fputcsv($this->fh, array_values($errMessage), ",", "\"", "")===false) {
			throw new \Exception("Cannot write to {$this->fileFullPath}");
		}
	}


	public function logRaw($level, $errMessage)
	{
		if (!in_array($level, ['error', 'info', 'skip'])) {
			throw new \Exception(sprintf("Invalid error level: %s", $level));
		}

		if (!$this->fh) {
			$this->openFile(array_keys($errMessage));
		}

		// write to file
		if (fputcsv($this->fh, array_values($errMessage), ",", "\"", "")===false) {
			throw new \Exception("Cannot write to {$this->fileFullPath}");
		}
	}

	/**
	 * Compose error message
	 * @param string $level
	 * @param string $reportName
	 * @param string $field
	 * @param string $message
	 * @param string $value
	 * @param array $identifiers
	 * @param array $context
	 * @return array
	 */
	protected function composeErrorMessage($level, $reportName, $field, $message, $value, array $identifiers, array $context)
	{
		$ret= [];

		$ret['report'] =		$reportName;
		$ret['field'] =			$field;
		$ret['message'] = 		$message;
		$ret['value'] =			$value;
		$ret['identifiers'] = 	($identifiers?json_encode($identifiers, true):'');
		$ret['context'] = 		($context?json_encode($context, true): '');

		return $ret;
	}

	public function getErrorLogs()
	{

	}

}