<?php

namespace apexl\Io\modules\display\pages;

use apexl\Config\ConfigurationInterface;
use apexl\Io\includes\HookManager;
use apexl\Io\modules\component\Factories\GenericComponentFactory;
use apexl\Io\modules\display\ComponentFactory\MenuComponentFactory;
use apexl\Io\modules\display\components\HorizontalMenu;
use apexl\Io\modules\display\components\ImageTile;
use apexl\Io\modules\display\components\RowWrapper;
use apexl\Io\modules\display\components\TheTopMenuBar;
use apexl\Io\modules\display\components\TitleTile;
use apexl\Io\modules\display\includes\Page;
use apexl\Io\modules\display\regions\ContentRegion;
use apexl\Io\modules\display\regions\PageContentRegion;
use apexl\Io\modules\display\regions\TopBarRegion;
use apexl\Io\modules\menu\services\MenuManager;
use apexl\Io\modules\user\services\CurrentUserFactory;
use apexl\Io\services\HttpPaths;
use apexl\Io\services\VariableManager;
use Exception;

class genericPageTopBar extends Page
{
    public function __construct(
        private readonly VariableManager $variableManager,
        private readonly MenuComponentFactory $menuComponentFactory,
        private readonly CurrentUserFactory $currentUserFactory,
        ConfigurationInterface $config,
        MenuManager $menuManager,
        HttpPaths $path,
        GenericComponentFactory $componentFactory,
        HookManager $hookManager
    ) {
        parent::__construct(
            $config,
            $menuManager,
            $path,
            $componentFactory,
            $hookManager
        );
    }

    public function addToTopBar(array $component, ?int $weight = null): genericPageTopBar
    {
        return $this->addToRegion($component, "TopBarRegion", $weight);
    }

    public function addToLeftSidebar(array $component, ?int $weight = null): genericPageTopBar
    {
        return $this->addToRegion($component, "LeftSideBarRegion", $weight);
    }

    /**
     * Build the wrapper, specify some default "regions" that can be populated with components.
     *
     * Page structure by default looks like this:
     *  The page content
     *  |------------------------------------------------
     *  | The Top Menu bar
     *  | - Image region -- Menu region --
     *  |------------------------------------------------
     *  | | Main content area
     *  | -----------------------------------------------
     *  | - The footer
     *  | -----------------------------------------------
     * @throws Exception
     */
    protected function buildRegions(): void
    {
        $this->registerTitleComponent(
            $this->componentFactory->make(TitleTile::class, ['page' => $this])->addBreadcrumbs(
                $this->menuManager->getMenu()
            )
        );
        // Reset the regions, we have only content as a default.
        $this->globalRegion = new PageContentRegion()->addWrapperClass('page-top-menu');
        $this->globalRegion->addRegions([
            new TopBarRegion([
                $this->componentFactory
                    ->make(TheTopMenuBar::class)
                    ->addComponent(
                        $this->componentFactory->make(ImageTile::class, [
                            'src' => $this->variableManager->get('generic_page_logo_url') ?? '/assets/logos/logo.svg'
                        ])
                    )
            ])->clearWrapperClasses(),
            new ContentRegion()->addSecondaryComponent(
                $this->componentFactory->make(RowWrapper::class)->addComponent(
                    $this->menuComponentFactory->make(
                        menuItems: $this->menuManager->getAllItemsAsMenu($this->currentUserFactory->get()),
                        menuClass: HorizontalMenu::class
                    )->addClass('col')
                )
            )->clearWrapperClasses()
        ]);
    }
}
