<?php
// SPDX-FileCopyrightText: IndieHosters <contact@indiehosters.net>
// SPDX-License-Identifier: AGPL-3.0-or-later

namespace OCA\CustomNav\Settings;

use OCA\CustomNav\AppInfo\Application;
use OCA\CustomNav\Service\Manager;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\AppFramework\Services\IInitialState;
use OCP\IUserSession;
use OCP\Settings\ISettings;
use OCP\Util;

class Personal implements ISettings
{
    private IUserSession $userSession;
    private IInitialState $initialState;
    private Manager $manager;

    public function __construct(
        IUserSession $userSession,
        Manager $manager,
        IInitialState $initialState,
    ) {
        $this->userSession = $userSession;
        $this->manager = $manager;
        $this->initialState = $initialState;
    }

    public function getForm(): TemplateResponse
    {
        $user = $this->userSession->getUser();
        $apps = $this->manager->getDefaultApps();
        $sysOverwrite = $this->manager->getSystemOverwrite();
        $defaultApps = $this->manager->applyOverwrites($apps, $sysOverwrite);
        $userOverwrite = $this->manager->getUserOverwrite($user);
        $this->initialState->provideInitialState("settings", [
            "isAdminPanel" => false,
            "apps" => $this->manager->applyOverwrites($defaultApps, $userOverwrite),
            "defaultApps" => $defaultApps,
        ]);

        Util::addScript(Application::APP_ID, "customnav-admin");

        return new TemplateResponse(Application::APP_ID, "main");
    }

    public function getSection(): string | null
    {
        if ($this->manager->isDisableUserEdit()) {
            return null;
        }
        return Application::APP_ID;
    }

    public function getPriority(): int
    {
        return 70;
    }
}
