<?php

declare(strict_types=1);

namespace Kitab\DocTest\Report\Cli\Fields;

use mageekguy\atoum\report\fields;

class Errors extends fields\runner\errors\cli
{
    public function __toString()
    {
        $string = '';

        if (null !== $this->runner) {
            $errors = $this->runner->getScore()->getErrors();

            $sizeOfErrors = count($errors);

            if ($sizeOfErrors > 0) {
                $string .=
                    $this->titlePrompt .
                    sprintf(
                        $this->locale->_('%s:'),
                        $this->titleColorizer->colorize(sprintf($this->locale->__('There is %d error', 'There are %d errors', $sizeOfErrors), $sizeOfErrors))
                    ) .
                    "\n"
                ;

                $class = null;
                $method = null;

                foreach ($errors as $error) {
                    $error['oldFile'] = $error['file'];
                    $error['file']    = basename($error['file']);

                    if ($error['class'] !== $class || $error['method'] !== $method) {
                        $string .=
                            $this->methodPrompt .
                            sprintf(
                                $this->locale->_('%s:'),
                                $this->methodColorizer->colorize($error['class'] . '::' . $error['method'] . '()')
                            ) .
                            "\n"
                        ;

                        $class = $error['class'];
                        $method = $error['method'];
                    }

                    $string .= $this->errorPrompt;

                    $type = static::getType($error['type']);
                    $case = $error['case'] === null ? '' : sprintf(' in case \'%s\'', $error['case']);

                    switch (true) {
                        case $error['file'] === null:
                            switch (true) {
                                case $error['errorFile'] === null:
                                    $errorMessage = $this->locale->_('Error %s in unknown file on unknown line%s, generated by unknown file', $type, $case);
                                    break;

                                case $error['errorLine'] === null:
                                    $errorMessage = $this->locale->_('Error %s in unknown file on unknown line, generated by file %s%s', $type, $error['errorFile'], $case);
                                    break;

                                case $error['errorLine'] !== null:
                                    $errorMessage = $this->locale->_('Error %s in unknown file on unknown line, generated by file %s on line %d%s', $type, $error['errorFile'], $error['errorLine'], $case);
                                    break;
                            }
                            break;

                        case $error['line'] === null:
                            switch (true) {
                                case $error['errorFile'] === null:
                                    $errorMessage = $this->locale->_('Error %s in %s on unknown line, generated by unknown file%s', $type, $error['file'], $case);
                                    break;

                                case $error['errorLine'] === null:
                                    $errorMessage = $this->locale->_('Error %s in %s on unknown line, generated by file %s%s', $type, $error['file'], $error['errorFile'], $case);
                                    break;

                                case $error['errorLine'] !== null:
                                    $errorMessage = $this->locale->_('Error %s in %s on unknown line, generated by file %s on line %d%s', $type, $error['file'], $error['errorFile'], $error['errorLine'], $case);
                                    break;
                            }
                            break;

                        default:
                            switch (true) {
                                case $error['errorFile'] === null:
                                    $errorMessage = $this->locale->_('Error %s in %s on line %d, generated by unknown file%s', $type, $error['file'], $error['line'], $case);
                                    break;

                                case $error['errorLine'] === null:
                                    $errorMessage = $this->locale->_('Error %s in %s on line %d, generated by file %s%s', $type, $error['file'], $error['line'], $error['errorFile'], $case);
                                    break;

                                case $error['errorLine'] !== null:
                                    $errorMessage = $this->locale->_('Error %s in %s on line %d, generated by file %s on line %d%s', $type, $error['file'], $error['line'], $error['errorFile'], $error['errorLine'], $case);
                                    break;
                            }
                            break;
                    }

                    $string .= sprintf(
                            $this->locale->_('%s:'),
                            $this->errorColorizer->colorize(($errorMessage))
                        ) .
                        "\n"
                    ;

                    $error['message'] = preg_replace(
                        [
                            '/Stack trace:.*/s',
                            '/' . preg_quote($error['oldFile'], '/') . '/'
                        ],
                        [
                            '',
                            basename($error['oldFile'])
                        ],
                        $error['message']
                    );

                    $string .= $error['message'];
                }
            }
        }

        return $string;
    }
}