<?php


namespace App\View;

/**
 * Collapsible text box widget
 */
class CollapsibleTextBoxWidget
{
    private $maxHeight;
    private $collapsing;
    private $text;
    private $srPrefix;
    private $srEscape;

    /**
     * Add a prefix inside the text box shown only to screen readers, Lynx etc.
     *
     * @param string $content
     * @param bool $escape - use html escape, default true
     * @return $this
     */
    public function srPrefix($content, $escape=true)
    {
        $this->srEscape = $escape;
        $this->srPrefix = $content;
        return $this;
    }

    /**
     * @param string $text - text to show
     * @param int $thresholdLength - mb_character length that triggers the collapsible behavior; choose experimentally
     * @param string $maxHeight - max height CSS value (e.g. 8em)
     */
    public function __construct($text, $thresholdLength, $maxHeight)
    {
        $this->text = $text;
        $this->collapsing = mb_strlen($text) > $thresholdLength;
        $this->maxHeight = $maxHeight;
    }

    private function processText($t)
    {
        $out = e($t);
        $out = str_replace("\r\n", "\n", $out);

        $out = '<p class="last-p-mb-0"> '.str_replace("\n\n",
                " </p>". '<p class="last-p-mb-0"> ', $out).' </p>';

        $out = nl2br($out);

        $out = preg_replace_callback('#\b((https?://|ftp://|mailto:)[^\s]+)#i', function($m) {
            $href = $m[1];
            $text = $href;
            if (strpos($text, 'mailto:') === 0) {
                $text = str_replace('mailto:', '', $text);
            }
            return "<a href=\"".e($href)."\">".e($text)."</a>";
        }, $out);

        return $out;
    }

    public function render()
    {
        $content = $this->processText($this->text);

        $prefix = '';
        if ($this->srPrefix) {
            $prefix = '<span class="sr-only">'.
                ($this->srEscape ? e($this->srPrefix) : $this->srPrefix).
                '&nbsp;</span>';
        }

        if ($this->collapsing) {
            return '<div class="block-collapse" 
                         title="Click to toggle"
                         style="max-height:'.$this->maxHeight.'">' . $prefix . $content . '</div>';
        } else {
            return $prefix.'<div>' . $prefix . $content . '</div>';
        }
    }

    public function __toString()
    {
        return (string) $this->render();
    }
}