<?php
namespace Engine\Debug\Dline;

/**
 * Class used by the Dline class, used for Cli output
 * For now its not implemented
 * @author Lucian Sirbu
 */
class Cli extends \Engine\Debug\Dline\Text
{
    /**
	 * (non-PHPdoc)
	 * @see \Engine\Debug\Dline\GeneralAbstract::formatValue()
	 */
	public function formatValue($var, $repl)
	{
		if($repl['ignore']) return '';

		$color = str_replace('#', '', $repl['color']);

		$autoType = $this->detectVarType($var);

		if (!$repl['type']) {
			$repl['type'] = $autoType;
		}

		if (!$repl['color']) {
			switch ($autoType) {
				default:
				case 'string':
					$color ='string';
				break;
				case 'int': case 'float':
					$color ='numeric';
				break;
				case 'bool':
					$color = ($var?'bool-true':'bool-false');
				break;
				case 'array-numeric':
					$color = 'array-numeric';
				break;
				case 'array-associative':
					$color = 'array-associative';
				break;
			}
		}

        $output = $this->formatValue_renderValue($var, $repl);
        $output = htmlspecialchars_decode($output);

        // @see https://misc.flogisoft.com/bash/tip_colors_and_formatting
        switch ($color) {
            case 'string':
                // do nothing for basic strings
                $output = "\e[0m${output}\e[0m";
            break;
            case 'numeric':
                $output = "\e[36m${output}\e[0m";
            break;
            case 'bool-true':
                $output = "\e[35m${output}\e[0m";
            break;
            case 'bool-false':
                $output = "\e[95m${output}\e[0m";
            break;
            case 'array-numeric':
                $output = "\e[94m${output}\e[0m";
            break;
            case 'array-associative':
                $output = "\e[96m${output}\e[0m";
            break;

            default:
                // convert hex-style colors to cli eqivalents

                // "bin" hex-style notation to smaller, less-variable values
                $colorArr = str_split($color, 1);

                // convert to int, "bin" it to 3 values
                foreach ($colorArr as $k=>$v) {
                    $v = hexdec($colorArr[$k]);
                    if ($v<4) {
                        $v = 0;
                    }
                    elseif ($v<12) {
                        $v = 1;
                    }
                    else {
                        $v = 2;
                    }

                    $colorArr[$k] = $v;
                }
                $binnedColor = sprintf("%d%d%d", $colorArr[0], $colorArr[1], $colorArr[2]);

                $cliColorAssoc = [
                    "\e[0m" => ["000", "222",  ],
                    "\e[37m" => ["111", ],
                    "\e[31m" => ["100", "200", ],
                    "\e[32m" => ["010", "020", ],
                    "\e[34m" => ["001", "002", ],
                    "\e[33m" => ["110", "220", ],
                    "\e[36m" => ["011", "022", ],
                    "\e[35m" => ["101", "202", ],
                ];

                $pickedCliColor = "";
                foreach ($cliColorAssoc as $cliColor=>$colors) {
                    if (in_array($binnedColor, $colors)) {
                        $pickedCliColor = $cliColor;
                        break;
                    }
                }

                $output = "{$pickedCliColor}${output}\e[0m";
            break;
        }

		return $output;
	}

}
