<?php
namespace Jet_Engine_Divi_Views\Components\Jet_Engine\Listing;

use ET\Builder\FrontEnd\BlockParser\BlockParserBlock;
use ET\Builder\FrontEnd\BlockParser\BlockParserStore;
use ET\Builder\FrontEnd\Assets\StaticCSS;
use ET\Builder\FrontEnd\Module\Style;
use Jet_Engine_Divi_Views\Components\Jet_Engine\Layout_Manager;

// Prevent direct access to the file.
if ( ! defined( 'WPINC' ) ) {
	die;
}

/**
 * Listing item renderer
 * Connects actual listing item content with Divi Builder
 */
class Renderer {

	protected $listing_id;

	public static $rendered_styles = [];

	/**
	 * Constructor
	 */
	public function __construct( $listing_id ) {
		$this->listing_id = $listing_id;
	}

	/**
	 * Get listing content callback
	 *
	 * @return string
	 */
	public function get_content() {

		$post = get_post( $this->listing_id );

		BlockParserStore::set_layout( [
			'id'   => $this->listing_id,
			'type' => Layout_Manager::get_layout_name( $this->listing_id ),
		] );

		do_action( 'jet-engine-divi-views/listing/before-item-content', $post );

		$listing_content = $this->get_parsed_divi_conent( $post->post_content );

		$listing_content = apply_filters(
			'jet-engine-divi-views/listing/item-content',
			$listing_content,
			$post
		);

		BlockParserBlock::reset_order_index();
		BlockParserStore::reset_layout();

		return $listing_content;
	}

	/**
	 * Get parsed Divi content
	 *
	 * @param string $content Content to parse.
	 *
	 * @return string
	 */
	public function get_parsed_divi_conent( $content ) {

		StaticCSS::begin_wp_editor_template( $this->listing_id );

		foreach ( StaticCSS::$wp_editor_template as $index => $template ) {
			if ( $template['id'] === $this->listing_id ) {
				StaticCSS::$wp_editor_template[ $index ]['type'] = Layout_Manager::get_layout_name( $this->listing_id );
			}
		}

		/**
		 * If we haven't rendered cached styles yet,
		 * we need to check - if StaticCSS::$styles_manager->enqueued is true,
		 * Divi will not redner our style, so we need to force StaticCSS::$styles_manager->enqueued
		 * to false and than restore.
		 */
		$styles_cache = Layout_Manager::get_layout_cache( $this->listing_id );
		$enqueued = null;

		if ( StaticCSS::$styles_manager && ! $styles_cache ) {

			$enqueued = StaticCSS::$styles_manager->enqueued;

			if ( $enqueued ) {
				StaticCSS::$styles_manager->enqueued = false;
			}
		}

		$content = do_blocks( $content );
		$content = $this->get_inline_styles() . $content;

		if ( StaticCSS::$styles_manager && $enqueued && ! $styles_cache ) {
			StaticCSS::$styles_manager->enqueued = $enqueued;
		}

		StaticCSS::end_wp_editor_template();

		return $content;
	}

	/**
	 * Get inline styles for listing item
	 *
	 * @return string
	 */
	public function get_inline_styles() {

		if ( ! empty( self::$rendered_styles[ $this->listing_id ] ) ) {
			return '';
		}

		$styles = Layout_Manager::get_layout_cache( $this->listing_id );

		if ( ! $styles ) {
			$styles = Style::render( 'critical', 'preset', $this->listing_id ) . Style::render( 'critical', 'module', $this->listing_id );
			$styles .= Style::render( 'default', 'preset', $this->listing_id ) . Style::render( 'default', 'module', $this->listing_id );

			Layout_Manager::set_layout_cache( $this->listing_id, $styles );
		}


		self::$rendered_styles[ $this->listing_id ] = true;

		return '<style>' . $styles . '</style>';
	}
}
