<?php

namespace apexl\Io\modules\userDisplay\components;

use apexl\Io\exceptions\RouteNotFoundException;
use apexl\Io\includes\RouteManager;
use apexl\Io\modules\component\Collections\ComponentCollection;
use apexl\Io\modules\component\Collections\ComponentPropertyCollection;
use apexl\Io\modules\component\Factories\ComponentPropertyFactory;
use apexl\Io\modules\display\components\DropdownMenu;
use apexl\Io\modules\menu\includes\menuItem;
use apexl\Io\modules\user\interfaces\MasqueradeServiceInterface;
use apexl\Io\modules\user\services\CurrentUserFactory;
use Arrayy\Type\StringCollection;

final readonly class AccountMenuDropdown extends DropdownMenu
{
    public function __construct(
        private MasqueradeServiceInterface $masqueradeService,
        private RouteManager $routeManager,
        ComponentPropertyFactory $propertyFactory,
        ComponentPropertyCollection $props,
        ComponentCollection $components,
        StringCollection $classes,
        CurrentUserFactory $currentUserFactory
    ) {
        parent::__construct($propertyFactory, $props, $components, $classes, $currentUserFactory);
    }

    /**
     * @throws RouteNotFoundException
     */
    public function init(): void
    {
        $currentUser = $this->currentUserFactory->get();

        $this->addMenuItems([
            $this->logoutOrEndMasqueradeItem()
        ])
            ->addIcon("user-circle")
            ->addClass('col top-bar-user-menu')
            ->setTitle(
                title: $currentUser->getNiceName(),
            );
    }

    /**
     * @throws RouteNotFoundException
     */
    private function logoutOrEndMasqueradeItem(): menuItem
    {
        return $this->masqueradeService->isMasquerading() ? new menuItem(
            [
                'routeName' => 'user.masquerade.end',
                'title' => 'End Masquerade',
                'icon' => 'arrow-right-from-bracket',
                'weight' => 999,
            ],
            $this->routeManager->getRoute('user.masquerade.end')
        ) : new menuItem(
            [
                'routeName' => 'user.logout',
                'title' => 'Logout',
                'icon' => 'arrow-right-from-bracket',
                'weight' => 999,
            ],
            $this->routeManager->getRoute('user.logout'),
        );
    }
}
