<?php
/**
 * Created by Jiří Zapletal.
 * Date: 06/10/16
 * Time: 07:23
 */

namespace App\Routers;
use Wakers\System\Router;

/**
 * Class RouterFactory
 * Továrnička na routy
 * @package App\Routers
 */
class RouterFactory extends Router
{
    const
        REGEX_PAGE_URL = '(\/[a-z0-9]{1,}(\-[a-z0-9]{1,})*)+',
        REGEX_PAGE_PAGINATE = '[1-9]{1}[0-9]{0,}',
        REGEX_NCS_ACTION = '[a-z]{1,}(\-[a-z]{1,})*',
        REGEX_LANG = '(\/[a-z]{2})?';

    public function createRouter()
    {
        $this->addMatchType('lang', self::REGEX_LANG);
        $this->addMatchType('url', self::REGEX_PAGE_URL);
        $this->addMatchType('paginate', self::REGEX_PAGE_PAGINATE);
        $this->addMatchType('ncs-action', self::REGEX_NCS_ACTION);


        /**
         * Vzor routy pro automatické volání Action UserControlleru (bez parametrů)
         * V tomto případě nelze volat defaultAction()
         * př: /site-manager/user/list
         */
        $this->addRoute('site-manager', '[lang:lang]/site-manager/[a:controller]/[ncs-action:action]', function ($lang, $controller, $action) {

            if ($action == 'default') return [
                'controller' => 'Error',
                'action' => 'default',
                'params' => ['lang' => $lang, 'statusCode' => 404]
            ];

            return [
                'controller' => $controller,
                'action' => $action,
                'params' => ['lang' => $lang]
            ];
        });


        /**
         * Routy pro hezké url, včetně stránkování
         */

        // Homepage
        $this->addRoute('homepage', '[lang:lang]/', function ($lang) { return [
            'controller' => 'Page',
            'action' => 'homepage',
            'params' => ['lang' => $lang]
        ];});

        // př: /url-adresa/se-strankovanim/strana-37
        $this->addRoute('page-paginate', '[lang:lang][url:url]/strana-[paginate:paginate]', function ($lang, $url, $paginate) { return [
            'controller' => 'Page',
            'action' => 'paginate',
            'params' => ['url' => $url, 'paginate' => $paginate, 'lang' => $lang],
            'templateByAction' => FALSE
        ];});

        // př: /url-adresa/bez-strankovani
        $this->addRoute('page-url', '[lang:lang][url:url]', function ($lang, $url) { return [
            'controller' => 'Page',
            'action' => 'default',
            'params' => ['url' => $url, 'lang' => $lang],
            'templateByAction' => FALSE
        ];});

        $this->match();
    }
}