<?php

/*
 * This file is part of the Fancy Bricks package.
 *
 * (c) Joshua Gugun Siagian <suabahasa@gmail.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */
declare (strict_types=1);
namespace Fancy\Animations\Admin;

use Fancy\Animations\Plugin;
use Fancy\Animations\Utils\Asset;
use _FancyAnimations\FANCY_ANIMATIONS;
class AdminPage
{
    public function __construct()
    {
        \add_action('admin_menu', fn() => $this->add_admin_menu(), 1000001);
    }
    public static function get_page_url() : string
    {
        return \add_query_arg(['page' => FANCY_ANIMATIONS::WP_OPTION], \admin_url('admin.php'));
    }
    public function add_admin_menu()
    {
        $hook = \add_menu_page(\__('Fancy Animations', 'fancy-animations'), \__('Fancy Animations', 'fancy-animations'), 'manage_options', FANCY_ANIMATIONS::WP_OPTION, fn() => $this->render(), 'data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iMzEiIGhlaWdodD0iMzEiIHZpZXdCb3g9IjAgMCAzMSAzMSIgZmlsbD0ibm9uZSIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIj4KPHBhdGggZD0iTTI0LjMyNzggMTYuMjI3MkwxOS4xMTM3IDMwLjQ4MzlIMTcuNDAwNEwyMy4yMzg0IDE1LjA4NUgyNC4zOTEyTDI0LjMyNzggMTYuMjI3MlpNMjguODk2NyAzMC40ODM5TDIzLjY3MjEgMTYuMjI3MkwyMy42MTkyIDE1LjA4NUgyNC43NjE0TDMwLjYyMDYgMzAuNDgzOUgyOC44OTY3Wk0yOC4yODMzIDI0LjkyMDhWMjYuMzA2M0gxOS44NjQ2VjI0LjkyMDhIMjguMjgzM1oiIGZpbGw9IndoaXRlIi8+CjxwYXRoIGQ9Ik0xMS4yNzc2IDkuOTM5NDlMOS44NzA2NCAxMy42Nzg4SDQuNzkzOTdWMjMuMjY2N0gwVjBIMTUuMDE3M0wxMy42MDQ0IDMuNzU1MjdINC43OTM5N1Y5LjkzOTQ5SDExLjI3NzZaIiBmaWxsPSIjRkZEMDMzIi8+CjxwYXRoIGZpbGwtcnVsZT0iZXZlbm9kZCIgY2xpcC1ydWxlPSJldmVub2RkIiBkPSJNOS43OTMyMSAzMC41MzI2TDIwLjcwNyAxLjQ1MjM3TDIxLjYwODcgMS40NTk5MkwyMS42MDc5IDEuNDYyM0wxMC42ODYyIDMwLjU2MzZMOS43ODA3MyAzMC41NzEzQzkuNzg0MjQgMzAuNTU4NCA5Ljc4ODQgMzAuNTQ1NCA5Ljc5MzIxIDMwLjUzMjZaIiBmaWxsPSJ3aGl0ZSIvPgo8L3N2Zz4=', 3);
        \add_submenu_page(FANCY_ANIMATIONS::WP_OPTION, \__('Settings', 'fancy-animations'), \__('Settings', 'fancy-animations'), 'manage_options', \sprintf('%s#/settings', FANCY_ANIMATIONS::WP_OPTION), fn() => $this->render());
        \add_action('load-' . $hook, fn() => $this->init_hooks());
    }
    private function render()
    {
        \add_filter('admin_footer_text', static fn($text) => 'Thank you for using <b>Fancy Animations</b>! Join us on the <a href="https://www.facebook.com/groups/fancybricks" target="_blank">Facebook Group</a>.', 1000001);
        \add_filter('update_footer', static fn($text) => $text . ' | Fancy Animations ' . FANCY_ANIMATIONS::VERSION, 1000001);
        echo '<div id="fancy-animations-app" class=""></div>';
    }
    private function init_hooks()
    {
        \add_action('admin_enqueue_scripts', fn() => $this->enqueue_scripts(), 1000001);
    }
    private function enqueue_scripts()
    {
        Asset::enqueue_entry('admin', [], \true);
        \wp_set_script_translations(FANCY_ANIMATIONS::WP_OPTION . ':admin.js', 'fancy-animations');
        \wp_localize_script(FANCY_ANIMATIONS::WP_OPTION . ':admin.js', 'fancyAnimations', ['_version' => FANCY_ANIMATIONS::VERSION, '_wpnonce' => \wp_create_nonce(FANCY_ANIMATIONS::WP_OPTION), 'web_history' => self::get_page_url(), 'rest_api' => ['nonce' => \wp_create_nonce('wp_rest'), 'root' => \esc_url_raw(\rest_url()), 'namespace' => FANCY_ANIMATIONS::REST_NAMESPACE, 'url' => \esc_url_raw(\rest_url(FANCY_ANIMATIONS::REST_NAMESPACE))], 'assets' => ['url' => Asset::asset_base_url()], 'site_meta' => ['name' => \get_bloginfo('name'), 'site_url' => \get_site_url()], 'license' => ['is_activated' => Plugin::get_instance()->maybe_update_plugin()->is_activated()]]);
    }
}
