<?php

class Whizzie {

	public function __construct() {
		$this->init();
	}

	public function init()
	{
	
	}

	public static function rc_drone_setup_widgets(){

		set_theme_mod( 'rc_drone_header_button_url', '#' );

		$rc_drone_product_image_gallery = array();
		$rc_drone_product_ids = array();

		$rc_drone_product_category= array(
			'Product Category'       => array(
				'DJI Mavic 3 Pro',
				'DJI Mavic Pro',
				'DJI Mavic 2 Enterise'
			),
		);

		$rc_drone_k = 1;
		foreach ( $rc_drone_product_category as $rc_drone_product_cats => $rc_drone_products_name ) { 
		// Insert porduct cats Start
		$content = 'This is sample product category';
		$rc_drone_parent_category	=	wp_insert_term(
		$rc_drone_product_cats, // the term
		'product_cat', // the taxonomy
			array(
			'description'=> $content,
			'slug' => str_replace( ' ', '-', $rc_drone_product_cats)
			)
		);

	// -------------- create subcategory END -----------------

		$rc_drone_n=1;
		// create Product START
		foreach ( $rc_drone_products_name as $key => $rc_drone_product_title ) {
		$content = '
			<div class="main_content">
			<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry\'s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.</p>
			</div>';

		// Create post object
		$rc_drone_my_post = array(
			'post_title'    => wp_strip_all_tags( $rc_drone_product_title ),
			'post_content'  => $content,
			'post_status'   => 'publish',
			'post_type'     => 'product',
			'post_category' => [$rc_drone_parent_category['term_id']]
		);

		// Insert the post into the database

		$rc_drone_uqpost_id = wp_insert_post($rc_drone_my_post);
		wp_set_object_terms( $rc_drone_uqpost_id, str_replace( ' ', '-', $rc_drone_product_cats), 'product_cat', true );

		$rc_drone_product_price = array('$2199.00','$2199.00','$2199.00','$2199.00');
		$rc_drone_product_regular_price = array('$2199.00','$2199.00','$2199.00','$2199.00');
		
		update_post_meta( $rc_drone_uqpost_id, '_regular_price', $rc_drone_product_regular_price[$rc_drone_n-1] );
		update_post_meta( $rc_drone_uqpost_id, '_price', $rc_drone_product_price[$rc_drone_n-1] );
		// update_post_meta( $rc_drone_uqpost_id, '_sale_price', $rc_drone_product_sale_price[$rc_drone_n-1] );
		array_push( $rc_drone_product_ids,  $rc_drone_uqpost_id );

		// Now replace meta w/ new updated value array
		$rc_drone_image_url = get_template_directory_uri().'/assets/images/product/'.$rc_drone_product_cats.'/' . str_replace(' ', '_', strtolower($rc_drone_product_title)).'.png';
		$rc_drone_image_name  = $rc_drone_product_title.'.png';
		$rc_drone_upload_dir = wp_upload_dir();
		// Set upload folder
		$rc_drone_image_data = file_get_contents(esc_url($rc_drone_image_url));
		// Get image data
		$unique_file_name = wp_unique_filename($rc_drone_upload_dir['path'], $rc_drone_image_name);
		// Generate unique name
		$rc_drone_filename = basename($unique_file_name);
		// Create image file name
		// Check folder permission and define file location
		if (wp_mkdir_p($rc_drone_upload_dir['path'])) {
		$rc_drone_file = $rc_drone_upload_dir['path'].'/'.$rc_drone_filename;
		} else {
		$rc_drone_file = $rc_drone_upload_dir['basedir'].'/'.$rc_drone_filename;
		}
		
		file_put_contents($rc_drone_file, $rc_drone_image_data);
		// Check image file type
		$wp_filetype = wp_check_filetype($rc_drone_filename, null);
		// Set attachment data
		$attachment = array(
			'post_mime_type' => $wp_filetype['type'],
			'post_title'     => sanitize_file_name($rc_drone_filename),
			'post_type'      => 'product',
			'post_status'    => 'inherit',
		);

		// Create the attachment
		$rc_drone_attach_id = wp_insert_attachment($attachment, $rc_drone_file, $rc_drone_uqpost_id);

		// Define attachment metadata
		$attach_data = wp_generate_attachment_metadata($rc_drone_attach_id, $rc_drone_file);

		// Assign metadata to attachment
		wp_update_attachment_metadata($rc_drone_attach_id, $attach_data);
		if ( count( $rc_drone_product_image_gallery ) < 3 ) {
			array_push( $rc_drone_product_image_gallery, $rc_drone_attach_id );
		}
		// // And finally assign featured image to post
		set_post_thumbnail($rc_drone_uqpost_id, $rc_drone_attach_id);
		++$rc_drone_n;
		}
		// Create product END
		++$rc_drone_k;
		}
		// Add Gallery in first simple product and second variable product START
		$rc_drone_product_image_gallery = implode( ',', $rc_drone_product_image_gallery );
		foreach ( $rc_drone_product_ids as $rc_drone_product_id ) {
		update_post_meta( $rc_drone_product_id, 'rc_drone_product_image_gallery', $rc_drone_product_image_gallery );

		}

	    /* Create Menu */
            $rc_drone_menuname  = 'Main Menus';
            $rc_drone_location  = 'main-menu';

            $rc_drone_menu = wp_get_nav_menu_object( $rc_drone_menuname );

            if ( ! $rc_drone_menu ) {
            $rc_drone_menu_id = wp_create_nav_menu( $rc_drone_menuname );

           // Home Page 
			wp_update_nav_menu_item( $rc_drone_menu_id, 0, array(
				'menu-item-title'     => __( 'Home', 'rc-drone' ),
				'menu-item-url'       => home_url( '/' ),
				'menu-item-type'      => 'custom',
				'menu-item-status'    => 'publish',
				'menu-item-position'  => 1,
			) );

			// About Page 
			$rc_drone_about_id = wp_insert_post( array(
			'post_type'   => 'page',
			'post_content' => 'We are committed to providing reliable, professional, and result-oriented solutions tailored to meet individual needs. Our goal is to empower people with the right guidance, knowledge, and support to help them make informed decisions for a better future. <br><br> Our mission is to deliver high-quality services with honesty, transparency, and dedication. We focus on understanding client requirements and offering practical solutions that create long-term value. <br><br> With a client-centric approach, experienced professionals, and a commitment to excellence, we ensure every individual receives the attention and guidance they deserve. We believe in building trust through quality service and consistent results.',
			'post_title'  => 'About Us',
			'post_status' => 'publish',
			) );

			if ( $rc_drone_about_id ) {
			wp_update_nav_menu_item( $rc_drone_menu_id, 0, array(
			'menu-item-title'     => 'About Us',
			'menu-item-object'    => 'page',
			'menu-item-object-id' => $rc_drone_about_id,
			'menu-item-type'      => 'post_type',
			'menu-item-status'    => 'publish',
            'menu-item-position'  => 2,
			) );
			}

			// Services Page 
			$rc_drone_about_id = wp_insert_post( array(
			'post_type'   => 'page',
			'post_content' => 'We are committed to providing reliable, professional, and result-oriented solutions tailored to meet individual needs. Our goal is to empower people with the right guidance, knowledge, and support to help them make informed decisions for a better future. <br><br> Our mission is to deliver high-quality services with honesty, transparency, and dedication. We focus on understanding client requirements and offering practical solutions that create long-term value. <br><br> With a client-centric approach, experienced professionals, and a commitment to excellence, we ensure every individual receives the attention and guidance they deserve. We believe in building trust through quality service and consistent results.',
			'post_title'  => 'Services',
			'post_status' => 'publish',
			) );

			if ( $rc_drone_about_id ) {
			wp_update_nav_menu_item( $rc_drone_menu_id, 0, array(
			'menu-item-title'     => 'Services',
			'menu-item-object'    => 'page',
			'menu-item-object-id' => $rc_drone_about_id,
			'menu-item-type'      => 'post_type',
			'menu-item-status'    => 'publish',
            'menu-item-position'  => 3,
			) );
			}

			// Products Page 
			$rc_drone_about_id = wp_insert_post( array(
			'post_type'   => 'page',
			'post_content' => 'We are committed to providing reliable, professional, and result-oriented solutions tailored to meet individual needs. Our goal is to empower people with the right guidance, knowledge, and support to help them make informed decisions for a better future. <br><br> Our mission is to deliver high-quality services with honesty, transparency, and dedication. We focus on understanding client requirements and offering practical solutions that create long-term value. <br><br> With a client-centric approach, experienced professionals, and a commitment to excellence, we ensure every individual receives the attention and guidance they deserve. We believe in building trust through quality service and consistent results.',
			'post_title'  => 'Products',
			'post_status' => 'publish',
			) );

			if ( $rc_drone_about_id ) {
			wp_update_nav_menu_item( $rc_drone_menu_id, 0, array(
			'menu-item-title'     => 'Products',
			'menu-item-object'    => 'page',
			'menu-item-object-id' => $rc_drone_about_id,
			'menu-item-type'      => 'post_type',
			'menu-item-status'    => 'publish',
            'menu-item-position'  => 4,
			) );
			}

            // Create Shop Page
                $rc_drone_about_title = 'Shop';
                $rc_drone_about_content = 'Lorem ipsum dolor sit amet...';

                $shop_page = get_page_by_path('shop');
                if ( !$shop_page ) {
                    $rc_drone_about = array(
                        'post_type'   => 'page',
                        'post_title'  => $rc_drone_about_title,
                        'post_content'=> $rc_drone_about_content,
                        'post_status' => 'publish',
                        'post_author' => 1,
                        'post_name'   => 'shop' 
                        );
                        $rc_drone_about_id = wp_insert_post($rc_drone_about);
                    } else {
                        $rc_drone_about_id = $shop_page->ID;
                    }

                    wp_update_nav_menu_item($rc_drone_menu_id, 0, array(
                        'menu-item-title'   => __('Shop', 'rc-drone'),
                        'menu-item-classes' => 'shop',
                        'menu-item-object-id' => $rc_drone_about_id,
                        'menu-item-object'  => 'page',
                        'menu-item-type'    => 'post_type',
                        'menu-item-status'  => 'publish'
                    ));
            
			/* ---------- Assign Menu Location ---------- */
			$rc_drone_locations = get_theme_mod( 'nav_menu_locations', array() );
			$rc_drone_locations[ $rc_drone_location ] = $rc_drone_menu_id;
			set_theme_mod( 'nav_menu_locations', $rc_drone_locations );
		}
    }
}
 