<?php

namespace Plainwork\Ui\Renderer;

use Plainwork\Ui\Ui;
use Plainwork\Http\Request;
use Plainwork\Ui\Component\Pager;
use Plainwork\Ui\Component\Link;
use Plainwork\Ui\Component\Icon;
use Plainwork\Ui\Component\InlineList;

class PagerRenderer
{
    public $ui = Ui::class;

    public function __invoke(Pager $el)
    {
        $ret = '';

        $paramOffset = $el->getParamOffset();
        $paramLimit = $el->getParamLimit();

        $totalCount = $el->getTotalCount();
        $limit = $el->getLimit();
        $offset = $el->getOffset();

        if (!$totalCount) {
            return $ret;
        }
        if ($totalCount <= $limit) {
            return $ret;
        }

        $ret = new InlineList();

        $displayed1 = $totalCount ? $offset + 1 : 0;
        $displayed2 = $limit ? min($offset + $limit, $totalCount) : $totalCount;

        $prevOffset = null;
        if ($limit && $offset) {
            $prevOffset = max($offset - $limit, 0);
        }

        $firstOffset = null;
        if ($offset && $prevOffset) {
            $firstOffset = 0;
        }

        $nextOffset = null;
        if ($limit && ($totalCount > $displayed2)) {
            $nextOffset = $offset + $limit;
        }

        $lastOffset = null;
        if ($limit && ($totalCount > $limit)) {
            $lastOffset = (ceil($totalCount / $limit) - 1) * $limit;
            if ($lastOffset == $nextOffset) {
                $lastOffset = null;
            }
            if ($lastOffset == $offset) {
                $lastOffset = null;
            }
        }

        if (null !== $firstOffset) {
            $p = [$paramOffset => $firstOffset ? $firstOffset : null];
            $v = (new Link('.', $p, new Icon('&laquo;'), '__First page__'))
                ->withAttr('title', '__First page__')
                ->withAttr('data-pw-keep-scroll', true)
                ->setMenu()
                ;
            $ret->add($v);
        }

        if (null !== $prevOffset) {
            $p = [$paramOffset => $prevOffset ? $prevOffset : null];
            $v = (new Link('.', $p, new Icon('&laquo;'), '__Previous page__'))
                ->withAttr('title', '__Previous page__')
                ->withAttr('data-pw-keep-scroll', true)
                ->setMenu()
                ;
            $ret->add($v);
        }

        if ($displayed1 == $displayed2) {
            $v = $displayed1;
        } else {
            $v = $displayed1 . ' - ' . $displayed2;
        }
        $v .= ' / ' . $totalCount;
        $ret->add($v);

        if ($nextOffset) {
            $p = [$paramOffset => $nextOffset];
            $v = (new Link('.', $p, '__Next page__', new Icon('&raquo;')))
                ->withAttr('title', '__Next page__')
                ->withAttr('data-pw-keep-scroll', true)
                ->setMenu()
                ;
            $ret->add($v);
        }

        if ($lastOffset) {
            $p = [$paramOffset => $lastOffset];
            $v = (new Link('.', $p, '__Last page__', new Icon('&raquo;')))
                ->withAttr('title', '__Last page__')
                ->withAttr('data-pw-keep-scroll', true)
                ->setMenu()
                ;
            $ret->add($v);
        }

        return $ret;
    }
}
