<div class="theme-info">
	<?php
        // Check if the demo import has been completed
        $blogger_hub_demo_import_completed = get_option('blogger_hub_demo_import_completed', false);

        // If the demo import is completed, display the "View Site" button
        if ($blogger_hub_demo_import_completed) {
        echo '<p class="notice-text">' . esc_html__('Your demo import has been completed successfully.', 'blogger-hub') . '</p>';
        echo '<span><a href="' . esc_url(home_url()) . '" class="import-btn site-btn" target="_blank">' . esc_html__('VISIT SITE', 'blogger-hub') . '</a></span>';
        echo '<span><a href="' . esc_url( admin_url('customize.php')) . '" class="import-btn site-btn" target="_blank">' . esc_html__('CUSTOMIZE', 'blogger-hub') . '</a></span>';
        }

		//POST and update the customizer and other related data of Blogger Hub
        if (isset($_POST['submit'])) {

            // ------- Create Nav Menu --------
            $blogger_hub_menuname = 'Main Menus';
            $blogger_hub_bpmenulocation = 'primary';
            $blogger_hub_menu_exists = wp_get_nav_menu_object($blogger_hub_menuname);

            if (!$blogger_hub_menu_exists) {
                $blogger_hub_menu_id = wp_create_nav_menu($blogger_hub_menuname);

                // Create Home Page
                $blogger_hub_home_title = 'Home';
                $blogger_hub_home = array(
                    'post_type' => 'page',
                    'post_title' => $blogger_hub_home_title,
                    'post_content' => '',
                    'post_status' => 'publish',
                    'post_author' => 1,
                    'post_slug' => 'home'
                );
                $blogger_hub_home_id = wp_insert_post($blogger_hub_home);
                // Assign Home Page Template
                add_post_meta($blogger_hub_home_id, '_wp_page_template', 'page-template/custom-front-page.php');
                // Update options to set Home Page as the front page
                update_option('page_on_front', $blogger_hub_home_id);
                update_option('show_on_front', 'page');
                // Add Home Page to Menu
                wp_update_nav_menu_item($blogger_hub_menu_id, 0, array(
                    'menu-item-title' => __('Home', 'blogger-hub'),
                    'menu-item-classes' => 'home',
                    'menu-item-url' => home_url('/'),
                    'menu-item-status' => 'publish',
                    'menu-item-object-id' => $blogger_hub_home_id,
                    'menu-item-object' => 'page',
                    'menu-item-type' => 'post_type'
                ));

                // Create Pages Page with Dummy Content
                $blogger_hub_pages_title = 'Pages';
                $blogger_hub_pages_content = '
                <p>Explore all the pages we have on our website. Find information about our services, company, and more.</p>

                 Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry standard dummy text ever since the 1500, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960 with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.<br> 

                  All the Lorem Ipsum generators on the Internet tend to repeat predefined chunks as necessary, making this the first true generator on the Internet. It uses a dictionary of over 200 Latin words, combined with a handful of model sentence structures, to generate Lorem Ipsum which looks reasonable. The generated Lorem Ipsum is therefore always free from repetition, injected humour, or non-characteristic words etc.';
                $blogger_hub_pages = array(
                    'post_type' => 'page',
                    'post_title' => $blogger_hub_pages_title,
                    'post_content' => $blogger_hub_pages_content,
                    'post_status' => 'publish',
                    'post_author' => 1,
                    'post_slug' => 'pages'
                );
                $blogger_hub_pages_id = wp_insert_post($blogger_hub_pages);
                // Add Pages Page to Menu
                wp_update_nav_menu_item($blogger_hub_menu_id, 0, array(
                    'menu-item-title' => __('Pages', 'blogger-hub'),
                    'menu-item-classes' => 'pages',
                    'menu-item-url' => home_url('/pages/'),
                    'menu-item-status' => 'publish',
                    'menu-item-object-id' => $blogger_hub_pages_id,
                    'menu-item-object' => 'page',
                    'menu-item-type' => 'post_type'
                ));

                // Create About Us Page with Dummy Content
                $blogger_hub_about_title = 'About Us';
                $blogger_hub_about_content = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam...<br>

                Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry standard dummy text ever since the 1500, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960 with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.<br> 

                There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form, by injected humour, or randomised words which dont look even slightly believable. If you are going to use a passage of Lorem Ipsum, you need to be sure there isnt anything embarrassing hidden in the middle of text.<br> 

                All the Lorem Ipsum generators on the Internet tend to repeat predefined chunks as necessary, making this the first true generator on the Internet. It uses a dictionary of over 200 Latin words, combined with a handful of model sentence structures, to generate Lorem Ipsum which looks reasonable. The generated Lorem Ipsum is therefore always free from repetition, injected humour, or non-characteristic words etc.';
                $blogger_hub_about = array(
                    'post_type' => 'page',
                    'post_title' => $blogger_hub_about_title,
                    'post_content' => $blogger_hub_about_content,
                    'post_status' => 'publish',
                    'post_author' => 1,
                    'post_slug' => 'about-us'
                );
                $blogger_hub_about_id = wp_insert_post($blogger_hub_about);
                // Add About Us Page to Menu
                wp_update_nav_menu_item($blogger_hub_menu_id, 0, array(
                    'menu-item-title' => __('About Us', 'blogger-hub'),
                    'menu-item-classes' => 'about-us',
                    'menu-item-url' => home_url('/about-us/'),
                    'menu-item-status' => 'publish',
                    'menu-item-object-id' => $blogger_hub_about_id,
                    'menu-item-object' => 'page',
                    'menu-item-type' => 'post_type'
                ));

                // Set the menu location if it's not already set
                if (!has_nav_menu($blogger_hub_bpmenulocation)) {
                    $blogger_hub_locations = get_theme_mod('nav_menu_locations'); // Use 'nav_menu_locations' to get locations array
                    if (empty($blogger_hub_locations)) {
                        $blogger_hub_locations = array();
                    }
                    $blogger_hub_locations[$blogger_hub_bpmenulocation] = $blogger_hub_menu_id;
                    set_theme_mod('nav_menu_locations', $blogger_hub_locations);
                }
                
        }    

        // Set the demo import completion flag
        update_option('blogger_hub_demo_import_completed', true);
        // Display success message and "View Site" button
        echo '<p class="notice-text">' . esc_html__('Your demo import has been completed successfully.', 'blogger-hub') . '</p>';
        echo '<span><a href="' . esc_url(home_url()) . '" class="import-btn site-btn" target="_blank">' . esc_html__('VISIT SITE', 'blogger-hub') . '</a></span>';
        echo '<span><a href="' . esc_url( admin_url('customize.php')) . '" class="import-btn site-btn" target="_blank">' . esc_html__('CUSTOMIZE', 'blogger-hub') . '</a></span>';
        //end 

        //Topbar
        set_theme_mod( 'blogger_hub_category_hide', true );
        set_theme_mod( 'blogger_hub_facebook_url', 'https://www.facebook.com/' );
        set_theme_mod( 'blogger_hub_twitter_url', 'https://twitter.com/' );
        set_theme_mod( 'blogger_hub_insta_url', 'https://www.instagram.com/' );
        set_theme_mod( 'blogger_hub_linkdin_url', 'https://www.linkedin.com/' );
        set_theme_mod( 'blogger_hub_youtube_url', 'https://www.instagram.com/' );

        // Set Header Image
        $header_image_url = get_template_directory_uri() . '/images/banner.png';

        // Download the image and get the attachment ID
        $header_image_id = media_sideload_image($header_image_url, 0, null, 'id');

        if (!is_wp_error($header_image_id)) {
            // Get the full image URL from the attachment
            $header_image_src = wp_get_attachment_image_src($header_image_id, 'full');

            if (!empty($header_image_src[0])) {
                // Set the header image using the theme mod
                set_theme_mod('header_image', esc_url_raw($header_image_src[0]));
            }
        }
        // Set site title and tagline
        set_theme_mod('blogger_hub_show_tagline', true);
        update_option('blogname', 'Journey');
        update_option('blogdescription', 'Welcome To Blog Theme');
       

        //Category Section
        set_theme_mod( 'blogger_hub_section_title', 'Recent Post' );
        set_theme_mod( 'blogger_hub_metafields_share_link', true );
        

        // Set the theme mod for the default category
        set_theme_mod('blogger_hub_our_category', 'postcategory1');

        // Define post category names and post titles
        $blogger_hub_category_names = ['postcategory1', 'postcategory2', 'postcategory3'];
        $blogger_hub_title_array = [
            ["FEATURES TITLE 1", "FEATURES TITLE 2"],
            ["FEATURES TITLE 1", "FEATURES TITLE 2"],
            ["FEATURES TITLE 1", "FEATURES TITLE 2"]
        ];

        $blogger_hub_content_array = [
            [
                "Lorem ipsum dolor sit amet consectetur adipisicing elit. Cumque delectus, quidem id ratione consequatur eveniet", 
                "Lorem ipsum dolor sit amet consectetur adipisicing elit. Cumque delectus, quidem id ratione consequatur eveniet", 
            ],
            [
                "Lorem ipsum dolor sit amet consectetur adipisicing elit. Cumque delectus, quidem id ratione consequatur eveniet", 
                "Lorem ipsum dolor sit amet consectetur adipisicing elit. Cumque delectus, quidem id ratione consequatur eveniet"
            ],
            [
                "Lorem ipsum dolor sit amet consectetur adipisicing elit. Cumque delectus, quidem id ratione consequatur eveniet", 
                "Lorem ipsum dolor sit amet consectetur adipisicing elit. Cumque delectus, quidem id ratione consequatur eveniet"
            ]
        ];

        foreach ($blogger_hub_category_names as $blogger_hub_index => $blogger_hub_category_name) {
            // Create or retrieve the post category term ID
            $blogger_hub_category_term = term_exists($blogger_hub_category_name, 'category');
            if (!$blogger_hub_category_term) {
                $blogger_hub_category_term = wp_insert_term($blogger_hub_category_name, 'category');
            }

            if (is_wp_error($blogger_hub_category_term)) {
                error_log('Error creating category: ' . $blogger_hub_category_term->get_error_message());
                continue; // Skip if category creation fails
            }

            $blogger_hub_category_id = isset($blogger_hub_category_term['term_id']) ? (int)$blogger_hub_category_term['term_id'] : $blogger_hub_category_term;

            for ($blogger_hub_i = 0; $blogger_hub_i < 2; $blogger_hub_i++) {
                // Ensure the required data exists
                if (empty($blogger_hub_title_array[$blogger_hub_index][$blogger_hub_i]) || 
                    empty($blogger_hub_content_array[$blogger_hub_index][$blogger_hub_i])) {
                    continue; // Skip if title or content is missing
                }

                // Create the post
                $blogger_hub_post_data = [
                    'post_title'    => wp_strip_all_tags($blogger_hub_title_array[$blogger_hub_index][$blogger_hub_i]),
                    'post_content'  => $blogger_hub_content_array[$blogger_hub_index][$blogger_hub_i],
                    'post_status'   => 'publish',
                    'post_type'     => 'post', // Default WordPress post type
                ];
                $blogger_hub_post_id = wp_insert_post($blogger_hub_post_data);

                if (is_wp_error($blogger_hub_post_id)) {
                    error_log('Error creating post: ' . $blogger_hub_post_id->get_error_message());
                    continue; // Skip if post creation fails
                }

                // Assign category to the post
                wp_set_post_categories($blogger_hub_post_id, [$blogger_hub_category_id]);

                // Handle the featured image
                $blogger_hub_image_url = get_template_directory_uri() . '/images/feature' . ($blogger_hub_i + 1) . '.png';
                $blogger_hub_image_id = media_sideload_image($blogger_hub_image_url, $blogger_hub_post_id, null, 'id');

                if (is_wp_error($blogger_hub_image_id)) {
                    error_log('Error downloading image: ' . $blogger_hub_image_id->get_error_message());
                    continue; // Skip if image download fails
                }

                // Set the featured image
                set_post_thumbnail($blogger_hub_post_id, $blogger_hub_image_id);
            }
        }

        //Copyright Text
        set_theme_mod( 'blogger_hub_footer_text', 'By Themesglance' );

        }
    ?>
    <form action="<?php echo esc_url(home_url()); ?>/wp-admin/themes.php?page=blogger_hub_guide" method="POST" onsubmit="return validate(this);">
    <?php if (!get_option('blogger_hub_demo_import_completed')) : ?>
        <div class="demo-btn">
        <p><?php echo  esc_html_e( 'You are about to use the most easy to use and flexible WordPress theme.', 'blogger-hub' ); ?></p>
            <button id="import-button" type="submit" name="submit" class="import-btn run-import button-large">
                    <?php esc_html_e('Run Importer', 'blogger-hub'); ?>
                        <span id="spinner" style="display: none;">
                            <img src="<?php echo esc_url(get_template_directory_uri()); ?>/images/spinner.gif" alt="Loading..." style="width:34px; height:34px; margin-left:10px;vertical-align: middle;" />
                        </span>
            </button>
        </div>
    <?php endif; ?>
    </form>
	<script type="text/javascript">
		function validate(valid) {
			 if(confirm("Do you really want to import the Demo Theme Content?")){
			    document.getElementById('spinner').style.display = 'inline-block';
			}
		    else {
			    return false;
		    }
		}
	</script>
</div>
