<?php

namespace Prodigy\Includes;

use Prodigy\Includes\Frontend\Prodigy_Product_Template_Builder;
use Prodigy\Includes\Frontend\Prodigy_Product_Template_Item;
use Prodigy\Includes\Helpers\Prodigy_Page;
use Prodigy\Includes\Helpers\Prodigy_Url_Helper;

defined( 'ABSPATH' ) || exit;

/**
 * Template Loader
 *
 * @version    2.3.0
 * @package    Prodigy
 * @subpackage Prodigy/includes
 */
class Prodigy_Elementor_Template_Loader {

	/**
	 * @return bool
	 */
	public static function is_exclude_shop_template(): bool {
		if ( ! is_page( Prodigy_Page::prodigy_get_page_id( 'shop' ) ) ) {
			return false;
		}

		$elementor_options = get_option( 'elementor_pro_theme_builder_conditions' );
		if ( ! isset( $elementor_options['archive'] ) || ! is_array( $elementor_options['archive'] ) ) {
			return false;
		}

		$exclude = false;
		foreach ( $elementor_options['archive'] as $elementor_option ) {
			if ( ! preg_grep( '/exclude\/product_archive/', $elementor_option ) ) {
				continue;
			}
			if ( preg_grep( '/^exclude\/product_archive\/shop_page$/', $elementor_option ) ) {
				$exclude = true;
				break;
			}
		}

		return $exclude;
	}

	/**
	 * @return bool
	 */
	public static function is_include_header_template(): bool {
		if ( ! function_exists( 'elementor_theme_do_location' ) ) {
			return false;
		}

		$elementor_options = get_option( 'elementor_pro_theme_builder_conditions' );
		if ( ! isset( $elementor_options['header'] ) || ! is_array( $elementor_options['header'] ) ) {
			return false;
		}

		$include = false;

		foreach ( $elementor_options['header'] as $elementor_option ) {
			if ( preg_grep( '/^include\/general$/', $elementor_option ) ) {
				$include = true;
			}
		}

		return $include;
	}


	/**
	 * @return bool
	 */
	public static function is_include_shop_template(): bool {
		if ( ! is_page( Prodigy_Page::prodigy_get_page_id( 'shop' ) ) ) {
			return false;
		}

		$elementor_options = get_option( 'elementor_pro_theme_builder_conditions' );
		if ( ! isset( $elementor_options['archive'] ) || ! is_array( $elementor_options['archive'] ) ) {
			return false;
		}

		$include = false;
		foreach ( $elementor_options['archive'] as $elementor_option ) {
			if ( ! preg_grep( '/include\/product_archive/', $elementor_option ) ) {
				continue;
			}
			if ( preg_grep( '/^include\/product_archive\/shop_page$/', $elementor_option ) ) {
				$include = true;
				break;
			}
		}

		return $include;
	}

	/**
	 * @return bool
	 */
	public static function is_exclude_archive_template(): bool {
		// Validate taxonomy terms before checking Elementor conditions
		self::validate_elementor_taxonomy_terms();

		if ( ! is_post_type_archive( Prodigy::get_prodigy_product_type() )
			&& ! is_tax( Prodigy::get_prodigy_category_type() )
			&& ! is_tax( Prodigy::get_prodigy_tag_type() )
			&& ! is_tax( Prodigy::get_prodigy_brand_type() )
			&& ! is_page( Prodigy_Page::prodigy_get_page_id( 'shop' ) )
		) {
			return false;
		}

		$elementor_options = get_option( 'elementor_pro_theme_builder_conditions' );
		if ( ! isset( $elementor_options['archive'] ) || ! is_array( $elementor_options['archive'] ) ) {
			return false;
		}

		$exclude = false;
		foreach ( $elementor_options['archive'] as $elementor_option ) {
			if ( ! preg_grep( '/exclude\/product_archive/', $elementor_option ) ) {
				continue;
			}
			if ( preg_grep( '/^exclude\/product_archive$/', $elementor_option )
				|| preg_grep( '/^exclude\/product_archive\/shop_page$/', $elementor_option )
				|| preg_grep( '/^exclude\/product_archive\/' . Prodigy::get_prodigy_category_type() . '$/', $elementor_option )
				|| preg_grep( '/^exclude\/product_archive\/' . Prodigy::get_prodigy_tag_type() . '$/', $elementor_option )
				|| preg_grep( '/^exclude\/product_archive\/' . Prodigy::get_prodigy_brand_type() . '$/', $elementor_option )
			) {
				$exclude = true;
				break;
			}

			$categories = preg_grep(
				'/include\/product_archive\/' . Prodigy::get_prodigy_category_type() . '(.*)\/(\d)/',
				$elementor_option
			);
			$categories = preg_replace(
				'/include\/product_archive\/' . Prodigy::get_prodigy_category_type() . '(.*)\//',
				'',
				$categories
			);
			$tags       = preg_grep(
				'/include\/product_archive\/' . Prodigy::get_prodigy_tag_type() . '(.*)\/(\d)/',
				$elementor_option
			);
			$tags       = preg_replace(
				'/include\/product_archive\/' . Prodigy::get_prodigy_tag_type() . '(.*)\//',
				'',
				$tags
			);

			$brands = preg_grep(
				'/include\/product_archive\/' . Prodigy::get_prodigy_brand_type() . '(.*)\/(\d)/',
				$elementor_option
			);
			$brands = preg_replace(
				'/include\/product_archive\/' . Prodigy::get_prodigy_brand_type() . '(.*)\//',
				'',
				$brands
			);

			if ( ( $categories && is_tax( PRODIGY::get_prodigy_category_type(), $categories ) )
				|| ( $tags && is_tax( Prodigy::get_prodigy_tag_type(), $tags ) )
				|| ( $brands && is_tax( Prodigy::get_prodigy_brand_type(), $brands ) )
			) {
				$exclude = true;
				break;
			}
		}

		return $exclude;
	}

	/**
	 * Validate taxonomy terms for Elementor templates
	 */
	private static function validate_elementor_taxonomy_terms(): void {
		// Only validate if we're on a taxonomy page
		if ( ! is_tax( Prodigy::get_prodigy_category_type() )
			&& ! is_tax( Prodigy::get_prodigy_tag_type() )
			&& ! is_tax( Prodigy::get_prodigy_brand_type() ) ) {
			return;
		}

		if ( ! empty( Prodigy_Url_Helper::prodigy_get_request_uri() ) ) {
			Prodigy_Url_Helper::validate_prodigy_taxonomy_url();
		}
	}

	/**
	 * @return bool
	 */
	public static function is_include_archive_template(): bool {
		// Validate taxonomy terms before checking Elementor conditions
		self::validate_elementor_taxonomy_terms();

		if ( ! is_post_type_archive( Prodigy::get_prodigy_product_type() )
			&& ! is_tax( Prodigy::get_prodigy_category_type() )
			&& ! is_tax( Prodigy::get_prodigy_tag_type() )
			&& ! is_tax( Prodigy::get_prodigy_brand_type() )
			&& ! is_page( Prodigy_Page::prodigy_get_page_id( 'shop' ) )
		) {
			return false;
		}

		$elementor_options = get_option( 'elementor_pro_theme_builder_conditions' );
		if ( ! isset( $elementor_options['archive'] ) || ! is_array( $elementor_options['archive'] ) ) {
			return false;
		}

		$include = false;
		foreach ( $elementor_options['archive'] as $elementor_option ) {
			if ( ! preg_grep( '/include\/product_archive/', $elementor_option ) ) {
				continue;
			}
			if ( preg_grep( '/^include\/product_archive$/', $elementor_option )
				|| preg_grep( '/^include\/product_archive\/shop_page$/', $elementor_option )
				|| preg_grep( '/^include\/product_archive\/' . Prodigy::get_prodigy_category_type() . '$/', $elementor_option )
				|| preg_grep( '/^include\/product_archive\/' . Prodigy::get_prodigy_tag_type() . '$/', $elementor_option )
				|| preg_grep( '/^include\/product_archive\/' . Prodigy::get_prodigy_brand_type() . '$/', $elementor_option )
			) {
				$include = true;
				break;
			}
			$categories = preg_grep(
				'/include\/product_archive\/' . Prodigy::get_prodigy_category_type() . '(.*)\/(\d)/',
				$elementor_option
			);
			$categories = preg_replace(
				'/include\/product_archive\/' . Prodigy::get_prodigy_category_type() . '(.*)\//',
				'',
				$categories
			);
			$tags       = preg_grep(
				'/include\/product_archive\/' . Prodigy::get_prodigy_tag_type() . '(.*)\/(\d)/',
				$elementor_option
			);
			$tags       = preg_replace(
				'/include\/product_archive\/' . Prodigy::get_prodigy_tag_type() . '(.*)\//',
				'',
				$tags
			);

			$brands = preg_grep(
				'/include\/product_archive\/' . Prodigy::get_prodigy_brand_type() . '(.*)\/(\d)/',
				$elementor_option
			);
			$brands = preg_replace(
				'/include\/product_archive\/' . Prodigy::get_prodigy_brand_type() . '(.*)\//',
				'',
				$brands
			);

			if ( ( $categories && is_tax( Prodigy::get_prodigy_category_type(), $categories ) )
				|| ( $tags && is_tax( Prodigy::get_prodigy_tag_type(), $tags ) )
				|| ( $tags && is_tax( Prodigy::get_prodigy_brand_type(), $tags ) )
			) {
				$include = true;
				break;
			}
		}

		return $include;
	}

	/**
	 * @return bool
	 */
	public static function is_include_single_template(): bool {
		if ( ! is_singular( Prodigy::get_prodigy_product_type() ) ) {
			return false;
		}

		global $post, $prodigy_product;
		$elementor_options = get_option( 'elementor_pro_theme_builder_conditions' );
		if ( ! isset( $elementor_options['single'] ) || ! is_array( $elementor_options['single'] ) ) {
			return false;
		}
		if ( empty( $prodigy_product ) || ( isset( $prodigy_product->id ) && $prodigy_product->get_id() !== $post->ID ) ) {
			$prodigy_product = self::prodigy_get_product( $post );
		}
		$include = false;
		foreach ( $elementor_options['single'] as $elementor_option ) {
			if ( ! preg_grep( '/include\/prodigy-product/', $elementor_option ) ) {
				continue;
			}
			if ( preg_grep( '/^include\/prodigy-product$/', $elementor_option )
				|| preg_grep( '/^include\/prodigy-product\/in_' . Prodigy::get_prodigy_tag_type() . '$/', $elementor_option )
				|| preg_grep( '/^include\/prodigy-product\/in_' . Prodigy::get_prodigy_category_type() . '$/', $elementor_option )
				|| preg_grep( '/^include\/prodigy-product\/in_' . Prodigy::get_prodigy_brand_type() . '$/', $elementor_option ) ) {
				$include = true;
				break;
			}
			$categories = preg_grep( '/include\/prodigy-product\/in_' . Prodigy::get_prodigy_category_type() . '(.*)\/(\d)/', $elementor_option );
			$categories = is_array( $categories ) ? preg_replace( '/include\/prodigy-product\/in_' . Prodigy::get_prodigy_category_type() . '(.*)\//', '', $categories ) : array();

			$tags = preg_grep( '/include\/prodigy-product\/in_' . Prodigy::get_prodigy_tag_type() . '(.*)\/(\d)/', $elementor_option );
			$tags = is_array( $tags ) ? preg_replace( '/include\/prodigy-product\/in_' . Prodigy::get_prodigy_tag_type() . '(.*)\//', '', $tags ) : array();

			$brands = preg_grep( '/include\/prodigy-product\/in_' . Prodigy::get_prodigy_brand_type() . '(.*)\/(\d)/', $elementor_option );
			$brands = is_array( $brands ) ? preg_replace( '/include\/prodigy-product\/in_' . Prodigy::get_prodigy_brand_type() . '(.*)\//', '', $brands ) : array();

			$match_cat   = ! empty( $categories ) && self::post_has_any_terms( $categories, Prodigy::get_prodigy_category_type(), $post );
			$match_tag   = ! empty( $tags ) && self::post_has_any_terms( $tags, Prodigy::get_prodigy_tag_type(), $post );
			$match_brand = ! empty( $brands ) && self::post_has_any_terms( $brands, Prodigy::get_prodigy_brand_type(), $post );

			if ( $match_cat || $match_tag || $match_brand ) {
				$include = true;
				break;
			}
		}

		return $include;
	}

	/**
	 * Check if post has any of the given term IDs for the given taxonomy.
	 *
	 * @param array    $term_ids Term IDs (strings or ints).
	 * @param string   $taxonomy Taxonomy name.
	 * @param \WP_Post $post     Post object.
	 * @return bool
	 */
	private static function post_has_any_terms( array $term_ids, string $taxonomy, $post ): bool {
		foreach ( $term_ids as $term_id ) {
			$term_id = is_numeric( $term_id ) ? (int) $term_id : 0;
			if ( $term_id > 0 && has_term( $term_id, $taxonomy, $post ) ) {
				return true;
			}
		}
		return false;
	}

	/**
	 * @return bool
	 */
	public static function is_exclude_single_template(): bool {
		if ( ! is_singular( Prodigy::get_prodigy_product_type() ) ) {
			return false;
		}

		global $post, $prodigy_product;
		$elementor_options = get_option( 'elementor_pro_theme_builder_conditions' );
		if ( ! isset( $elementor_options['single'] ) || ! is_array( $elementor_options['single'] ) ) {
			return false;
		}
		if ( empty( $prodigy_product ) ) {
			$product_template = new Prodigy_Product_Template_Builder( new Prodigy_Product_Template_Item() );
			$prodigy_product  = $product_template->set_product( (int) ( $post->ID ?? 0 ) );
		}
		$exclude = false;
		foreach ( $elementor_options['single'] as $elementor_option ) {
			if ( ! preg_grep( '/exclude\/prodigy-product/', $elementor_option ) ) {
				continue;
			}
			if ( preg_grep( '/^include\/prodigy-product$/', $elementor_option )
				|| preg_grep( '/^include\/prodigy-product\/in_' . Prodigy::get_prodigy_tag_type() . '$/', $elementor_option )
				|| preg_grep( '/^include\/prodigy-product\/in_' . Prodigy::get_prodigy_category_type() . '$/', $elementor_option )
				|| preg_grep( '/^include\/prodigy-product\/in_' . Prodigy::get_prodigy_brand_type() . '$/', $elementor_option ) ) {
				$include = true;
				break;
			}
			$categories = preg_grep( '/include\/prodigy-product\/in_' . Prodigy::get_prodigy_category_type() . '(.*)\/(\d)/', $elementor_option );
			$categories = is_array( $categories ) ? preg_replace( '/include\/prodigy-product\/in_' . Prodigy::get_prodigy_category_type() . '(.*)\//', '', $categories ) : array();

			$tags = preg_grep( '/include\/prodigy-product\/in_' . Prodigy::get_prodigy_tag_type() . '(.*)\/(\d)/', $elementor_option );
			$tags = is_array( $tags ) ? preg_replace( '/include\/prodigy-product\/in_' . Prodigy::get_prodigy_tag_type() . '(.*)\//', '', $tags ) : array();

			$brands = preg_grep( '/include\/prodigy-product\/in_' . Prodigy::get_prodigy_brand_type() . '(.*)\/(\d)/', $elementor_option );
			$brands = is_array( $brands ) ? preg_replace( '/include\/prodigy-product\/in_' . Prodigy::get_prodigy_brand_type() . '(.*)\//', '', $brands ) : array();

			$match_cat   = ! empty( $categories ) && self::post_has_any_terms( $categories, Prodigy::get_prodigy_category_type(), $post );
			$match_tag   = ! empty( $tags ) && self::post_has_any_terms( $tags, Prodigy::get_prodigy_tag_type(), $post );
			$match_brand = ! empty( $brands ) && self::post_has_any_terms( $brands, Prodigy::get_prodigy_brand_type(), $post );

			if ( $match_cat || $match_tag || $match_brand ) {
				$exclude = true;
				break;
			}
		}

		return $exclude;
	}

	/**
	 * @return bool
	 */
	public static function is_elementor_page(): bool {
		return false;
		$shop_page_id = Prodigy_Page::prodigy_get_page_id( 'shop' );
		if ( ! is_page( $shop_page_id ) ) {
			return false;
		}
		if ( get_post_meta( $shop_page_id, '_elementor_edit_mode', true ) === 'builder' ) {
			return true;
		}

		return false;
	}

	/**
	 * @return bool
	 */


	/**
	 * @return bool
	 */
	public static function is_use_archive_template(): bool {
		return self::is_include_archive_template() || self::is_exclude_archive_template()
				|| self::is_include_shop_template() || self::is_exclude_shop_template();
	}

	/**
	 * @return bool
	 */
	public static function is_use_product_template(): bool {
		return ( self::is_include_single_template() || self::is_exclude_single_template() );
	}

	/**
	 * @param array|null $options
	 *
	 * @return bool
	 */
	public static function is_live_mode( array $options = null ): ?bool {
		return isset( $options );
	}

	/**
	 * @param \WP_Post $post
	 *
	 * @return false|Prodigy_Product_Template_Item
	 */
	public static function prodigy_get_product( \WP_Post $post ) {
		$product_id = self::prodigy_get_product_id( $post );

		if ( ! $product_id ) {
			return false;
		}

		$product_template = new Prodigy_Product_Template_Builder( new Prodigy_Product_Template_Item() );

		return $product_template->set_product( (int) $product_id );
	}

	/**
	 * @param \WP_Post $product
	 *
	 * @return false|float|int
	 */
	public static function prodigy_get_product_id( \WP_Post $product ) {
		global $post;

		if ( isset( $post, $post->ID ) && get_post_type( $post->ID ) === Prodigy::PRODIGY_POST_TYPE_DEFAULT ) {
			return absint( $post->ID );
		}

		if ( ! empty( $product->ID ) ) {
			return $product->ID;
		}

		return false;
	}
}
