<?php
    declare(strict_types=1);

    namespace Icing\View;

    use Authentication\View\Helper\IdentityHelper;
    use Cake\Event\EventManager;
    use Cake\Http\Response;
    use Cake\Http\ServerRequest;
    use Icing\View\Helper\AuthHelper;
    use Icing\View\Helper\BreadcrumbsHelper;
    use Icing\View\Helper\FlashHelper;
    use Icing\View\Helper\FormHelper;
    use Icing\View\Helper\HtmlHelper;
    use Icing\View\Helper\NavHelper;
    use Icing\View\Helper\PageActionsHelper;
    use Icing\View\Helper\PaginatorHelper;
    use Icing\View\Helper\UrlHelper;

    /**
     * Icing View
     *
     * @property HtmlHelper        Html
     * @property FormHelper        Form
     * @property FlashHelper       Flash
     * @property PaginatorHelper   Paginator
     * @property BreadcrumbsHelper Breadcrumbs
     * @property IdentityHelper    Identity
     * @property AuthHelper        Auth
     * @property NavHelper         Nav
     * @property PageActionsHelper PageActions
     * @property UrlHelper         $Url
     *
     */
    class View extends \Cake\View\View
    {

        protected string $layout = 'Icing.default';

        protected array $_defaultConfig = [
            'helpers'   => [
                'Url'         => ['className' => 'Icing.Url'],
                'Html'        => ['className' => 'Icing.Html'],
                'Form'        => ['className' => 'Icing.Form'],
                'Flash'       => ['className' => 'Icing.Flash'],
                'Paginator'   => ['className' => 'Icing.Paginator'],
                'Breadcrumbs' => ['className' => 'Icing.Breadcrumbs'],
                'Identity'    => ['className' => 'Authentication.Identity'],
                'Auth'        => ['className' => 'Icing.Auth'],
                'Nav'         => ['className' => 'Icing.Nav'],
                'PageActions' => ['className' => 'Icing.PageActions'],
            ],
            'x-layouts' => [
                'modal' => 'Icing.modal',
                'ajax'  => 'Icing.ajax',
            ],
        ];

        public function __construct(
            ?ServerRequest $request = null,
            ?Response      $response = null,
            ?EventManager  $eventManager = null,
            array          $viewOptions = []
        ) {
            $viewOptions['helpers'] = array_merge($viewOptions['helpers'] ?? [], $this->_defaultConfig['helpers']);
            parent::__construct($request, $response, $eventManager, $viewOptions);
            $this->_setXLayout();
        }


        /**
         * Set the layout based on the X-Layout header.
         *
         * This method allows ajax requests that come from the view to request a layout based on the context of the request.
         * The most common case is when a modal is requested, the layout is requested to be a modal.
         *
         * The x-layouts configuration allows for the mapping of the X-Layout header to a layout file.
         * By default, icing provides a modal layout for modal requests
         *
         * The modal layout used for modal can be changed by changing the mapping of which layout file is used for modal.
         * This can be extended to consider the user group of each user's request and provide a layout based on the user group.
         * $this->setConfig('x-layouts.modal', "$userGroup.modal");
         * would map all requests that request a modal layout to use the "$userGroup.modal" layout file.
         *
         * and X-Layout mapping from configuration
         *
         * @return void
         */
        protected function _setXLayout(): void
        {
            $layout = $this->getRequest()->getHeaderLine('X-Layout');
            $layout = $this->getConfig("x-layouts.$layout", false);
            if ($layout) {
                $this->setLayout($layout);
            }
        }

    }
