<?php
/**
 * Essential Classy Addons For Elementor Documents
 *
 * @package Ecafe
 * @since   3.0.24
 */
if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}

if ( ! class_exists( 'Ecafe_Elementor_Builder_Documents' ) ) {
	class Ecafe_Elementor_Builder_Documents {

		/**
		 * Instance
		 */
		private static $instance;

		/**
		 * Document Type Singular/Archives
		 */
		protected $doc_type = null;

		/**
		 * Initiator
		 */
		public static function get_instance() {
			if ( ! isset( self::$instance ) ) {
				self::$instance = new self();
			}
			return self::$instance;
		}

		/**
		 * Constructor
		 */
		public function __construct() {

			add_action( 'elementor/documents/register', array( $this, 'ec_register_elementor_documents' ) );

			add_action( 'elementor/template-library/before_get_source_data', array( $this, 'ec_preview_query_switch' ) );
			add_action( 'elementor/template-library/after_get_source_data', array( $this, 'ec_current_query_restore' ) );
			add_action( 'elementor/dynamic_tags/before_render', array( $this, 'ec_preview_query_switch' ) );
			add_action( 'elementor/dynamic_tags/after_render', array( $this, 'ec_current_query_restore' ) );

			add_filter( 'post_class', array( $this, 'ec_set_post_class' ) );
			add_filter( 'body_class', array( $this, 'ec_set_body_class' ) );
			add_filter( 'the_content', array( $this, 'ec_add_post_product_wrapper' ), 1000000 );
		}

		/**
		 * Add product wrapper to content
		 */
		public function ec_add_post_product_wrapper( $content ) {

			if ( is_singular( ECAFE_POST ) && isset( $_GET['elementor-preview'] ) ) {
				$content = sprintf( '<div class="product">%s</div>', $content );
			}

			return $content;
		}

		/**
		 * Register appropriate document types for 'ec_theme_builder' post type
		 *
		 * @param Elementor\Core\Documents_Manager $documents_manager [description]
		 */
		public function ec_register_elementor_documents( $documents_manager ) {

			$doc_path = ECAFE_CLASSES_URL . 'documents/';
			require $doc_path . 'class-ele-builder-document-base.php';
			require $doc_path . 'class-ele-document-singular.php';

			$documents_manager->register_document_type( 'ec_theme_builder', 'Ecafe_Ele_Document' );
		}

		/**
		 * Add 'product' class
		 */
		public function ec_set_post_class( $classes ) {

			if ( is_singular( ECAFE_POST ) ) {
				$classes[] = 'product';
			}

			return $classes;
		}

		/**
		 * Add 'single-product' class to body on build post
		 */
		public function ec_set_body_class( $classes ) {

			if ( is_singular( ECAFE_POST ) ) {
				$classes[] = 'woocommerce single-product';
			}

			return $classes;
		}

		/**
		 * Set document type
		 */
		public function set_current_type( $type ) {
			$this->doc_type = $type;
		}

		/**
		 * Get document type
		 */
		public function get_current_type() {
			return $this->doc_type;
		}


		/**
		 * Preview query switch
		 */
		public function ec_preview_query_switch() {

			$post_id  = get_the_ID();
			$document = Elementor\Plugin::instance()->documents->get_doc_or_auto_save( $post_id );

			if ( ! is_object( $document ) ) {
				return null;
			}

			$new_query = $this->ec_get_args_preview_query();

			if ( empty( $new_query ) ) {
				return null;
			}

			Elementor\Plugin::instance()->db->switch_to_query( $new_query );
		}

		/**
		 * Restore default query
		 */
		public function ec_current_query_restore() {
			Elementor\Plugin::instance()->db->restore_current_query();
		}

		public function preview_post_setting( $parent_post_id = '' ) {
			$preview = array();
			if ( empty( $parent_post_id ) ) {
				$post_id = get_the_ID();
			} else {
				$post_id = $parent_post_id;
			}

			if ( get_post_type() == ECAFE_POST ) {
				$template_type = get_post_meta( $post_id, 'ecafe_build_template_type', true );
				if ( $template_type == 'singular' ) {
					$singular_prev_type = get_post_meta( $post_id, 'ecafe_build_display_singular', true );
					$singular_prev_id   = get_post_meta( $post_id, 'ecafe_build_display_prev', true );
					if ( ! empty( $singular_prev_type ) && ! empty( $singular_prev_id ) ) {
						$preview['type']         = 'singular';
						$preview['preview_type'] = $singular_prev_type;
						$preview['preview_id']   = $singular_prev_id;
						$preview['preview_url']  = get_permalink( $singular_prev_id );
					}
				} elseif ( $template_type == 'archives' ) {
					$archive_prev_type = get_post_meta( $post_id, 'ecafe_build_display_archives', true );
					$archive_prev_id   = get_post_meta( $post_id, 'ecafe_build_display_prev', true );
					if ( ! empty( $archive_prev_type ) && ! empty( $archive_prev_id ) ) {
						$preview['type']         = 'archives';
						$preview['preview_type'] = $archive_prev_type;
						$preview['preview_id']   = $archive_prev_id;
						$preview['preview_url']  = get_category_link( $archive_prev_id );
					}
				}
			}

			return $preview;
		}

		public function ec_get_args_preview_query() {
			$prev_data  = $this->preview_post_setting();
			$preview_id = '';
			if ( ! empty( $prev_data ) && ! empty( $prev_data['preview_id'] ) ) {
				$preview_id = $prev_data['preview_id'];
			}

			$args = array();
			if ( isset( $prev_data['type'] ) && ! empty( $prev_data['type'] ) ) {
				switch ( $prev_data['type'] ) {
					case 'singular':
						$post = get_post( $preview_id );
						if ( ! $post ) {
							break;
						}

						$args = array(
							'p'         => $post->ID,
							'post_type' => $post->post_type,
						);
						break;
					case 'archives':
						switch ( $prev_data['preview_type'] ) {
							default:
								$get_term = get_term( $preview_id );

								if ( $get_term && ! is_wp_error( $get_term ) ) {
									$args = array(
										'tax_query' => array(
											array(
												'taxonomy' => $get_term->taxonomy,
												'terms'    => array( $preview_id ),
												'field'    => 'id',
											),
										),
									);
								}
								break;
						}
						break;
				}
			}

			if ( empty( $args ) ) {
				$args = array(
					'p'         => get_the_ID(),
					'post_type' => get_post_type(),
				);
			}

			return $args;
		}
	}
}
