<?php

namespace ElementPack\Includes\Builder;

if ( ! defined( 'WPINC' ) ) {
	die;
}

use Elementor\Plugin;
use Elementor\Controls_Manager;
use Elementor\Core\DocumentTypes\PageBase;
use ElementPack\Base\Singleton;
use ElementPack\Includes\Builder\Meta;
use ElementPack\Includes\Builder\Builder_Template_Helper;
use ElementPack\Includes\Builder\Builder_Post_Singleton;
use ElementPack\Includes\Controls\SelectInput\Dynamic_Select;


class Builder_Integration {

	use Singleton;

	private $current_template = null;
	public $current_template_id = null;

	function __construct() {
		add_filter( 'template_include', [ $this, 'set_builder_template' ], 9999 );
		add_action( 'elementor/editor/init', [ $this, 'set_sample_post' ], 999 );

		add_action( 'print_default_editor_scripts', array( $this, 'my_custom_fonts' ) );

		add_action( 'elementor/documents/register_controls', [ $this, 'register_document_controls' ] );
	}

	public function my_custom_fonts() {
		if ( is_admin() && Plugin::instance()->editor->is_edit_mode() ) {
			if ( isset( $_REQUEST['bdt-template'] ) ) {
				wp_register_style( 'bdt-template-builder-hide-preview-btn-inline', false ); // phpcs:ignore
				wp_enqueue_style( 'bdt-template-builder-hide-preview-btn-inline' );
				wp_add_inline_style(
					'bdt-template-builder-hide-preview-btn-inline',
					'#elementor-panel-footer-saver-preview {display:none!important}'
				);
			}
		}
	}
	function set_sample_post() {
		if ( Builder_Template_Helper::isTemplateEditMode() ) {
			$object = Builder_Post_Singleton::instance();
			$object::set_sample_post();
		}
	}

	function register_document_controls( $document ) {
		if ( ! $document instanceof PageBase || ! $document::get_property( 'has_elements' ) ) {
			return;
		}

		if ( Plugin::instance()->preview->is_preview_mode() )
			return;

		if ( ! Builder_Template_Helper::isTemplateEditMode() ) {
			return;
		}

		global $post;

		if ( ! isset( $post->ID ) ) {
			return;
		}
		$meta = get_post_meta( $post->ID );

		$templateMeta = optional( $meta )[ Meta::TEMPLATE_TYPE ];
		if ( ! isset( $templateMeta[0] ) ) {
			return;
		}
		$postMeta = $templateMeta[0];
		$postMeta = explode( '|', $postMeta );
		$postType = $postMeta[0];

		if ( 'single' != $postMeta[1] ) {
			return;
		}

		$document->start_controls_section(
			'bdt_page_setting_preview',
			[ 
				'label' => esc_html__( 'Builder Settings', 'bdthemes-element-pack' ),
				'tab'   => Controls_Manager::TAB_SETTINGS,
			]
		);

		$document->add_control(
			'bdt_builder_sample_post_id',
			[ 
				'label'       => esc_html__( 'Builder Post', 'bdthemes-element-pack' ),
				'type'        => Dynamic_Select::TYPE,
				'multiple'    => false,
				'label_block' => true,
				'query_args'  => [ 
					'post_type' => $postType
				],
			]
		);

		$document->add_control(
			'bdt_builder_sample_apply_preview',
			[ 
				'type'        => Controls_Manager::BUTTON,
				'label'       => esc_html__( 'Apply & Preview', 'bdthemes-element-pack' ),
				'label_block' => true,
				'show_label'  => false,
				'text'        => esc_html__( 'Apply & Preview', 'bdthemes-element-pack' ),
				'separator'   => 'none',
				'event'       => 'ElementPackBuilderSetting:applySinglePagePostOnPreview',
			]
		);

		$document->end_controls_section();
	}

	/**
	 * Rewrite default template
	 *
	 */
	function set_builder_template( $template ) {
		if ( Builder_Template_Helper::isTemplateEditMode() ) {
			return $this->setBackendTemplate( $template );
		} else {
			return $this->setFrontendTemplate( $template );
		}
	}


	protected function setBackendTemplate( $template ) {
		return $template;
	}


	protected function setFrontendTemplate( $template ) {
		// if (get_post_type() == 'product') {
		//     global $product;
		//     $product = wc_get_product();
		// }



		if ( defined( 'ELEMENTOR_PATH' ) ) {
			$elementorTem = ELEMENTOR_PATH . "modules/page-templates/templates/";
			$elementorTem = explode( $elementorTem, $template );
			if ( count( $elementorTem ) == 2 ) {
				return $template;
			}
		}

		// single posts
		if ( is_single() && 'post' == get_post_type() ) {
			//single__3168
			if ( $custom_template = $this->get_template_id( 'single', 'post' ) ) {
				$this->current_template_id = $custom_template;
				return $this->getTemplatePath( 'posts/single', $template );
			}
		}

		// archive page
		if ( ( is_archive() || is_home() ) && get_post_type( get_the_ID() ) == 'post' ) {
			if ( is_category() ) {
				if ( $custom_template = $this->get_template_id( 'category', 'post' ) ) {
					$this->current_template_id = $custom_template;
					return $this->getTemplatePath( 'posts/category', $template );
				}
			} elseif ( is_tag() ) {
				if ( $custom_template = $this->get_template_id( 'tag', 'post' ) ) {
					$this->current_template_id = $custom_template;
					return $this->getTemplatePath( 'posts/tag', $template );
				}
			} elseif ( is_author() ) {
				if ( $custom_template = $this->get_template_id( 'author', 'post' ) ) {
					$this->current_template_id = $custom_template;
					return $this->getTemplatePath( 'posts/author', $template );
				}
			} elseif ( is_date() ) {
				if ( $custom_template = $this->get_template_id( 'date', 'post' ) ) {
					$this->current_template_id = $custom_template;
					return $this->getTemplatePath( 'posts/date', $template );
				}
			} else {
				if ( $custom_template = $this->get_template_id( 'archive', 'post' ) ) {
					$this->current_template_id = $custom_template;
					return $this->getTemplatePath( 'posts/archive', $template );
				}
			}
		}

		// Pages
		if ( is_page() && ! is_page_template() && 'page' == get_post_type() ) {
			if ( $custom_template = $this->get_template_id( 'single', 'page' ) ) {
				$this->current_template_id = $custom_template;
				return $this->getTemplatePath( 'pages/single', $template );
			}
		}

		//  404 page
		if ( is_404() ) {
			if ( $custom_template = $this->get_template_id( '404', 'page' ) ) {
				$this->current_template_id = $custom_template;
				return $this->getTemplatePath( 'pages/404', $template );
			}
		}

		// search page
		if ( is_search() ) {
			if ( $custom_template = $this->get_template_id( 'search', 'page' ) ) {
				$this->current_template_id = $custom_template;
				return $this->getTemplatePath( 'pages/search', $template );
			}
		}
		// front page
		// if (is_front_page()) {
		//     if ($custom_template = $this->get_template_id('home', 'page')) {
		//         $this->current_template_id = $custom_template;
		//         return $this->getTemplatePath('pages/home', $template);
		//     }
		// }

		// Custom post types - single
		if ( is_single() && ! in_array( get_post_type(), [ 'post', 'page' ] ) ) {
			$post_type = get_post_type();
			if ( $custom_template = $this->get_template_id( 'single', $post_type ) ) {
				$this->current_template_id = $custom_template;
				// Try specific template first, then fall back to generic custom template
				if ( $template_path = $this->getTemplatePath( "custom/{$post_type}/single", '' ) ) {
					return $template_path;
				}
				return $this->getTemplatePath( "custom/single", $template );
			}
		}

		// Custom post types - archive
		if ( is_post_type_archive() && ! in_array( get_post_type(), [ 'post', 'page' ] ) ) {
			$post_type = get_post_type();
			if ( $custom_template = $this->get_template_id( 'archive', $post_type ) ) {
				$this->current_template_id = $custom_template;
				// Try specific template first, then fall back to generic custom template
				if ( $template_path = $this->getTemplatePath( "custom/{$post_type}/archive", '' ) ) {
					return $template_path;
				}
				return $this->getTemplatePath( "custom/archive", $template );
			}
		}

		// Custom post types - taxonomy archives
		if ( is_tax() ) {
			$taxonomy = get_queried_object();
			if ( $taxonomy && isset( $taxonomy->taxonomy ) ) {
				$post_types = get_taxonomy( $taxonomy->taxonomy )->object_type;
				foreach ( $post_types as $post_type ) {
					if ( ! in_array( $post_type, [ 'post', 'page' ] ) ) {
						if ( $custom_template = $this->get_template_id( $taxonomy->taxonomy, $post_type ) ) {
							$this->current_template_id = $custom_template;
							// Try specific template first, then fall back to generic custom template
							if ( $template_path = $this->getTemplatePath( "custom/{$post_type}/{$taxonomy->taxonomy}", '' ) ) {
								return $template_path;
							}
							return $this->getTemplatePath( "custom/archive", $template );
						}
					}
				}
			}
		}


		// themes header
		if ( is_page() && ! is_page_template() && 'themes' == get_post_type() ) {
			if ( $custom_template = $this->get_template_id( 'header', 'themes' ) ) {
				$this->current_template_id = $custom_template;
				return $this->getTemplatePath( 'themes/header', $template );
			}
		}

		// WooCommerce pages — only when WooCommerce is active
		if ( class_exists( 'WooCommerce' ) ) {

			// Cart page — check before checkout because is_checkout() can return true on cart too
			if ( function_exists( 'is_cart' ) && is_cart() ) {
				if ( $custom_template = $this->get_template_id( 'cart', 'product' ) ) {
					$this->current_template_id = $custom_template;
					return $this->getTemplatePath( 'woocommerce/cart', $template );
				}
			}

			// Thank You / Order Received page
			if ( function_exists( 'is_order_received_page' ) && is_order_received_page() ) {
				if ( $custom_template = $this->get_template_id( 'thankyou', 'product' ) ) {
					$this->current_template_id = $custom_template;
					return $this->getTemplatePath( 'woocommerce/thankyou', $template );
				}
			}

			// Checkout page
			if ( function_exists( 'is_checkout' ) && is_checkout() ) {
				if ( $custom_template = $this->get_template_id( 'checkout', 'product' ) ) {
					$this->current_template_id = $custom_template;
					return $this->getTemplatePath( 'woocommerce/checkout', $template );
				}
			}

			// My Account — Orders endpoint (/my-account/orders/).
			// Must come before the generic myaccount check since is_account_page() is true for all endpoints.
			if ( function_exists( 'is_account_page' ) && is_account_page() && function_exists( 'is_wc_endpoint_url' ) && is_wc_endpoint_url( 'orders' ) ) {
				if ( $custom_template = $this->get_template_id( 'orders', 'product' ) ) {
					$this->current_template_id = $custom_template;
					return $this->getTemplatePath( 'woocommerce/orders', $template );
				}
			}

			// My Account dashboard page (/my-account/).
			if ( function_exists( 'is_account_page' ) && is_account_page() ) {
				if ( $custom_template = $this->get_template_id( 'myaccount', 'product' ) ) {
					$this->current_template_id = $custom_template;
					return $this->getTemplatePath( 'woocommerce/my-account', $template );
				}
			}

			// Product Category page.
			if ( is_tax( 'product_cat' ) ) {
				if ( $custom_template = $this->get_template_id( 'category', 'product' ) ) {
					$this->current_template_id = $custom_template;
					return $this->getTemplatePath( 'woocommerce/archive-product', $template );
				}
			}

			// Product Tag page.
			if ( is_tax( 'product_tag' ) ) {
				if ( $custom_template = $this->get_template_id( 'tag', 'product' ) ) {
					$this->current_template_id = $custom_template;
					return $this->getTemplatePath( 'woocommerce/archive-product', $template );
				}
			}

			// Shop / Product Archive page.
			// is_shop() covers the main WooCommerce shop page; is_post_type_archive('product') covers
			// any additional product archive URL. Both use the same archive-product template.
			if ( ( function_exists( 'is_shop' ) && is_shop() ) || is_post_type_archive( 'product' ) ) {
				if ( $custom_template = $this->get_template_id( 'shop', 'product' ) ) {
					$this->current_template_id = $custom_template;
					return $this->getTemplatePath( 'woocommerce/archive-product', $template );
				}
			}
		}

		return $template;
	}


	public function getThemeTemplatePath( $slug ) {

		$fullPath = get_template_directory() . "/bdthemes-element-pack/$slug";
		if ( file_exists( $fullPath ) ) {
			return $fullPath;
		}
	}

	public function getPluginTemplatePath( $slug ) {

		$fullPath = BDTEP_PATH . "includes/builder/templates/$slug";
		if ( file_exists( $fullPath ) ) {
			return $fullPath;
		}
	}


	/**
	 * Get Template Path ID
	 *
	 * @param $slug
	 * @param $postType
	 *
	 * @return mixed|void|null
	 */
	public function get_template_id( $slug, $postType = false ) {

		if ( 'post' == $postType ) {
			$patterns = [
				'single' 	=> ['post', '_bdthemes_builder_post|single__'],
				'archive' 	=> ['post', '_bdthemes_builder_post|archive__'],
				'category' 	=> ['post', '_bdthemes_builder_post|category__'],
				'tag' 		=> ['post', '_bdthemes_builder_post|tag__'],
				'author' 	=> ['post', '_bdthemes_builder_post|author__'],
				'date' 		=> ['post', '_bdthemes_builder_post|date__'],
			];
		} elseif ( 'page' == $postType ) {
			$patterns = [
				'single'    => ['page', '_bdthemes_builder_page|single__'],
				'404'     	=> ['page', '_bdthemes_builder_page|404__'],
				'search'    => ['page', '_bdthemes_builder_page|search__'],
			];
		} elseif ( 'product' == $postType ) {
			$patterns = [
				'shop'      => ['product', '_bdthemes_builder_product|shop__'],
				'single'    => ['product', '_bdthemes_builder_product|single__'],
				'archive'   => ['product', '_bdthemes_builder_product|archive__'],
				'category'  => ['product', '_bdthemes_builder_product|category__'],
				'tag'       => ['product', '_bdthemes_builder_product|tag__'],
				'cart'      => ['product', '_bdthemes_builder_product|cart__'],
				'checkout'  => ['product', '_bdthemes_builder_product|checkout__'],
				'myaccount' => ['product', '_bdthemes_builder_product|myaccount__'],
				'orders'    => ['product', '_bdthemes_builder_product|orders__'],
				'thankyou'  => ['product', '_bdthemes_builder_product|thankyou__'],
			];
		} else {
			// Handle custom post types
			$patterns = [
				'single' 	=> [$postType, "_bdthemes_builder_{$postType}|single__"],
				'archive' 	=> [$postType, "_bdthemes_builder_{$postType}|archive__"],
			];
			
			// Add taxonomy patterns for custom post types
			$taxonomies = get_object_taxonomies( $postType, 'objects' );
			foreach ( $taxonomies as $taxonomy ) {
				if ( $taxonomy->public && $taxonomy->show_ui ) {
					$patterns[ $taxonomy->name ] = [$postType, "_bdthemes_builder_{$postType}|{$taxonomy->name}__"];
				}
			}
		}
	
		if (isset($patterns[$slug]) && $postType === $patterns[$slug][0]) {
			$results = Builder_Template_Helper::searchTemplateOptions($patterns[$slug][1]);
	
			$option_values = array_map(fn($result) => $result->option_value, $results);
			$slug = !empty($option_values) ? "{$slug}__" . end($option_values) : null;
		}


		$templateId                = Builder_Template_Helper::getTemplate( $slug, $postType );
		$this->current_template_id = apply_filters( 'bdthemes-templates-builder/custom-template', $templateId );

		return $this->current_template_id;
	}

	/**
	 * Get Template Path
	 *
	 * @param $slug
	 * @param $default
	 *
	 * @return mixed|string|void
	 */
	protected function getTemplatePath( $slug, $default = '' ) {
		$phpSlug = "{$slug}.php";

		if ( $template = $this->getThemeTemplatePath( $phpSlug ) ) {
			return $template;
		}

		if ( $template = $this->getPluginTemplatePath( $phpSlug ) ) {
			return $template;
		}

		return $default;
	}
}


Builder_Integration::instance();
