<?php
namespace ONLINE_ADAv4;

use ONLINE_ADAv4\Plugin;
use ONLINE_ADAv4\Helper;

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

/**
 * Include JS and CSS in the dashboard
 */
 function enqueue_admin_scripts(){

	$current_screen = get_current_screen();

    wp_register_script("ada_plugin_script", Plugin::$instance->info["url"] . '/admin/assets/js/admin.js', array("jquery"), Plugin::$instance->info["version"],['in_footer' => false] );
 	wp_localize_script(
		 "ada_plugin_script",
		 "ada_plugin",
		 array(
			"ajaxurl" => admin_url( "admin-ajax.php" ),
			"home_url" => home_url(),
			"site_url" => site_url(),
			"current_screen" => get_current_screen(),
			"license" => trim(get_option("accessibility-suite-pro-license")),
			"env" => Helper::get_env_vars(),
			"security" => wp_create_nonce('oadaas_security_nonce'),
			"chunkSize" => Plugin::$instance->info["chunkSize"],
			"dbRowLimit" => Plugin::$instance->info["db_row_limit"],
			"pagenow" => $current_screen->id
		)
	);
	wp_enqueue_script( 'ada_plugin_script' );
	wp_enqueue_style('ada_plugin', Plugin::$instance->info["url"] . '/admin/assets/css/admin.css', array(), Plugin::$instance->info["version"]);

	$assetExt = ACCESSSUITEMODE == 'dev' ? '' : '.min';
	$current_screen = get_current_screen();
	
	if( $current_screen->id == "wcag_scan_page_wcag-guidelines" ){
		wp_register_script("ada-plugin-admin-js-fontawesome5", 'https://kit.fontawesome.com/88badb7223.js', array("jquery"), Plugin::$instance->info["version"],['in_footer' => false]);
		wp_enqueue_script( "ada-plugin-admin-js-fontawesome5" );

		wp_register_script( "guidelines-script", Plugin::$instance->info["url"] . '/admin/assets/js/guidelines.js', ["jquery"], Plugin::$instance->info["version"],['in_footer' => false]);
		wp_enqueue_script( "guidelines-script" );
	}

	if($current_screen->id == "wcag_scan_page_license"){
		wp_register_script( 'ada-plugin-admin-js_license', Plugin::$instance->info["url"] . '/dist/license'.$assetExt.'.js',['jquery'], Plugin::$instance->info["version"],['in_footer' => false] );
		wp_localize_script( 'ada-plugin-admin-js_license', 'data_license', [
			'nonce' => wp_create_nonce( 'wp_rest' ),
			'root' => esc_url_raw( rest_url() ),
		] );
		wp_register_script("ada-plugin-admin-js-fontawesome5", 'https://kit.fontawesome.com/88badb7223.js', array("jquery"), Plugin::$instance->info["version"],['in_footer' => false]);
		wp_enqueue_script( "ada-plugin-admin-js-fontawesome5" );
		wp_enqueue_script( 'ada-plugin-admin-js_license' );

        // the vue code needs type=module.
//      thx to //https://micksp.medium.com/integrating-vue-in-a-wordpress-plugin-135f875c9913 for this snippet
        add_filter('script_loader_tag', function($tag, $handle, $src) {
            if ( 'ada-plugin-admin-js_license' === $handle ) {
                // change the script tag by adding type="module" and return it.
                $tag = '<script type="module" src="' . esc_url( $src ) . '"></script>';
                return $tag;
            }

            return $tag;
        } , 10, 3);
	}
	
	$post = isset($_GET["post"]) ? sanitize_text_field($_GET["post"]) : "";
	if( $post && $current_screen->id == "wcag_scan"){
		wp_enqueue_style('grid', Plugin::$instance->info["url"] . '/admin/assets/css/grid.css', array(), Plugin::$instance->info["version"]);		
		$scan = new WCAG_Scan($post);
		if($scan->status == "complete"){
			wp_register_script("ada_plugin_scan_reports_script", Plugin::$instance->info["url"] . '/admin/assets/js/scan-reports-scripts-bundle.js', array("jquery"), Plugin::$instance->info["version"], ['in_footer' => true]);
			
			$false_positives = Helper::get_false_positives($scan->ID);
			
			if( !isset($false_positives) ){
				global $wpdb;
                $table_name = $wpdb->prefix.'oada_false_positives';
				$arr = $wpdb->_real_escape(json_encode([]));
                $wpdb->query($wpdb->prepare("INSERT INTO $table_name (scan_id, list) VALUES ($scan->ID, '$arr')"));  // phpcs:ignore
				$false_positives = [];
			}

			wp_localize_script("ada_plugin_scan_reports_script", "oada_php_vars", [
				"scan_title" => get_post($_GET["post"])->post_title,
				"false_positives" => $false_positives,
				"ajaxurl" => admin_url("admin-ajax.php"),
				"scan_id" => $scan->ID
			]);
			wp_enqueue_script('ada_plugin_scan_reports_script');

			wp_enqueue_style('ada_plugin_scan_reports_styles', Plugin::$instance->info["url"] . '/admin/assets/css/scan-reports-styles.css', array(), Plugin::$instance->info["version"]);

			wp_enqueue_style("elem-widget-scan-reports-fontawesome-css", "https://use.fontawesome.com/releases/v5.1.0/css/all.css", "1.0", true);

			wp_enqueue_style("ada_plugin_pagination_styles", Plugin::$instance->info["url"] . '/admin/assets/css/simplePagination.css', array(), Plugin::$instance->info["version"] );

			wp_enqueue_style("vuesax_styles", "https://cdn.jsdelivr.net/npm/vuesax/dist/vuesax.css", array(), Plugin::$instance->info["version"] );
		}
	}
 }
 
 add_action( 'admin_enqueue_scripts', __NAMESPACE__ . '\\enqueue_admin_scripts', 99999999 );

/**
 * Allow accessing the site with CSS disabled by adding ?nocss=1 to the URL
 */
function dequeue_stylesheets_nocss() {
	$nocss = isset($_GET['nocss']) ? sanitize_text_field($_GET['nocss']) : "";
	if ( $nocss && $nocss === "1" ) {

		global $wp_styles;

		if ( empty($wp_styles) ) return;

		if ( !empty($wp_styles->registered) ) foreach( $wp_styles->registered as $style ) {
			// Allow wp-admin styles
			wp_dequeue_style( $style->handle );
		}
	}
}
add_action( 'wp_enqueue_scripts', __NAMESPACE__. '\\dequeue_stylesheets_nocss', 9999999999999 );

if(Helper::is_pro()){
	function enqueue_admin_scripts_pro($handle)
	{
		if (get_current_screen()->id == "wcag_scan_page_sitemap") {
			wp_register_script("oadaas_sitemap_script", Plugin::$instance->info["url"] . '/admin/assets/js/sitemap-admin.js', array("jquery"), Plugin::$instance->info["version"], ['in_footer' => true]);
			wp_enqueue_script("oadaas_sitemap_script");

			wp_enqueue_style("oadaas_sitemap_style", Plugin::$instance->info["url"] . '/admin/assets/css/sitemap-admin.css', array(), Plugin::$instance->info["version"]);
		}

		$page_set = isset($_GET["page"]);
		
		if( $page_set && $_GET["page"] === "fix_images" ){
			wp_register_script("fix_images_script", Plugin::$instance->info["url"] . '/admin/assets/js/fix-images-bundle.js', array("jquery"), Plugin::$instance->info["version"], true);
			wp_enqueue_script('fix_images_script');
		}
	}
	add_action( 'admin_enqueue_scripts', __NAMESPACE__ . '\\enqueue_admin_scripts_pro' );


	add_action('admin_enqueue_scripts', __NAMESPACE__ . '\\add_material_icons', 10);

	function add_material_icons(){
		$current_screen = get_current_screen();
		
		if($current_screen->id == "wcag_scan_page_fix_images"){
			wp_enqueue_style('material', 'https://fonts.googleapis.com/css?family=Roboto:100,300,400,500,700,900|Material+Icons', array(), Plugin::$instance->info["version"]);
			wp_enqueue_style('fix-images', Plugin::$instance->info["url"] . '/admin/assets/css/fix-images.css', array(), Plugin::$instance->info["version"]);
			wp_enqueue_style('font-awesome-solid', 'https://use.fontawesome.com/releases/v5.8.1/css/solid.css', array(), Plugin::$instance->info["version"]);
			wp_enqueue_style('font-awesome', 'https://use.fontawesome.com/releases/v5.8.1/css/fontawesome.css', array(), Plugin::$instance->info["version"]);
		}
	}

	add_action('wp_enqueue_scripts', __NAMESPACE__ . '\\frontend_scripts');
	function frontend_scripts(){
		wp_register_script('oadaas_images', Plugin::$instance->info["url"] . '/admin/assets/js/image-replacement-script.js', array('jquery'), Plugin::$instance->info["version"], true);
		wp_enqueue_script('oadaas_images');
	}
}

