<?php
namespace Jet_Engine_Divi_Views;

use ET\Builder\FrontEnd\BlockParser\BlockParserStore;
use ET\Builder\Packages\Module\Module;
use ET\Builder\Packages\IconLibrary\IconFont\Utils;
use ET\Builder\Framework\Utility\HTMLUtility;

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

/**
 * Module render container class
 */
class Module_Render {

	/**
	 * @return void
	 */
	public function hook_icons_renderer() {
		add_filter( 'jet-engine/tools/pre-render-icon', [ $this, 'render_icon' ], 0, 2 );
	}

	/**
	 * @return void
	 */
	public function unhook_icons_renderer() {
		remove_filter( 'jet-engine/tools/pre-render-icon', [ $this, 'render_icon' ], 0, 2 );
	}

	/**
	 * @param $result
	 * @param $icon_data
	 * @return false|mixed|string
	 * @throws \Exception
	 */
	public function render_icon( $result = false, $icon_data = [] ) {
		if ( is_array( $icon_data['icon'] ) && ! empty( $icon_data['icon']['type'] ) ) {
			$icon_class = ! empty( $icon_data['icon_class'] ) ? esc_attr( $icon_data['icon_class'] ) : '';
			switch ( $icon_data['icon']['type'] ) {
				case 'fa':
					$font = 'FontAwesome';
					break;

				default:
					$font = 'ETmodules';
					break;
			}
			$result = HTMLUtility::render(
				[
					'tag'               => 'div',
					'attributes'        => [
						'class'     => $icon_class . ' et-pb-icon',
						'style'     => 'font-family: ' . $font,
						'data-icon' => Utils::process_font_icon( $icon_data['icon'] ),
					],
					'childrenSanitizer' => 'esc_html',
					'children'          => Utils::process_font_icon( $icon_data['icon'] ),
				]
			);
		}

		return $result;
	}

	/**
	 * Setup current object for the preview
	 */
	public function setup_preview_object( $render ) {

		$listing    = isset( $_REQUEST['listing'] ) ? $_REQUEST['listing'] : false;
		$listing_id = isset( $_REQUEST['post_id'] ) ? absint( $_REQUEST['post_id'] ) : false;
		$object_id  = isset( $_REQUEST['object'] ) ? absint( $_REQUEST['object'] ) : jet_engine()->listings->data->get_current_object();

		if ( ! $listing_id ) {
			$listing_id = jet_engine()->blocks_views->render->get_current_listing_id();
		}

		if ( $listing_id ) {
			$render->setup_listing( $listing, $object_id, true, $listing_id );
		} else {
			$render->setup_listing( $listing, $object_id );
		}
	}

	/**
	 * @param $render
	 * @param $attrs
	 * @param $block
	 * @param $elements
	 * @param $module_class
	 * @return string
	 */
	public function render_content( $render, $attrs, $block, $elements, $module_class ) {

		if ( ! $render || ! is_object( $render ) || ! method_exists( $render, 'setup_listing' ) || ! method_exists( $render, 'get_content' ) ) {
			return '';
		}

		$this->setup_preview_object( $render );

		$this->hook_icons_renderer();
		$content = $render->get_content();
		$this->unhook_icons_renderer();

		$parent       = BlockParserStore::get_parent( $block->parsed_block['id'], $block->parsed_block['storeInstance'] );
		$parent_attrs = $parent->attrs ?? [];

		return Module::render(
			[
				// FE only.
				'orderIndex'          => $block->parsed_block['orderIndex'],
				'storeInstance'       => $block->parsed_block['storeInstance'],

				// VB equivalent.
				'id'                  => $block->parsed_block['id'],
				'name'                => $block->block_type->name,
				'moduleCategory'      => $block->block_type->category,
				'attrs'               => $attrs,
				'elements'            => $elements,
				'classnamesFunction'  => [ $module_class, 'module_classnames' ],
				'stylesComponent'     => [ $module_class, 'module_styles' ],
				'scriptDataComponent' => [ $module_class, 'module_script_data' ],
				'parentAttrs'         => $parent_attrs,
				'parentId'            => $parent->id ?? '',
				'parentName'          => $parent->blockName ?? '',
				'children'            => [ $content ],
			]
		);
	}
}
