<?php
/**
 * Adventure Block functions and definitions
 *
 * @link https://developer.wordpress.org/themes/basics/theme-functions/
 *
 * @package adventure-block
 * @since adventure-block 1.0
 */

function adventure_block_scripts() {

	wp_enqueue_style( 'adventure-block-style-parent', get_template_directory_uri() . '/style.css' );

	wp_enqueue_style( 'adventure-block-basic-style', get_stylesheet_uri() );
}
add_action( 'wp_enqueue_scripts', 'adventure_block_scripts' );

function adventure_block_editor_assets() {
    add_editor_style( get_stylesheet_directory_uri() . '/css/editor-style.css' );
}
add_action( 'admin_init', 'adventure_block_editor_assets' );

add_action( 'init', 'adventure_block_remove_action');
function adventure_block_remove_action() {
    remove_action( 'admin_menu','radiant_adventure_tour_admin_menu_page' );
    remove_action( 'admin_notices','radiant_adventure_tour_admin_notice' );
}

add_action( 'init', 'adventure_block_remove_customizer_action');
function adventure_block_remove_customizer_action() {
    remove_action( 'customize_register','radiant_adventure_tour_customize_register' );
}

/* Enqueue admin-notice-script js */
add_action('admin_enqueue_scripts', function ($hook) {
    if ($hook !== 'appearance_page_adventure-block') return;

    wp_enqueue_script('admin-notice-script', get_template_directory_uri() . '/get-started/js/admin-notice-script.js', ['jquery'], null, true);
    wp_localize_script('admin-notice-script', 'installPluginData', [
        'ajaxurl'     => admin_url('admin-ajax.php'),
        'nonce'       => wp_create_nonce('install_activate_nonce'),
        'redirectUrl' => admin_url('themes.php?page=adventure-block'),
    ]);
});

add_action('wp_ajax_check_plugin_activation', function () {
    if (!isset($_POST['plugin']) || empty($_POST['plugin'])) {
        wp_send_json_error(['message' => 'Missing plugin identifier']);
    }

    include_once ABSPATH . 'wp-admin/includes/plugin.php';

    // Map plugin identifiers to their main files
    $adventure_block_plugin_map = [
        'woocommerce'          => 'woocommerce/woocommerce.php',
        'wordclever_ai_content_writer'    => 'wordclever-ai-content-writer/wordclever.php',
        'wp_travel'    => 'wp-travel.php',
        'wp_travel_blocks'    => 'wp-travel-blocks.php',
    ];

    $adventure_block_requested_plugin = sanitize_text_field($_POST['plugin']);

    if (!isset($adventure_block_plugin_map[$adventure_block_requested_plugin])) {
        wp_send_json_error(['message' => 'Invalid plugin']);
    }

    $adventure_block_plugin_file = $adventure_block_plugin_map[$adventure_block_requested_plugin];
    $adventure_block_is_active   = is_plugin_active($adventure_block_plugin_file);

    wp_send_json_success(['active' => $adventure_block_is_active]);
});

function adventure_block_theme_setting() {

    // Get Started
    require get_theme_file_path() . '/get-started/getstart.php';

    // Get Notice
    require get_theme_file_path() . '/get-started/notice.php';

    // Demo Import
    require get_theme_file_path() . '/get-started/demo-import.php';

    // Get Notice
    require get_theme_file_path() . '/inc/customizer.php';

    // TGM
    require get_theme_file_path() . '/inc/tgm/plugin-activation.php';

}
add_action('after_setup_theme', 'adventure_block_theme_setting');

add_action( 'admin_init', function() {

    delete_transient( '_wp_travel_activation_redirect' );

}, 1 );