<?php
// Add Getstart admin notice
function vehicle_detailing_admin_notice() { 
    global $pagenow;
    $vehicle_detailing_theme_args      = wp_get_theme();
    $vehicle_detailing_meta            = get_option( 'vehicle_detailing_admin_notice' );
    $vehicle_detailing_name            = $vehicle_detailing_theme_args->__get( 'Name' );
    $vehicle_detailing_current_screen  = get_current_screen();

    if( !$vehicle_detailing_meta ){
        if( is_network_admin() ){
            return;
        }

        if( ! current_user_can( 'manage_options' ) ){
            return;
        } if($vehicle_detailing_current_screen->base != 'appearance_page_vehicle-detailing-guide-page' && $vehicle_detailing_current_screen->id != 'appearance_page_vehicle-detailing-info' && $vehicle_detailing_current_screen->base != 'toplevel_page_cretats-theme-showcase' ) { ?>
        <div class="notice notice-success is-dismissible welcome-notice">
            <div class="notice-row">
                <div class="notice-text">
                    <p class="welcome-text1"><?php esc_html_e( '🎉 Welcome to VW Themes,', 'vehicle-detailing' ); ?></p>
                    <p class="welcome-text2"><?php esc_html_e( 'You are now using the Vehicle Detailing, a beautifully designed theme to kickstart your website.', 'vehicle-detailing' ); ?></p>
                    <p class="welcome-text3"><?php esc_html_e( 'To help you get started quickly, use the options below:', 'vehicle-detailing' ); ?></p>

                    <span class="import-btn">
                        <a href="javascript:void(0);" id="install-activate-button" class="button admin-button info-button">
                           <?php echo __('GET STARTED', 'vehicle-detailing'); ?>
                        </a>
                        <script type="text/javascript">
                            document.getElementById('install-activate-button').addEventListener('click', function () {
                                const vehicle_detailing_button = this;
                                const vehicle_detailing_redirectUrl = '<?php echo esc_url(admin_url("themes.php?page=vehicle-detailing-info")); ?>';
                                // First, check if plugin is already active
                                jQuery.post(ajaxurl, { action: 'check_plugin_activation' }, function (response) {
                                    if (response.success && response.data.active) {
                                        // Plugin already active — just redirect
                                        window.location.href = vehicle_detailing_redirectUrl;
                                    } else {
                                        // Show Installing & Activating only if not already active
                                        vehicle_detailing_button.textContent = 'Installing & Activating...';

                                        jQuery.post(ajaxurl, {
                                            action: 'install_and_activate_required_plugin',
                                            nonce: '<?php echo wp_create_nonce("install_activate_nonce"); ?>'
                                        }, function (response) {
                                             if (response.success) {

                                            vehicle_detailing_button.textContent = 'Importing Demo...';

                                            jQuery.post(ajaxurl, {
                                                action: 'vehicle_detailing_run_demo_import',
                                                nonce: '<?php echo wp_create_nonce("vehicle_detailing_demo_nonce"); ?>'
                                            }, function (importResponse) {

                                                if (importResponse.success) {
                                                    window.location.href = vehicle_detailing_redirectUrl;
                                                } else {
                                                    vehicle_detailing_button.textContent = 'Try Again';
                                                }

                                            });
                                        }
                                        else {
                                                alert('Failed to activate the plugin.');
                                                vehicle_detailing_button.textContent = 'Try Again';
                                            }
                                        });
                                    }
                                });
                            });
                        </script>
                    </span>

                    <span class="demo-btn">
                        <a href="https://www.vwthemes.net/vehicle-detailing-pro/" class="button button-primary" target="_blank">
                            <?php esc_html_e( 'VIEW DEMO', 'vehicle-detailing' ); ?>
                        </a>
                    </span>

                    <span class="upgrade-btn">
                        <a href="https://www.vwthemes.com/products/car-detailing-wordpress-theme" class="button button-primary" target="_blank">
                            <?php esc_html_e( 'UPGRADE TO PRO', 'vehicle-detailing' ); ?>
                        </a>
                    </span>

                    <span class="bundle-btn">
                        <a href="https://www.vwthemes.com/products/wp-theme-bundle" class="button button-primary" target="_blank">
                            <?php esc_html_e( 'BUNDLE OF 400+ THEMES', 'vehicle-detailing' ); ?>
                        </a>
                    </span>
                </div>

                <div class="notice-img1">
                    <img src="<?php echo esc_url( get_template_directory_uri() . '/images/arrow-notice.png' ); ?>" width="180" alt="<?php esc_attr_e( 'Vehicle Detailing', 'vehicle-detailing' ); ?>" />
                </div>

                <div class="notice-img2">
                    <img src="<?php echo esc_url( get_template_directory_uri() . '/images/bundle-notice.png' ); ?>" width="180" alt="<?php esc_attr_e( 'Vehicle Detailing', 'vehicle-detailing' ); ?>" />
                </div>
            </div>
        </div>
        <?php

    }?>
        <?php

    }
}

add_action( 'admin_notices', 'vehicle_detailing_admin_notice' );

if( ! function_exists( 'vehicle_detailing_update_admin_notice' ) ) :
/**
 * Updating admin notice on dismiss
*/
function vehicle_detailing_update_admin_notice(){
    if ( isset( $_GET['vehicle_detailing_admin_notice'] ) && $_GET['vehicle_detailing_admin_notice'] = '1' ) {
        update_option( 'vehicle_detailing_admin_notice', true );
    }
}
endif;
add_action( 'admin_init', 'vehicle_detailing_update_admin_notice' );

//After Switch theme function
add_action('after_switch_theme', 'vehicle_detailing_getstart_setup_options');
function vehicle_detailing_getstart_setup_options () {
    update_option('vehicle_detailing_admin_notice', FALSE );
}