<?php

namespace PhotoSphereViewer\Includes;


/**
 * Plugin admin functions.
 *
 * @package photo-sphere-viewer
 */

if (! defined('ABSPATH')) {
    exit;
}

/**
 * Ultimate Cursor Admin class.
 */
class Admin {
    /**
     * photo_sphere_viewer_Admin constructor.
     */
    public function __construct() {
        add_action('admin_init', [$this, 'redirect_to_welcome_screen']);
        add_action('admin_menu', [$this, 'register_admin_menu'], 20);
        add_action('in_admin_header', [$this, 'disable_admin_notices'], PHP_INT_MAX);

        add_filter('admin_body_class', [$this, 'admin_body_class']);
        // Enqueue admin scripts
        add_action('admin_enqueue_scripts', [$this, 'enqueue_scripts']);

        add_filter('plugin_action_links_photo-sphere-viewer/photo-sphere-viewer.php', [$this, 'photo_sphere_viewer_settings_link']);
    }


    public function disable_admin_notices() {
        // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Simple page check, not processing form data
        if (isset($_GET['page']) && sanitize_text_field(wp_unslash($_GET['page'])) === 'photo-sphere-viewer') {
            remove_all_actions('admin_notices');
            remove_all_actions('all_admin_notices');
            remove_all_actions('network_admin_notices');
        }
    }
    public function photo_sphere_viewer_settings_link($links) {
        $settings_link = '<a href="' . admin_url('admin.php?page=photo-sphere-viewer') . '">Settings</a>';
        array_unshift($links, $settings_link);
        return $links;
    }

    /**
     * Enqueue admin scripts and styles
     *
     * @param string $hook_suffix The current admin page.
     */
    public function enqueue_scripts($hook_suffix) {
        // Only load admin scripts on our admin page
        if ('toplevel_page_photo-sphere-viewer' !== $hook_suffix) {
            return;
        }

        // Enqueue WordPress media uploader
        wp_enqueue_media();

        $admin_assets = PSV_PATH . 'build/admin/index.asset.php';
        if (file_exists($admin_assets)) {
            $admin_assets = require $admin_assets;

            wp_enqueue_script(
                'photo-sphere-viewer-admin',
                PSV_URL . 'build/admin/index.js',
                $admin_assets['dependencies'],
                $admin_assets['version'],
                true
            );

            wp_set_script_translations('photo-sphere-viewer-admin', 'photo-sphere-viewer', PSV_PATH . 'languages/');

            wp_enqueue_style(
                'photo-sphere-viewer-admin',
                PSV_URL . 'build/admin/style-index.css',
                ['wp-components'],
                $admin_assets['version']
            );

            $is_pro_active = defined('PHOTO_SPHERE_VIEWER_PRO_VERSION') || class_exists('PhotoSphereViewerPro\\Plugin');

            wp_localize_script('photo-sphere-viewer-admin', 'psvAdminData', array(
                'ajaxUrl' => admin_url('admin-ajax.php'),
                'nonce' => wp_create_nonce('psv_admin_nonce'),
                'version' => PSV_VER,
                'isPro' => $is_pro_active,
                'adminUrl' => admin_url(),
                'upgradeUrl' => 'https://wpxero.com/plugins/photo-sphere-viewer/pricing/',
            ));
        }
    }


    /**
     * Redirect to Welcome page after activation.
     */
    public function redirect_to_welcome_screen() {
        // Bail if no activation redirect.
        if (! get_transient('_photo_sphere_viewer_welcome_screen_activation_redirect')) {
            return;
        }

        // Delete the redirect transient.
        delete_transient('_photo_sphere_viewer_welcome_screen_activation_redirect');

        // Bail if activating from network, or bulk.
        // phpcs:ignore WordPress.Security.NonceVerification.Recommended
        if (is_network_admin() || isset($_GET['activate-multi'])) {
            return;
        }

        // Redirect to welcome page.
        wp_safe_redirect(admin_url('admin.php?page=photo-sphere-viewer&sub_page=settings'));
    }

    /**
     * Register admin menu.
     *
     * Add new Photo Sphere Viewer Settings admin menu.
     */
    public function register_admin_menu() {
        if (! current_user_can('manage_options')) {
            return;
        }

        add_menu_page(
            esc_html__('Photo Sphere Viewer', 'photo-sphere-viewer'),
            esc_html__('Photo Sphere Viewer', 'photo-sphere-viewer'),
            'manage_options',
            'photo-sphere-viewer',
            [$this, 'print_admin_page'],
            'data:image/svg+xml;base64,' . base64_encode('<svg viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg" id="Panorama--Streamline-Solar" height="24" width="24"><path d="M19.2093 12.8396c0 0.7784 -0.6247 1.4093 -1.3953 1.4093 -0.7707 0 -1.3954 -0.6309 -1.3954 -1.4093 0 -0.7783 0.6247 -1.4092 1.3954 -1.4092 0.7706 0 1.3953 0.6309 1.3953 1.4092Z" fill="#000000" stroke-width="1"/><path fill-rule="evenodd" clip-rule="evenodd" d="M18.4475 3.07312c-1.0594 -0.33163 -2.0289 0.51384 -2.0289 1.54693v3.62564c-1.2969 0.25199 -2.8046 0.39777 -4.4186 0.39777 -1.614 0 -3.12174 -0.14578 -4.4186 -0.39777V4.62005c0 -1.03309 -0.96947 -1.87856 -2.02888 -1.54693 -0.98141 0.30721 -1.83062 0.69715 -2.44969 1.16934C2.49454 4.70643 2 5.33865 2 6.13148V18.0787c0 0.2153 0.03738 0.4209 0.10405 0.6147 0.05983 0.174 0.14324 0.3385 0.2444 0.4922 0.32342 0.4914 0.8407 0.8942 1.44015 1.2234 0.6081 0.3341 1.35043 0.6195 2.18407 0.8524C7.64058 21.7273 9.73668 22 12 22c1.9009 0 3.6816 -0.1924 5.1889 -0.5288 1.4929 -0.3332 2.773 -0.82 3.6299 -1.445 0.2084 -0.152 0.4051 -0.3226 0.5761 -0.5116 0.3596 -0.3975 0.6051 -0.8766 0.6051 -1.4359V6.13148c0 -0.79283 -0.4945 -1.42505 -1.1028 -1.88902 -0.6191 -0.47219 -1.4683 -0.86213 -2.4497 -1.16934Zm2.1572 5.15347c-0.0269 0.01757 -0.054 0.03489 -0.0812 0.05196 -0.8221 0.51655 -1.9514 0.93001 -3.2535 1.22708 -1.5245 0.3478 -3.3351 0.54707 -5.27 0.54707 -1.9349 0 -3.74548 -0.19927 -5.27 -0.54707 -1.3021 -0.29707 -2.43136 -0.71053 -3.25355 -1.22708 -0.02716 -0.01707 -0.05421 -0.03439 -0.0811 -0.05196v9.16261l2.83071 -2.6754 1.27627 -1.0789c0.92762 -0.7842 2.31738 -0.7412 3.19207 0.1039l3.0894 2.9848c0.2555 0.2468 0.6715 0.2851 0.9752 0.0789l0.2147 -0.1458c1.0829 -0.7353 2.5436 -0.6524 3.5321 0.207l1.9011 1.6531c0.1557 -0.1877 0.1978 -0.3373 0.1978 -0.4381V8.22659Z" fill="#000000" stroke-width="1"/></svg>'),
            '58.7'
        );

        add_submenu_page(
            'photo-sphere-viewer',
            '',
            esc_html__('Integration', 'photo-sphere-viewer'),
            'manage_options',
            'photo-sphere-viewer'
        );
    }

    /**
     * Print admin page.
     */
    public function print_admin_page() {
?>
        <div id="photo-sphere-viewer-admin-root"></div>
<?php
    }

    /**
     * Add page class to body.
     *
     * @param string $classes - body classes.
     */
    public function admin_body_class($classes) {
        $screen = get_current_screen();

        if ('toplevel_page_photo-sphere-viewer' !== $screen->id) {
            return $classes;
        }

        $classes .= ' photo-sphere-viewer-admin-page';

        // Sub page.
        $page_name = 'settings';

        // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Simple page check, not processing form data
        if (isset($_GET['sub_page']) && sanitize_text_field(wp_unslash($_GET['sub_page']))) {
            // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Simple page check, not processing form data
            $page_name = esc_attr(sanitize_text_field(wp_unslash($_GET['sub_page'])));
        }

        $classes .= ' photo-sphere-viewer-admin-page-' . $page_name;

        // Is first loading after plugin activation redirect.
        // phpcs:ignore WordPress.Security.NonceVerification
        if (isset($_GET['is_first_loading'])) {
            $classes .= ' photo-sphere-viewer-admin-first-loading';
        }

        return $classes;
    }
}
