<?php

use Application\Service\Extension\Helper\RouterHelper;
use Laminas\ServiceManager\Factory\InvokableFactory;

/**
 * 下面是定义路由地址的规则
 *
 * 如果路由定义重复了, 这里会覆盖掉之前定义的路由地址
 *
 * 路由地址 => [控制器类, 方法名]
 * '/item/field/view' => ['ItemFieldControllerV2', 'view'],
 *
 * ItemFieldControllerV2 相当于是 \Application\Controller\v2\ItemFieldController::class
 * 因为在下面的 CONTROLLER_ALIAS 定义了别名
 *
 * 你也可以这么写
 * '/item/field/view' => [\Application\Controller\v2\ItemFieldController::class, 'view'],
 */

$mainRoutes = [
//    '/item/field/view' => ['ItemFieldControllerV2', 'view'],

    '/patient/form/inactive' => ['PatientFormControllerV2', 'inactive'], // 受试者表单失活

    '/patient/form/compare' => [\Application\Controller\item\patient\FormController::class, 'compare'], // 受试者表单失活
];


return new class ($mainRoutes) {

    private array $_routes = [];

    private array $_controllers = [];

    const CONTROLLER_ALIAS = [
        'ItemFieldControllerV2' => \Application\Controller\v2\ItemFieldController::class,
        'PatientFormControllerV2' => \Application\Controller\v2\PatientFormController::class,
    ];

    public function __construct($routes)
    {
        $this->_routes = $routes;
    }

    private function _getControllerClass(string $controllerClass)
    {
        $class = self::CONTROLLER_ALIAS[$controllerClass] ?? $controllerClass;

        if (!isset($this->_controllers[$class])) {
            $this->_controllers[$class] = InvokableFactory::class;
        }

        return $class;
    }

    public function getRoutes(): array
    {
        $res = [];
        foreach ($this->_routes as $url => $params) {
            $res[$url] = RouterHelper::literalRoute($url, $this->_getControllerClass($params[0]), $params[1]);
        }

        return $res;
    }

    public function getControllers(): array
    {
        return $this->_controllers;
    }
};