<?php

namespace Opentalent\OtCore\Controller;

use Opentalent\OtAdmin\Controller\SiteController;
use Opentalent\OtCore\Exception\NoSiteSelected;
use Opentalent\OtCore\Exception\NoSuchWebsiteException;
use Opentalent\OtCore\Website\OtPageRepository;
use Psr\Http\Message\ResponseInterface;
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Extbase\Mvc\Controller\Arguments;
use TYPO3\CMS\Extbase\Mvc\Exception\StopActionException;
use TYPO3\CMS\Extbase\Mvc\RequestInterface;
use TYPO3\CMS\Extbase\Object\ObjectManager;

/**
 * Base class for all controllers of backend modules that need
 * a typo3 site's page to be selected to execute **ALL** of its actions methods
 *
 * If no page is selected or if this page does not have any root page as an ancestor,
 * the action is forwarded to a page asking the user to select one.
 *
 * If only one website appears in the page-tree, this site is automatically selected
 *
 * Class BeOnSiteController
 */
class SelectedSiteController extends ActionController
{
    /**
     * The current website
     *
     * @var array
     */
    protected $currentWebsite;

    /**
     * The current site root page uid
     *
     * @var int
     */
    protected $currentRootUid;

    /** [FOR TESTS ONLY] */
    protected $preventPropagation = false;

    /**
     *
     * @throws StopActionException
     * @throws NoSuchWebsiteException
     * @throws \Exception
     */
    protected function callActionMethod(RequestInterface $request): ResponseInterface {

        // Check if the current be-user has a db_mountpoint, and only has one.
        // If so, this will be considered as the selected site (since its the only one available)
        $mountpoints = $this->otPageRepository->getCurrentBeUserMountpoints();

        if ($mountpoints && count($mountpoints) === 1) {
            $this->currentRootUid = (int)$mountpoints[0];
        } else {
            // No db mountpoint has been set up, or more than one.
            // Now we check if a site's page is selected
            try {
                $this->currentRootUid = $this->otPageRepository->getCurrentBeRootUid();
            } catch (NoSiteSelected $e) {
                $this->currentRootUid = null;
            }
        }

        if ($this->currentRootUid) {
            $this->currentWebsite = $this->otWebsiteRepository->getWebsiteByPageUid($this->currentRootUid);
        }

        // [TESTS ONLY]
        if ($this->preventPropagation) { return $this->callParentActionMethod($request); }

        // No site is selected, redirect to the warning page
        if ($this->actionMethodName != 'displayNoSelectedPageWarningAction' && $this->currentRootUid == null) {
            throw new \Exception("No website selected");
            // forward does not work correctly when called from another extension...
            // $this->forward('displayNoSelectedPageWarning', 'SelectedSite', 'OtCore');
        }

        return $this->callParentActionMethod($request);
    }

    protected function callParentActionMethod(RequestInterface $request): ResponseInterface
    {
        return parent::callActionMethod($request);
    }

    protected function displayNoSelectedPageWarningAction() {}

//    /**
//     * [TESTS ONLY]
//     * @param Arguments $arguments
//     * @return void
//     */
//    public function injectArguments(Arguments $arguments): void
//    {
//        $this->arguments = $arguments;
//    }
}
