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

        // If the demo import is completed, display the "View Site" button
        if ($video_streaming_demo_import_completed) {
        echo '<p class="notice-text">' . esc_html__('Your demo import has been completed successfully.', 'video-streaming') . '</p>';
        echo '<span><a href="' . esc_url(home_url()) . '" class="button button-primary site-btn" target="_blank">' . esc_html__('VIEW SITE', 'video-streaming') . '</a></span>';
        }
		
        if (isset($_POST['submit'])) {

            // ------- Create Nav Menu --------
            $video_streaming_menuname = 'Main Menus';
            $video_streaming_bpmenulocation = 'primary';
            $video_streaming_menu_exists = wp_get_nav_menu_object($video_streaming_menuname);

            if (!$video_streaming_menu_exists) {
                $video_streaming_menu_id = wp_create_nav_menu($video_streaming_menuname);

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

                // Create About Us Page with Dummy Content
                $video_streaming_about_title = 'About Us';
                $video_streaming_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.';
                $video_streaming_about = array(
                    'post_type' => 'page',
                    'post_title' => $video_streaming_about_title,
                    'post_content' => $video_streaming_about_content,
                    'post_status' => 'publish',
                    'post_author' => 1,
                    'post_slug' => 'about-us'
                );
                $video_streaming_about_id = wp_insert_post($video_streaming_about);
                // Add About Us Page to Menu
                wp_update_nav_menu_item($video_streaming_menu_id, 0, array(
                    'menu-item-title' => __('About Us', 'video-streaming'),
                    'menu-item-classes' => 'about-us',
                    'menu-item-url' => home_url('/about-us/'),
                    'menu-item-status' => 'publish',
                    'menu-item-object-id' => $video_streaming_about_id,
                    'menu-item-object' => 'page',
                    'menu-item-type' => 'post_type'
                ));

                /* Create Page */
                $video_streaming_page_title = 'Page';
                $video_streaming_page_content = 'Explore different pages available on our video streaming platform.';

                $video_streaming_page = array(
                    'post_type'    => 'page',
                    'post_title'   => $video_streaming_page_title,
                    'post_content' => $video_streaming_page_content,
                    'post_status'  => 'publish',
                    'post_author'  => 1,
                    'post_name'    => 'page'
                );
                $video_streaming_page_id = wp_insert_post($video_streaming_page);

                wp_update_nav_menu_item($video_streaming_menu_id, 0, array(
                    'menu-item-title'   => __('Page', 'video-streaming'),
                    'menu-item-classes' => 'page',
                    'menu-item-url'     => home_url('/page/'),
                    'menu-item-status'  => 'publish',
                    'menu-item-object-id' => $video_streaming_page_id,
                    'menu-item-object'  => 'page',
                    'menu-item-type'    => 'post_type'
                ));

                /* Create Modules */
                $video_streaming_modules_title = 'Modules';
                $video_streaming_modules_content = 'Explore available modules and features of our platform.';

                $video_streaming_modules = array(
                    'post_type'    => 'page',
                    'post_title'   => $video_streaming_modules_title,
                    'post_content' => $video_streaming_modules_content,
                    'post_status'  => 'publish',
                    'post_author'  => 1,
                    'post_name'    => 'modules'
                );
                $video_streaming_modules_id = wp_insert_post($video_streaming_modules);

                wp_update_nav_menu_item($video_streaming_menu_id, 0, array(
                    'menu-item-title'   => __('Modules', 'video-streaming'),
                    'menu-item-classes' => 'modules',
                    'menu-item-url'     => home_url('/modules/'),
                    'menu-item-status'  => 'publish',
                    'menu-item-object-id' => $video_streaming_modules_id,
                    'menu-item-object'  => 'page',
                    'menu-item-type'    => 'post_type'
                ));

                // Create Blog Page 
                $video_streaming_blog_page_title = 'Blog';
                $video_streaming_blog_page_query = new WP_Query(array(
                    'post_type'      => 'page',
                    'name'           => sanitize_title($video_streaming_blog_page_title),
                    'post_status'    => 'publish',
                    'posts_per_page' => 1
                ));
                if (!$video_streaming_blog_page_query->have_posts()) {
                    $video_streaming_blog_page = array(
                    'post_type'   => 'page',
                    'post_title'  => $video_streaming_blog_page_title,
                    'post_status' => 'publish',
                    'post_author' => 1,
                    );
                    $video_streaming_blog_page_id = wp_insert_post($video_streaming_blog_page);
                    update_option('page_for_posts', $video_streaming_blog_page_id);
                    wp_update_nav_menu_item($video_streaming_menu_id, 0, array(
                    'menu-item-title'      => __('Blog', 'video-streaming'),
                    'menu-item-url'        => get_permalink($video_streaming_blog_page_id),
                    'menu-item-status'     => 'publish',
                    'menu-item-object-id'  => $video_streaming_blog_page_id,
                    'menu-item-object'     => 'page',
                    'menu-item-type'       => 'post_type',
                    ));
                }

                // Create Pages Page with Dummy Content
                $video_streaming_contact_title = 'Contact Us';
                $video_streaming_contact_content = 'Explore all the contact we have on our website. Find information about our services, company, and more.
                 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 contactly 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.';
                $video_streaming_contact = array(
                    'post_type' => 'page',
                    'post_title' => $video_streaming_contact_title,
                    'post_content' => $video_streaming_contact_content,
                    'post_status' => 'publish',
                    'post_author' => 1,
                    'post_slug' => 'contact'
                );
                $video_streaming_contact_id = wp_insert_post($video_streaming_contact);
                // Add Pages Page to Menu
                wp_update_nav_menu_item($video_streaming_menu_id, 0, array(
                    'menu-item-title' => __('Contact', 'video-streaming'),
                    'menu-item-classes' => 'contact',
                    'menu-item-url' => home_url('/contact/'),
                    'menu-item-status' => 'publish',
                    'menu-item-object-id' => $video_streaming_contact_id,
                    'menu-item-object' => 'page',
                    'menu-item-type' => 'post_type'
                ));

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

            //Header
            set_theme_mod('video_streaming_topbar_text', 'Daily New & Trendy Vlogs at one Point');
            set_theme_mod('video_streaming_header_button_text', 'Submit a Video');
            set_theme_mod('video_streaming_header_button_url', '#');

            //Social Media
            set_theme_mod('video_streaming_facebook_url', 'https://www.facebook.com');
            set_theme_mod('video_streaming_twitter_url', 'https://www.twitter.com');
            set_theme_mod('video_streaming_instagram_url', 'https://www.instagram.com');
            set_theme_mod('video_streaming_linkedin_url', 'https://www.linkedin.com');

            //Slider
            set_theme_mod('video_streaming_slider_hide', true);
            set_theme_mod('video_streaming_slider_button_text', 'Read More');
            set_theme_mod('video_streaming_slider_button_link', '#');
            set_theme_mod('video_streaming_slider_video_category', 'category1' );
            // Define post category names and post titles
            $video_streaming_category_names = array('category1', 'category2', 'category3', 'category4');
            $video_streaming_title_array = array(
                array(
                    "Fashion is the armor to survive the reality of everyday life.", 
                    "Clothing is the canvas, and fashion is the art that paints it.", 
                    "Your outfit is your daily chance to tell the world your story without saying a word.", 
                    "Fashion is the bridge that connects self-expression to the rhythm of society."
                ),
                array(
                    "Fashion is the armor to survive the reality of everyday life.", 
                    "Clothing is the canvas, and fashion is the art that paints it.", 
                    "Your outfit is your daily chance to tell the world your story without saying a word.", 
                    "Fashion is the bridge that connects self-expression to the rhythm of society."
                ),
                array(
                    "Fashion is the armor to survive the reality of everyday life.", 
                    "Clothing is the canvas, and fashion is the art that paints it.", 
                    "Your outfit is your daily chance to tell the world your story without saying a word.", 
                    "Fashion is the bridge that connects self-expression to the rhythm of society."
                ),
                array(
                    "Fashion is the armor to survive the reality of everyday life.", 
                    "Clothing is the canvas, and fashion is the art that paints it.", 
                    "Your outfit is your daily chance to tell the world your story without saying a word.", 
                    "Fashion is the bridge that connects self-expression to the rhythm of society."
                )
            );
            $video_streaming_post_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 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.';
            // Define unique video URLs
            $video_streaming_video_urls = array(
                'https://www.youtube.com/embed/9xwazD5SyVg',
                'https://www.youtube.com/embed/tgbNymZ7vqY',
                'https://www.youtube.com/embed/3JZ_D3ELwOQ',
                'https://www.youtube.com/embed/L_jWHffIx5E'
            );

            foreach ($video_streaming_category_names as $video_streaming_index => $video_streaming_category_name) {
                // Create or retrieve the post category term ID
                $video_streaming_term = term_exists($video_streaming_category_name, 'category');
                if ($video_streaming_term === 0 || $video_streaming_term === null) {
                    $video_streaming_term = wp_insert_term($video_streaming_category_name, 'category');
                }
                if (is_wp_error($video_streaming_term)) {
                    error_log('Error creating category: ' . $video_streaming_term->get_error_message());
                    continue;
                }

                for ($video_streaming_i = 0; $video_streaming_i < 4; $video_streaming_i++) {
                    // Create post content with unique video URL
                    $video_streaming_title = $video_streaming_title_array[$video_streaming_index][$video_streaming_i];
                    $current_video_url = $video_streaming_video_urls[$video_streaming_i % count($video_streaming_video_urls)];
                    $current_post_content = $video_streaming_post_content . "\n\n[embed]{$current_video_url}[/embed]";

                    // Create post post object
                    $video_streaming_my_post = array(
                        'post_title'    => wp_strip_all_tags($video_streaming_title),
                        'post_content'  => $current_post_content,
                        'post_status'   => 'publish',
                        'post_type'     => 'post',
                        'post_excerpt'  => substr($current_post_content, 0, 100) . '...',
                    );

                    // Insert the post into the database
                    $video_streaming_post_id = wp_insert_post($video_streaming_my_post);

                    if (is_wp_error($video_streaming_post_id)) {
                        error_log('Error creating post: ' . $video_streaming_post_id->get_error_message());
                        continue;
                    }

                    // Assign the category to the post
                    wp_set_post_categories($video_streaming_post_id, array((int)$video_streaming_term['term_id']));

                    // Handle the featured image using media_sideload_image
                    $video_streaming_image_url = get_template_directory_uri() . '/images/slider' . ($video_streaming_i + 1) . '.png';
                    $video_streaming_image_id = media_sideload_image($video_streaming_image_url, $video_streaming_post_id, null, 'id');

                    if (is_wp_error($video_streaming_image_id)) {
                        error_log('Error downloading image: ' . $video_streaming_image_id->get_error_message());
                        continue;
                    }

                    // Assign featured image to post
                    set_post_thumbnail($video_streaming_post_id, $video_streaming_image_id);
                }
            }

            //Trending Videos
            set_theme_mod('video_streaming_video_enable', true);
            set_theme_mod('video_streaming_video_section_title', 'Trending Videos');
            set_theme_mod('video_streaming_video_category', 'postcategory1' );
            // Define post category names and post titles
            $video_streaming_category_names = array('postcategory1', 'postcategory2', 'postcategory3', 'postcategory4');
            $video_streaming_title_array = array(
                array(
                    "Down in the lower pastures there are already some flowers.", 
                    "Use Artist Wordpress Theme to Find Out About Art Event.", 
                    "Explore the Vibrant World of Art with the Latest Artist Wordpress Theme.", 
                    "Elevate Your Art Event with a Stunning Artist Portfolio Theme."
                ),
                array(
                    "Down in the lower pastures there are already some flowers.", 
                    "Use Artist Wordpress Theme to Find Out About Art Event.", 
                    "Explore the Vibrant World of Art with the Latest Artist Wordpress Theme.", 
                    "Elevate Your Art Event with a Stunning Artist Portfolio Theme."
                ),
                array(
                    "Down in the lower pastures there are already some flowers.", 
                    "Use Artist Wordpress Theme to Find Out About Art Event.", 
                    "Explore the Vibrant World of Art with the Latest Artist Wordpress Theme.", 
                    "Elevate Your Art Event with a Stunning Artist Portfolio Theme."
                ),
                array(
                    "Down in the lower pastures there are already some flowers.", 
                    "Use Artist Wordpress Theme to Find Out About Art Event.", 
                    "Explore the Vibrant World of Art with the Latest Artist Wordpress Theme.", 
                    "Elevate Your Art Event with a Stunning Artist Portfolio Theme."
                )
            );
            $video_streaming_post_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 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.';
            $video_streaming_video_urls = array(
                'https://www.youtube.com/embed/9xwazD5SyVg',
                'https://www.youtube.com/embed/tgbNymZ7vqY',
                'https://www.youtube.com/embed/3JZ_D3ELwOQ',
                'https://www.youtube.com/embed/L_jWHffIx5E'
            );
            
            foreach ($video_streaming_category_names as $video_streaming_index => $video_streaming_category_name) {
                // Create or retrieve the post category term ID
                $video_streaming_term = term_exists($video_streaming_category_name, 'category');
                if ($video_streaming_term === 0 || $video_streaming_term === null) {
                    // If the term does not exist, create it
                    $video_streaming_term = wp_insert_term($video_streaming_category_name, 'category');
                }
                if (is_wp_error($video_streaming_term)) {
                    error_log('Error creating category: ' . $video_streaming_term->get_error_message());
                    continue; // Skip to the next iteration if category creation fails
                }
            
                for ($video_streaming_i = 0; $video_streaming_i < 4; $video_streaming_i++) {
                    // Create post content with a unique video URL for each post
                    $video_streaming_video_url = $video_streaming_video_urls[$video_streaming_i % count($video_streaming_video_urls)];
                    $video_streaming_post_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 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.';
                    $video_streaming_post_content .= "\n\n[embed]{$video_streaming_video_url}[/embed]";
            
                    // Create post post object
                    $video_streaming_title = $video_streaming_title_array[$video_streaming_index][$video_streaming_i];
                    $video_streaming_my_post = array(
                        'post_title'    => wp_strip_all_tags($video_streaming_title),
                        'post_content'  => $video_streaming_post_content,
                        'post_status'   => 'publish',
                        'post_type'     => 'post', // Post type set to 'post'
                        'post_excerpt'  => substr($video_streaming_post_content, 0, 100) . '...', // Add an excerpt
                    );
            
                    // Insert the post into the database
                    $video_streaming_post_id = wp_insert_post($video_streaming_my_post);
            
                    if (is_wp_error($video_streaming_post_id)) {
                        error_log('Error creating post: ' . $video_streaming_post_id->get_error_message());
                        continue; // Skip to the next post if creation fails
                    }
            
                    // Assign the category to the post
                    wp_set_post_categories($video_streaming_post_id, array((int)$video_streaming_term['term_id']));
            
                    // Handle the featured image using media_sideload_image
                    $video_streaming_image_url = get_template_directory_uri() . '/images/videos' . ($video_streaming_i + 1) . '.png';
                    $video_streaming_image_id = media_sideload_image($video_streaming_image_url, $video_streaming_post_id, null, 'id');
            
                    if (is_wp_error($video_streaming_image_id)) {
                        error_log('Error downloading image: ' . $video_streaming_image_id->get_error_message());
                        continue; // Skip to the next post if image download fails
                    }
            
                    // Assign featured image to post
                    set_post_thumbnail($video_streaming_post_id, $video_streaming_image_id);
                }
            }
            

            // Set the demo import completion flag
    		update_option('video_streaming_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.', 'video-streaming') . '</p>';
    		echo '<span><a href="' . esc_url(home_url()) . '" class="button button-primary site-btn" target="_blank">' . esc_html__('VIEW SITE', 'video-streaming') . '</a></span>';
            //end

        }
    ?>

    <form action="<?php echo esc_url(home_url()); ?>/wp-admin/themes.php?page=video-streaming-guide-page" method="POST" onsubmit="return validate(this);">
    <?php if (!get_option('video_streaming_demo_import_completed')) : ?>
        <form method="post">
        <p><?php esc_html_e('Click the below run importer button to import demo content','video-streaming'); ?></p>
            <input class= "run-import" type="submit" name="submit" value="<?php esc_attr_e('Run Importer','video-streaming'); ?>" class="button button-primary button-large">
        </form>
    <?php endif; ?>
    </form>
	<script type="text/javascript">
		function validate(valid) {
			if ( confirm("Do you really want to import the theme demo content?")) {
                // Show loader
                document.getElementById('demo-import-loader').style.display = 'block';
                
                // Submit form
                document.forms[0].submit();
            } else {
                return false;
            }
		}
	</script>
</div>
