<?php

namespace Storeplugin\PDFInvoice;

defined("ABSPATH") || exit();

use Storeplugin\PDFInvoice\App\Fields\FieldRenderer;

class Config
{
    private static ?string $prefix = null;
    private static ?array $pluginData = null;
    private static array $adminPageSlugs = [];

    public static function prefix(): string
    {
        if (self::$prefix === null) {
            self::$prefix = "nxtpdf";
        }

        return self::$prefix;
    }

    public static function name(): string
    {
        return (string) self::pluginData()["TextDomain"];
    }

    public static function title(): string
    {
        return (string) self::pluginData()["Name"];
    }

    public static function version(): string
    {
        return (string) self::pluginData()["Version"];
    }

    public static function appFieldRenderer(): string
    {
        return FieldRenderer::class;
    }

    public static function cacheExpiration(): int
    {
        return MONTH_IN_SECONDS;
    }

    public static function pluginNamespace(string $pluginNamespace = ""): string
    {
        $pluginNamespace = trim($pluginNamespace);

        if ($pluginNamespace !== "") {
            return trim($pluginNamespace, "\\");
        }

        return __NAMESPACE__;
    }

    public static function option(string $name): string
    {
        return self::prefix() . "_" . $name;
    }

    public static function nonce(string $name): string
    {
        return self::prefix() . "_" . $name . "_nonce";
    }

    public static function asset(string $name): string
    {
        return self::prefix() . "-" . $name;
    }

    public static function ajax(string $name): string
    {
        return self::prefix() . "_" . $name;
    }

    public static function isSecurityLogDisabled(): bool
    {
        return defined("NXTPDF_DISABLE_SECURITY_LOG") &&
            NXTPDF_DISABLE_SECURITY_LOG;
    }

    public static function isCspDisabled(): bool
    {
        return defined("NXTPDF_DISABLE_CSP") && NXTPDF_DISABLE_CSP;
    }

    public static function setPrefix(string $prefix): void
    {
        self::$prefix = $prefix;
    }

    /**
     * Register an admin page slug owned by the plugin.
     *
     * @param string $slug
     * @return void
     */
    public static function registerAdminPageSlug(string $slug): void
    {
        $slug = \sanitize_key($slug);
        if ($slug === "") {
            return;
        }

        if (!in_array($slug, self::$adminPageSlugs, true)) {
            self::$adminPageSlugs[] = $slug;
        }
    }

    /**
     * Check whether the plugin owns the given admin page slug.
     *
     * @param string $slug
     * @return bool
     */
    public static function isRegisteredAdminPageSlug(string $slug): bool
    {
        $slug = \sanitize_key($slug);
        return $slug !== "" && in_array($slug, self::$adminPageSlugs, true);
    }

    /**
     * Get all registered plugin admin page slugs for this request.
     *
     * @return array<int, string>
     */
    public static function getRegisteredAdminPageSlugs(): array
    {
        return array_values(self::$adminPageSlugs);
    }

    /**
     * Determine whether the current request targets a plugin-owned admin page.
     *
     * @return bool
     */
    public static function isCurrentPluginAdminPage(): bool
    {
        $pluginPage =
            isset($GLOBALS["plugin_page"]) && is_string($GLOBALS["plugin_page"])
                ? \sanitize_key($GLOBALS["plugin_page"])
                : "";
        if (self::matchesPluginAdminSlug($pluginPage)) {
            return true;
        }

        $pageValue = \filter_input(INPUT_GET, "page", FILTER_UNSAFE_RAW, FILTER_REQUIRE_SCALAR);
        $page = is_string($pageValue) ? \sanitize_key(\wp_unslash($pageValue)) : "";
        return self::matchesPluginAdminSlug($page);
    }

    /**
     * Check whether an admin page slug should be treated as plugin-owned.
     *
     * @param string $slug
     * @return bool
     */
    private static function matchesPluginAdminSlug(string $slug): bool
    {
        return self::isRegisteredAdminPageSlug($slug);
    }

    /**
     * Get plugin header data with in-request caching.
     *
     * @return array<string, mixed>
     */
    private static function pluginData(): array
    {
        if (self::$pluginData === null) {
            if (!function_exists("get_plugin_data")) {
                require_once ABSPATH . "wp-admin/includes/plugin.php";
            }

            self::$pluginData = get_plugin_data(NXTPDF_PATH, false, false);
        }

        return self::$pluginData;
    }
}
