<?php
/**
 * @package Leather House Starter Sites
 * @since 1.0
 */


/**
 * Set import files
 */
if ( ! function_exists( 'leather_house_starter_sites_import_files' ) ) {

	/**
	 * Demo import files for Leather House theme
	 */
	function leather_house_starter_sites_import_files() {

		$demo_url = get_template_directory_uri();
		$demo_data_url = plugin_dir_url( '' ) . 'wpfrank-companion/inc/leather-house/demo-data';

		// Get current active theme name (lowercase for reliable match)
		$activate_theme_data = wp_get_theme();
		$activate_theme      = strtolower( $activate_theme_data->get('Name') );

		$demo_files = array();

		// Show Leather House demo only if that theme is active
		if ( $activate_theme === 'leather house' ) {
			$demo_files[] = array(
				'import_file_name'           => esc_html__( 'Leather House', 'leather-house' ),
				'categories'                 => array( 'Free Demos' ),
				'import_file_url'            => $demo_data_url . '/leather-house/leather-house.xml',
				'import_widget_file_url'     => $demo_data_url . '/leather-house/leather-house.wie',
				'import_customizer_file_url' => $demo_data_url . '/leather-house/leather-house.dat',
				'import_preview_image_url'   => $demo_url . '/screenshot.png',
				'is_active_theme'            => true,
			);
		}

		// Show Modern House demo only if that theme is active
		if ( $activate_theme === 'modern house' ) {
			$demo_files[] = array(
				'import_file_name'           => esc_html__( 'Modern House', 'leather-house' ),
				'categories'                 => array( 'Free Demos' ),
				'import_file_url'            => $demo_data_url . '/modern-house/modern-house.xml',
				'import_widget_file_url'     => $demo_data_url . '/modern-house/modern-house.wie',
				'import_customizer_file_url' => $demo_data_url . '/modern-house/modern-house.dat',
				'import_preview_image_url'   => $demo_url . '/assets/images/demo-screenshots/modern-house-premium.png',
				'is_active_theme'            => true,
			);
		}

		// Show Modern House demo only if that theme is active
		if ( $activate_theme === 'newscraft' ) {
			$demo_files[] = array(
				'import_file_name'           => esc_html__( 'Newscraft', 'leather-house' ),
				'categories'                 => array( 'Free Demos' ),
				'import_file_url'            => $demo_data_url . '/newscraft/newscraft.xml',
				'import_widget_file_url'     => $demo_data_url . '/newscraft/newscraft.wie',
				'import_customizer_file_url' => $demo_data_url . '/newscraft/newscraft.dat',
				'import_preview_image_url'   => $demo_url . '/assets/images/demo-screenshots/newscraft-premium.png',
				'is_active_theme'            => true,
			);
		}		

		// Always show Leather House Premium
		$demo_files[] = array(
			'import_file_name'           => esc_html__( 'Leather House Premium', 'leather-house' ),
			'categories'                 => array( 'Pro Demos' ),
			'preview_url'                => 'https://wpfrank.com/demo/leather-house-premium/',
			'import_preview_image_url'   => $demo_url . '/assets/images/demo-screenshots/leather-house-premium.png',
			'is_active_theme'            => ( $activate_theme === 'leather house premium' ),
		);

		// Always show Leather House Premium
		$demo_files[] = array(
			'import_file_name'           => esc_html__( 'Modern House Premium', 'leather-house' ),
			'categories'                 => array( 'Pro Demos' ),
			'preview_url'                => 'https://wpfrank.com/demo/modern-house-pro/',
			'import_preview_image_url'   => $demo_url . '/assets/images/demo-screenshots/modern-house-premium.png',
			'is_active_theme'            => ( $activate_theme === 'leather house premium' ),
		);

		return $demo_files;
	}
}
add_filter( 'pt-ocdi/import_files', 'leather_house_starter_sites_import_files' );

/**
 * Define actions that happen after import
 */
if ( !function_exists( 'leather_house_starter_sites_after_import_mods' ) ) {

	function leather_house_starter_sites_after_import_mods() {

		//Assign the menu.
		$main_menu = get_term_by( 'name', 'Primary Menu', 'nav_menu' );
		set_theme_mod( 'nav_menu_locations', array(
				'primary' => $main_menu->term_id,
			)
		);

		// Now ensure we have a Home page and assign it as front page
		$home = get_page_by_title( 'Home' );
		if ( ! $home ) {
			$home_id = wp_insert_post( array(
				'post_title'   => 'Home',
				'post_name'    => 'home',
				'post_status'  => 'publish',
				'post_type'    => 'page',
				'post_content' => '',
			) );
		} else {
			$home_id = $home->ID;
		}

		// Set the custom Frontpage Template
		update_post_meta( $home_id, '_wp_page_template', 'templates/frontpage-template.php' );

		// Make “Home” the static front page
		update_option( 'show_on_front', 'page' );
		update_option( 'page_on_front',  $home_id );

	}

}
add_action( 'pt-ocdi/after_import', 'leather_house_starter_sites_after_import_mods' );


// Custom CSS for OCDI plugin.
function leather_house_starter_sites_ocdi_css() { ?>
	<style >
		.ocdi__gl-item:nth-child(n+2) .ocdi__gl-item-buttons .button-primary, .ocdi .ocdi__theme-about, .ocdi__intro-text {
			display: none;
		}
		.ocdi__gl-item-image-container::after {
			padding-top: 75% !important;
		}

	</style>
	<?php
}
add_action( 'admin_enqueue_scripts', 'leather_house_starter_sites_ocdi_css' );

// Change the "One Click Demo Import" name from "Starter Sites" in Appearance menu.
function leather_house_starter_sites_ocdi_plugin_page_setup( $default_settings ) {

	$default_settings['parent_slug'] = 'themes.php';
	$default_settings['page_title']  = esc_html__( 'Leather House One Click Demo Import', 'leather-house' );
	$default_settings['menu_title']  = esc_html__( 'Leather House Starter Sites', 'leather-house' );
	$default_settings['capability']  = 'import';
	$default_settings['menu_slug']   = 'one-click-demo-import';

	return $default_settings;

}
add_filter( 'ocdi/plugin_page_setup', 'leather_house_starter_sites_ocdi_plugin_page_setup' );

// Register required plugins for the demos.
function leather_house_starter_sites_register_plugins( $plugins ) {

	// Get current active theme name (lowercase for reliable match)
	$activate_theme_data = wp_get_theme();
	$activate_theme      = strtolower( $activate_theme_data->get('Name') );

	// List of plugins used by all theme demos.
	$theme_plugins = array();

	if ( $activate_theme === 'leather house' ) {
		$theme_plugins[] = array(
			'name'     => 'WooCommerce',
			'slug'     => 'woocommerce',
			'required' => true,
		);
	}
		$theme_plugins[] = array(
			'name'     => 'WPFrank Companion',
			'slug'     => 'wpfrank-companion',
			'required' => true,
		);
		$theme_plugins[] = array(
			'name'     => 'Lead Generation Form',
			'slug'     => 'lead-generation-form',
			'required' => true,
		);
		$theme_plugins[] = array(
			'name'     => 'Login Page Customizer',
			'slug'     => 'customizer-login-page',
			'required' => true,
		);


	return $theme_plugins;

}
add_filter( 'ocdi/register_plugins', 'leather_house_starter_sites_register_plugins' );


/**
* Remove branding
*/
// add_filter( 'pt-ocdi/disable_pt_branding', '__return_true' );
