<?php
/**
 * Getting Started
 *
 */

if( ! function_exists( 'flex_multi_business_getting_started_menu' ) ) :
/**
 * Adding Getting Started Page in admin menu
 */
function flex_multi_business_getting_started_menu(){

	add_theme_page(
		__( 'Get Started', 'flex-multi-business' ),
		__( 'Getting Started', 'flex-multi-business' ),
		'manage_options',
		'flex-multi-business-getting-started',
		'flex_multi_business_getting_started_page'
	);
}
endif;
add_action( 'admin_menu', 'flex_multi_business_getting_started_menu' );

if( ! function_exists( 'flex_multi_business_getting_started_admin_scripts' ) ) :
/**
 * Load Getting Started styles in the admin
 */
function flex_multi_business_getting_started_admin_scripts(){
    wp_enqueue_style( 'flex-multi-business-getting-started', get_template_directory_uri() . '/inc/dashboard/css/getting-started.css', false);
    wp_enqueue_script( 'updates' );
    // Admin notice code START
	wp_register_script('flex-multi-business-notice', esc_url(get_template_directory_uri()) . '/inc/dashboard/js/notice.js', array('jquery'), time(), true);
	wp_enqueue_script('flex-multi-business-notice');
	// Admin notice code END
}
endif;
add_action( 'admin_enqueue_scripts', 'flex_multi_business_getting_started_admin_scripts' );

if( ! function_exists( 'flex_multi_business_getting_started_page' ) ) :
/**
 * Callback function for admin page.
*/
function flex_multi_business_getting_started_page(){ ?>
	<div class="wrap getting-started">
		<h2 class="notices"></h2>
		<div class="flex-dashboard-container">
			<div class="inner-tab-box">
				<?php require get_template_directory() . '/inc/dashboard/tabs/start-dashboard.php'; ?>
			</div>
		</div><!-- flex-container -->
	</div><!-- .getting-started -->
	<?php
}
endif;

/**
 * Banner Notice
 */
function flex_multi_business_admin_notice_activation() {

    // Don't show if dismissed
    if ( get_option( 'flex_multi_business_admin_notice', false ) ) {
        return;
    }

    $flex_multi_business_screen  = get_current_screen();
    global $pagenow;

    if (
        $pagenow === 'themes.php' &&
        isset($_GET['page']) &&
        $_GET['page'] === 'flex-multi-business-getting-started'
    ) {
        return;
    }

    if ( ! $flex_multi_business_screen ) {
        return;
    }
    ?> 
		<div id="flex-multi-business-welcome-notice" class="notice is-dismissible notice-getstarted-flex" data-notice="get-start" >
			<div class="inner-notice">
				<img src="<?php echo esc_url(get_template_directory_uri() . '/inc/dashboard/images/responsive-img.png'); ?>" />
				<div class="inner-content">
					<h2><?php esc_html_e('🎉 Thank you for activating the ', 'flex-multi-business'); ?><span><?php echo esc_html( wp_get_theme()->get( 'Name' ) ); ?></span></h2>
					<p><?php esc_html_e('Get started quickly by importing the demo content or explore more powerful options below.', 'flex-multi-business'); ?></p>
                <?php $flex_template_is_imported = get_option('flex_template_is_imported'); ?>
                <div class="notice-button">
                    <?php if ( !$flex_template_is_imported ) { ?>
                        <a href="javascript:void(0);" 
                        id="install-activate-getstarted" 
                        class="button admin-button info-button">
                            <?php esc_html_e('Import Demo', 'flex-multi-business'); ?>
                        </a>
                    <?php } else { ?>
                        <a href="<?php echo esc_url(home_url()); ?>" 
                        target="_blank"
                        id="install-activate-getstarted" 
                        class="button admin-button info-button">
                            <?php esc_html_e('View Site', 'flex-multi-business'); ?>
                        </a>
                    <?php } ?>
                </div>
						<script type="text/javascript">
						document.getElementById('install-activate-getstarted').addEventListener('click', function () {
							const flex_multi_business_button_banner = this;
							const flex_multi_business_redirectUrl = '<?php echo esc_url(admin_url("admin.php?page=fleximp-template-importer")); ?>';
							// First, check if plugin is already active
							jQuery.post(ajaxurl, { action: 'check_flex_import_activation' }, function (response) {
								if (response.success && response.data.active) {
									// Plugin already active — just redirect
									window.location.href = flex_multi_business_redirectUrl;
								} else {
									// Show Installing & Activating only if not already active
									flex_multi_business_button_banner.textContent = 'Installing & Activating...';

									jQuery.post(ajaxurl, {
										action: 'install_and_activate_flex_import_plugin',
										nonce: '<?php echo wp_create_nonce("install_activate_nonce"); ?>'
									}, function (response) {
										if (response.success) {
											window.location.href = flex_multi_business_redirectUrl;
										} else {
											alert('Failed to activate the plugin.');
											flex_multi_business_button_banner.textContent = 'Try Again';
										}
									});
								}
							});
						});
						</script>

						<a href="<?php echo esc_url( FLEX_MULTI_BUSINESS_BUY_NOW ); ?>" target="_blank" id="go-pro-button" class="button admin-button buy-now-button"><?php echo __('Upgrade Pro', 'flex-multi-business'); ?></a>

						<a href="<?php echo esc_url( FLEX_MULTI_BUSINESS_LIVE_DEMO ); ?>" target="_blank" id="bundle-button" class="button admin-button bundle-button"><?php echo __('Live Demo', 'flex-multi-business'); ?></a>

						<a href="<?php echo esc_url( FLEX_MULTI_BUSINESS_DOCUMENTATION ); ?>" target="_blank" id="doc-button" class="button admin-button bundle-button"><?php echo __('Documentation', 'flex-multi-business'); ?></a>

                        <p><?php esc_html_e('Build faster. Look better. Go Pro with Flex!', 'flex-multi-business'); ?></p>
					</div>
				</div>
			</div>
		</div>
        <?php
    }

/**
 * Theme Setup
 */
if ( ! function_exists( 'flex_multi_business_admin_notice_setup' ) ) :

function flex_multi_business_admin_notice_setup() {

    if ( is_admin() ) {
        add_action('admin_notices', 'flex_multi_business_admin_notice_activation');
    }
}

endif;

add_action( 'after_setup_theme', 'flex_multi_business_admin_notice_setup' );

add_action('wp_ajax_install_and_activate_flex_import_plugin', 'install_and_activate_flex_import_plugin');

function install_and_activate_flex_import_plugin() {
    // Verify nonce for security
    if (!isset($_POST['nonce']) || !wp_verify_nonce($_POST['nonce'], 'install_activate_nonce')) {
        wp_send_json_error(['message' => 'Nonce verification failed.']);
    }

    // Define plugin slugs and file paths
    $flex_multi_business_elementor_slug = 'elementor';
    $flex_multi_business_elementor_file = 'elementor/elementor.php';
    $flex_multi_business_elementor_url  = 'https://downloads.wordpress.org/plugin/elementor.latest-stable.zip';

    $flex_multi_business_flex_importer_slug = 'flex-import';
    $flex_multi_business_flex_importer_file = 'flex-import/flex-import.php';
    $flex_multi_business_flex_importer_url  = 'https://downloads.wordpress.org/plugin/flex-import.latest-stable.zip';

    // Include necessary WordPress files
    include_once ABSPATH . 'wp-admin/includes/plugin.php';
    include_once ABSPATH . 'wp-admin/includes/file.php';
    include_once ABSPATH . 'wp-admin/includes/misc.php';
    include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
    include_once ABSPATH . 'wp-admin/includes/plugin-install.php';

    $flex_multi_business_upgrader = new Plugin_Upgrader(new Automatic_Upgrader_Skin());

    // Step 1: Install and activate WooCommerce if not active
    if (!is_plugin_active($flex_multi_business_elementor_file)) {
        $flex_multi_business_installed_plugins = get_plugins();

        if (!isset($flex_multi_business_installed_plugins[$flex_multi_business_elementor_file])) {
            // Install WooCommerce
            $flex_multi_business_install_wc = $flex_multi_business_upgrader->install($flex_multi_business_elementor_url);
            if (is_wp_error($flex_multi_business_install_wc)) {
                wp_send_json_error(['message' => 'WooCommerce installation failed.']);
            }
        }

        // Activate WooCommerce
        $flex_multi_business_activate_wc = activate_plugin($flex_multi_business_elementor_file);
        if (is_wp_error($flex_multi_business_activate_wc)) {
            wp_send_json_error(['message' => 'WooCommerce activation failed.', 'error' => $flex_multi_business_activate_wc->get_error_message()]);
        }
    }

    // Step 2: Install and activate Flex Importer plugin
    if (!is_plugin_active($flex_multi_business_flex_importer_file)) {
        $flex_multi_business_installed_plugins = get_plugins();

        if (!isset($flex_multi_business_installed_plugins[$flex_multi_business_flex_importer_file])) {
            // Install Flex Importer plugin
            $flex_multi_business_install_wc_plugin = $flex_multi_business_upgrader->install($flex_multi_business_flex_importer_url);
            if (is_wp_error($flex_multi_business_install_wc_plugin)) {
                wp_send_json_error(['message' => 'Flex Importer plugin installation failed.']);
            }
        }

        // Activate Flex Importer plugin
        $flex_multi_business_activate_wc_plugin = activate_plugin($flex_multi_business_flex_importer_file);
        if (is_wp_error($flex_multi_business_activate_wc_plugin)) {
            wp_send_json_error(['message' => 'Flex Importer plugin activation failed.', 'error' => $flex_multi_business_activate_wc_plugin->get_error_message()]);
        }
    }

    // Success response
    wp_send_json_success(['message' => 'WooCommerce and Flex Importer plugins are activated successfully.']);
}