<?php
namespace Engine\Logger\Context;

class StringRaw extends \Engine\Logger\Context\GeneralAbstract
{
	protected function calculateDimensionClass($text)
	{
		$rows = explode("\n", $text);
		$maxRowLen = 0;
		foreach ($rows as $r) {
			$maxRowLen = max($maxRowLen, strlen($r));
		}

		$classes = [];
		if (count($rows)>40) {
			$classes[]= 'limit-height';
		}
		if ($maxRowLen>60) {
			$classes[]= 'limit-width';
		}

		return $classes;
	}

	protected function renderOperations($classes)
	{
		$outputOperations = "";
		$outputOperations.= sprintf("<div class='operations %s'>", implode(" ", $classes));
		$outputOperations.= $this->renderOperations_groups($classes);
		$outputOperations.= "</div>";

		return $outputOperations;
	}

	protected function renderOperations_groups($classes)
	{
		$outputOperations = "";
		if (in_array('limit-height', $classes)) {
			$outputOperations.= "<span class='group'>";
			$outputOperations.= "<button data-group='height' data-type='collapse'>minimize</button>";
			$outputOperations.= "<button data-group='height' data-type='maximize'>maximize &#x2195;</button>";
			$outputOperations.= "<button data-group='height' data-type='reset' class='active'>reset</button>";
			$outputOperations.= "</span>";
		}
		if (in_array('limit-width', $classes)) {
			$outputOperations.= "<span class='group'>";
			$outputOperations.= "<button data-group='width' data-type='maximize'>maximize &#x2194;</button>";
			$outputOperations.= "<button data-group='width' data-type='reset' class='active'>reset</button>";
			$outputOperations.= "</span>";
		}

		$outputOperations.= "<span class='infoGroup'>";
		if (is_string($this->value) && $this->value) {
			$outputOperations.= sprintf("<div class='size'>%s</div>", \Engine\File::formatBytes(strlen((string)$this->value)));

			$attributes = [];
			if (json_decode($this->value)) {
				$attributes[]= 'json format';
			}

			$prevValue = libxml_use_internal_errors(true);
			$xml = simplexml_load_string($this->value);
			libxml_use_internal_errors($prevValue);
			if ($xml) {
				$attributes[]= 'xml format';
			}

			if (base64_decode($this->value, true) && mb_detect_encoding($this->value, null, true)=='ASCII') {
				$attributes[]= 'base64 encoded';
			}

			$attributes[]= (mb_detect_encoding($this->value, null, true)?:'unknown encoding');
			$outputOperations.= sprintf("<div class='analysis'>%s</div>", implode(", ", $attributes));
		}
		$outputOperations.= "</span>";

		return $outputOperations;
	}

	public function render($logRow=null, $key=null)
	{
		$this->value = (string)$this->value;

		$output = '';
		if (strlen($this->value)>(2*1024*1024)) {	// value is too big to show in the browser
			$classes = [];
			$output.= sprintf("<div>String %s, having %s, is too large to be displayed in the browser</div>", $key, \Engine\File::formatBytes(strlen($this->value)));
		}
		else {
			$classes = $this->calculateDimensionClass($this->value);
			$output.= sprintf("%s<pre class='string %s'><span>%s</span></pre>", $this->renderOperations($classes), 	implode(" ", $classes), htmlspecialchars($this->value));
		}

		if (strlen($this->value)>1024) {
			$output.= $this->renderOperations_download($classes, $logRow, $key);
		}

		return $output;
	}

	protected function renderOperations_download_determineData(array $classes, $logRow=null, $key=null)
	{
		$fileName = "file";
		$fileExtension = "txt";
		if ($logRow && $key) {
			$fileName = sprintf("%s-%s", $logRow['logSession'], $key);
		}

		$value = $this->value;

		return [$fileName, $fileExtension, $value];
	}

	protected function renderOperations_download(array $classes, $logRow=null, $key=null)
	{
		list($fileName, $fileExtension, $value) = $this->renderOperations_download_determineData($classes, $logRow, $key);

		return $this->generateDownloadLink($value, $fileName, $fileExtension);
	}

}
