<?php

namespace WPKIN_SHOP_MANAGER\Cleanup;

if ( ! defined( 'ABSPATH' ) ) {
	wp_die( esc_html__( 'You can\'t access this page', 'shop-manager' ) );
}

class Scripts {

	private static $instance = null;
    private static $checked = false;

    public static function get_instance() {
        if ( null === self::$instance ) {
            self::$instance = new self();
        }
        return self::$instance;
    }

    private function __construct() {
        add_action( 'template_redirect', [ $this, 'wpkin_check_frontend_page' ], 999 );
    }

    public function wpkin_check_frontend_page() {
        if ( self::$checked ) {
            return;
        }
        self::$checked = true;

        $request_uri = '';
        if ( isset( $_SERVER['REQUEST_URI'] ) ) {
            $request_uri = sanitize_text_field( wp_unslash( $_SERVER['REQUEST_URI'] ) );
        }

        if ( empty( $request_uri ) || false !== strpos( $request_uri, 'favicon.ico' ) ) {
            return;
        }

        if ( is_admin() || wp_doing_ajax() || ! is_main_query() ) {
            return;
        }

        if ( is_page( 'sm-dashboard' ) ) {
            add_action( 'wp_print_styles', [ $this, 'wpkin_clear_styles_and_scripts' ], 100 );
            add_action( 'wp_head', [ $this, 'wpkin_cleanup_head' ], 1 );
        }
    }

    public function wpkin_clear_styles_and_scripts() {
        global $wp_styles, $wp_scripts;

        $styles_to_keep = [
            // Plugin styles
            'wpkin-sm-public-style',

            // WordPress editor styles
            'wp-editor',
            'wp-editor-font',
            'editor-buttons',
            'dashicons',
            'editor-style',
            'tinymce',
            'tinymce-5',

            // Media library styles
            'wp-mediaelement',
            'mediaelement',
            'wp-mediaelement-legacy',
            'media-views',
            'imgareaselect',
            'thickbox',
            'wp-jquery-ui-dialog',
            'buttons',

            // WordPress core styles
            'wp-block-library',
            'wp-components',
            'wp-format-library',
            'wp-edit-blocks',
            'wp-block-editor',
            'wp-nux',
            'wp-reusable-blocks',
            'wp-editor-classic-layout-styles',

            // jQuery UI styles
            'jquery-ui-core',
            'jquery-ui-theme',
            'jquery-ui-dialog',
            'jquery-ui-resizable',
            'jquery-ui-draggable',
            'jquery-ui-button',

            // Other potentially needed styles
            'common',
            'forms',
            'admin-menu',
            'admin-bar',
            'colors',
            'wp-auth-check',
            'wp-pointer',
        ];

        // Comprehensive list of scripts needed for media library and editor
        $scripts_to_keep = [
            // Plugin scripts
            'wpkin-sm-public-scripts',

            // jQuery and UI
            'jquery',
            'jquery-core',
            'jquery-migrate',
            'jquery-ui-core',
            'jquery-ui-widget',
            'jquery-ui-mouse',
            'jquery-ui-draggable',
            'jquery-ui-droppable',
            'jquery-ui-resizable',
            'jquery-ui-button',
            'jquery-ui-position',
            'jquery-ui-dialog',
            'jquery-ui-sortable',
            'jquery-effects-core',
            'jquery-effects-blind',
            'jquery-effects-bounce',
            'jquery-effects-clip',
            'jquery-effects-drop',
            'jquery-effects-explode',
            'jquery-effects-fade',
            'jquery-effects-fold',
            'jquery-effects-highlight',
            'jquery-effects-puff',
            'jquery-effects-pulsate',
            'jquery-effects-scale',
            'jquery-effects-shake',
            'jquery-effects-size',
            'jquery-effects-slide',
            'jquery-effects-transfer',

            // WordPress API
            'wp-api',
            'wp-api-request',

            // WordPress core
            'utils',
            'common',
            'wp-dom-ready',
            'wp-hooks',
            'wp-i18n',
            'wp-polyfill',
            'wp-a11y',

            // Editor scripts
            'wp-tinymce',
            'tinymce-latest',
            'tinymce-latest-lists',
            'tinymce-latest-paste',
            'tinymce-latest-table',
            'editor',
            'editor-functions',
            'quicktags',
            'wplink',
            'wp-embed',

            // Media scripts
            'wp-util',
            'backbone',
            'underscore',
            'media-upload',
            'media-models',
            'media-views',
            'media-editor',
            'media-audiovideo',
            'mce-view',
            'image-edit',
            'thickbox',
            'wp-mediaelement',
            'mediaelement-core',
            'mediaelement-migrate',
            'mediaelement-vimeo',
            'wp-mediaelement',
            'wp-playlist',
            'plupload',
            'plupload-handlers',
            'wp-plupload',
            'swfobject',
            'moxiejs',
            'shortcode',

            // Other potentially needed scripts
            'clipboard',
            'wp-auth-check',
            'wp-pointer',
            'heartbeat',
            'wp-codemirror',
            'csslint',
            'jshint',
            'esprima',
            'jsonlint',
            'htmlhint',
            'wp-theme-plugin-editor',
            'wp-custom-header',
            'wp-ajax-response',
            'wp-lists',
            'cropper',
            'json2',
            'jquery-hotkeys',
            'suggest',
            'imagesloaded',
            'masonry',
            'jquery-masonry',
            'user-profile',
            'language-chooser',
            'user-suggest',
            'admin-bar',
            'wplink',
            'wpdialogs',
            'word-count',
            'wp-sanitize',
            'wp-element',
            'wp-components',
            'wp-block-editor',
            'wp-block-library',
            'wp-blocks',
            'wp-data',
            'wp-core-data',
            'wp-date',
            'wp-compose',
            'wp-deprecated',
            'wp-dom',
            'wp-editor',
            'wp-format-library',
            'wp-keycodes',
            'wp-keyboard-shortcuts',
            'wp-media-utils',
            'wp-notices',
            'wp-plugins',
            'wp-preferences',
            'wp-primitives',
            'wp-priority-queue',
            'wp-redux-routine',
            'wp-reusable-blocks',
            'wp-rich-text',
            'wp-server-side-render',
            'wp-shortcode',
            'wp-token-list',
            'wp-url',
            'wp-viewport',
            'wp-wordcount',
        ];

        foreach ( $wp_styles->queue as $handle ) {
            if ( ! in_array( $handle, $styles_to_keep, true ) ) {
                wp_dequeue_style( $handle );
                wp_deregister_style( $handle );
            }
        }

        foreach ( $wp_scripts->queue as $handle ) {
            if ( ! in_array( $handle, $scripts_to_keep, true ) ) {
                wp_dequeue_script( $handle );
                wp_deregister_script( $handle );
            }
        }
    }

    public function wpkin_cleanup_head() {
        remove_action( 'wp_head', 'print_emoji_detection_script', 7 );
        remove_action( 'wp_head', 'wp_generator' );
        remove_action( 'wp_head', 'wlwmanifest_link' );
        remove_action( 'wp_head', 'rsd_link' );
        remove_action( 'wp_head', 'wp_shortlink_wp_head' );
        remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10 );
        remove_action( 'wp_head', 'wp_resource_hints', 2 );

        remove_action( 'wp_print_styles', 'print_emoji_styles' );

        remove_action( 'wp_head', 'rest_output_link_wp_head' );
        remove_action( 'wp_head', 'wp_oembed_add_discovery_links' );
        remove_action( 'wp_head', 'wp_oembed_add_host_js' );
    }
}
