<?php

/**
 * Admin assets class
 * Registers all admin assets and scripts
 *
 * @since 1.1.0
 * @package RVIOR
 */

// Namespace.
namespace RVIOR\Admin;

// Exit if accessed directly.
defined('ABSPATH') || exit(1);

if ( ! class_exists(__NAMESPACE__ . '\Assets') ) {

    /**
     * Admin Hooks
     *
     * @since 1.1.0
     */
    class Assets extends \RVIOR\Base\Controller {


        // Use utilities trait.
        use \RVIOR\Traits\Utilities;

        /**
         * Registers all admin hooks
         *
         * @return void
         */
        public function register_hooks() {
            add_action('admin_enqueue_scripts', [ $this, 'admin_enqueue_scripts' ]);

            add_action('admin_footer', [ $this, 'admin_footer' ]);
        }

        /**
         * Enqueues admin scripts
         *
         * @param string $hook Hook.
         * @return void
         */
        public function admin_enqueue_scripts( $hook ) {
            $current_screen  = get_current_screen();
            $is_block_editor = method_exists($current_screen, 'is_block_editor') && $current_screen->is_block_editor();

            $current_page = '';

            try {
                $current_page = explode('-', $hook);
                $current_page = end($current_page);
            } catch ( \Exception $e ) {
                $current_page = '';
            }

            $all_pages = array_map(
                function ( $page ) {
                    // Return ID as value, post_title as label and post_name as slug.
                    return [
                        'value' => $page->ID,
                        'label' => $page->post_title,
                        'slug'  => $page->post_name,
                    ];
                },
                get_pages()
            );

            $translation         = new \RVIOR\Translation();
            $admin_footer_script = [
                'ajax_url'                => admin_url('admin-ajax.php'),
                'server_url'              => $this->client()->get_server(),
                'plugin_url'              => RVIOR_URL,
                'home_url'                => home_url(),
                'host'                    => $this->client()->get_host(),
                'nonce'                   => wp_create_nonce('rvior_admin_nonce'),
                'api_nonce'               => wp_create_nonce('wp_rest'),
                'base_endpoint'           => esc_url_raw(rest_url('rvior/v1/')),
                'current_page'            => $current_page,
                'folders'                 => $this->client()->get_access_token() ? $this->client()->folders() : [],
                'is_user_logogin'         => $this->option()->get('is_user_logedin', false),
                'google_client_url'       => '',
                'pages'                   => $all_pages,
                'galleries'               => $this->option()->get('gallaries', false),
                'create_gallery_page_url' => get_site_url() . '/wp-admin/admin.php?page=rvior-gallery',
                'extensions'              => apply_filters('rvior_extensions', []),
                'is_debug'                => defined('WP_DEBUG') && WP_DEBUG,
                'preference'              => $this->option()->get_all(),
                'translations'            => $translation->get_all(),
            ];
            // Conditional elements.
            if ( $this->client()->has_valid_token() ) {
                $admin_footer_script['access_token']     = $this->client()->get_access_token();
                $admin_footer_script['is_wc_installed']  = $this->is_wc_installed();
                $admin_footer_script['is_wc_active']     = $this->is_wc_active();
                $admin_footer_script['is_edd_installed'] = $this->is_edd_installed();
                $admin_footer_script['is_edd_active']    = $this->is_edd_active();
            } else {
                $admin_footer_script['prefill'] = [
                    'email'      => get_option('admin_email', ''),
                    'first_name' => get_user_meta(get_current_user_id(), 'first_name', true),
                    'last_name'  => get_user_meta(get_current_user_id(), 'last_name', true),
                ];
            }

            $admin_footer_script = apply_filters('rvior_admin_localize_script', $admin_footer_script);

            wp_register_script('_rvior_inline', '', [], RVIOR_VERSION, true);
            wp_localize_script('_rvior_inline', '_rvior_admin', $admin_footer_script);
            wp_enqueue_script('_rvior_inline');

            wp_enqueue_script('sizzle', RVIOR_PUBLIC . 'js/sizzle.min.js', [ 'jquery' ], RVIOR_VERSION, true);
            wp_enqueue_script('rvior-events', RVIOR_PUBLIC . 'js/events.min.js', [ 'jquery' ], RVIOR_VERSION, true);
            wp_enqueue_style('rvior-notice', RVIOR_PUBLIC . 'css/notice.css', [], RVIOR_VERSION);

            if ( $is_block_editor || in_array($hook, [
                'toplevel_page_revio',
                'revio_page_rvior-review-gallery',
                'revio_page_rvior-review-integrations',
                'revio_page_rvior-review-settings',
                'revio_page_rvior-review-profile',
                'revio_page_rvior-review-gallery',
                'revio_page_rvior-review-utility',
                'revio_page_rvior-review-documentation',
            ]) ) {

                wp_enqueue_style('rvior-admin', RVIOR_PUBLIC . 'css/admin.min.css', [], RVIOR_VERSION);
                wp_enqueue_script('rvior-admin', RVIOR_PUBLIC . 'js/admin.min.js', [ 'jquery' ], RVIOR_VERSION, true);
                wp_enqueue_editor();
            }

            // Create a properly escaped URL for the image
            $rvior_image_url = esc_url(RVIOR_PUBLIC . 'images/revio.png');

            // Now build the CSS with the escaped URL
            $custom_styles = '
            [href="admin.php?page=revio"] .dashicons-before:before {
                content: "" !important;
                background: url("' . $rvior_image_url . '") no-repeat !important;
                background-size: 21px 21px !important;
                background-position: center center !important;
                width: 36px !important;
                height: 31px !important;
                padding: 8px 0 0 0 !important;
                margin: auto !important;
            }
            .wp-has-current-submenu [href="admin.php?page=revio"] .dashicons-before:before {
                content: "" !important;
                background: url("' . $rvior_image_url . '") no-repeat !important;
                background-size: 21px 21px !important;
                background-position: center center !important;
                width: 36px !important;
                height: 36px !important;
                padding: 8px 0 0 0 !important;
                margin: auto !important;
            }
            .rvior-upgrade-plan{
                display: flex;
                align-items: center;
            }
            .rvior-upgrade-icon{
                margin-right: 5px;
            }
            .rvior-upgrade-icon-text{
                font-family: Inter;
                font-size: 14px;
                font-weight: 600;
                line-height: 20px;
                text-align: left;
                text-underline-position: from-font;
                text-decoration-skip-ink: none;
            }
            ';

            wp_enqueue_style('rvior-admin-custom', RVIOR_PUBLIC . 'css/blank.css', [], RVIOR_VERSION);
            wp_add_inline_style('rvior-admin-custom', $custom_styles);
        }

        /**
         * Returns the allowed HTML tags for kses.
         *
         * @param array  $allowed Allowed HTML tags and attributes.
         * @param string $context Context to judge allowed tags by. Allowed values are 'post'.
         * @return array
         */
        public function kses_filter_allowed_html( $allowed, $context ) {
            if ( is_array($context) ) {
                return $allowed;
            }

            if ( 'post' === $context ) {
                $allowed['a']['data-*']     = true;
                $allowed['table']['data-*'] = true;
            }

            return $allowed;
        }

        /**
         * Add admin footer
         *
         * @return void
         */
        public function admin_footer() {
            $this->render_template('admin/other/admin-footer');
        }
    }

    // Instantiate.
    Assets::init();
}
