<?php

/**
 * Price List Module.
 *
 * @package DiviSupreme.
 */

declare(strict_types=1);

namespace DiviSupreme\Modules;

if (!defined('ABSPATH')) {
	die('Direct access forbidden.');
}


use ET\Builder\Framework\DependencyManagement\Interfaces\DependencyInterface;
use ET\Builder\Framework\Utility\HTMLUtility;
use ET\Builder\FrontEnd\Module\Style;
use ET\Builder\Packages\Module\Module;
use ET\Builder\Packages\Module\Options\Element\ElementClassnames;
use ET\Builder\Packages\ModuleLibrary\ModuleRegistration;
use ET\Builder\FrontEnd\BlockParser\BlockParserStore;
use ET\Builder\Packages\Module\Options\Element\ElementComponents;
use ET\Builder\Packages\Module\Layout\Components\StyleCommon\CommonStyle;
use ET\Builder\Packages\IconLibrary\IconFont\Utils;
use ET\Builder\Packages\Module\Options\Css\CssStyle;



/**
 * Class that handles "PriceList Module" module.
 */
class PriceList implements DependencyInterface
{


	/**
	 * Register module.
	 * `DependencyInterface` interface ensures class method name `load()` is executed for initialization.
	 */
	public function load()
	{
		add_action('init', array(self::class, 'register_module'));
	}

	/**
	 * Initilises the module.
	 *
	 * @return void
	 */
	public static function dsm_initialise()
	{
		add_action(
			'divi_module_library_modules_dependency_tree',
			function ($dependency_tree) {
				$dependency_tree->add_dependency(new self());
			}
		);
	}

	/**
	 * Register module.
	 */
	public static function register_module()
	{

		$module_json_path = DSM_DIR_PATH . 'divi-5/public/packages/module-library/price-list';

		ModuleRegistration::register_module(
			$module_json_path,
			array(
				'render_callback' => array(self::class, 'render_callback'),
			)
		);
	}

	/**
	 * Render module style.
	 *
	 * @param array $args - Arguments.
	 */
	public static function module_styles($args)
	{
		$attrs = $args['attrs'] ?? array();
		$elements = $args['elements'];

		Style::add(
			array(
				'id' => $args['id'],
				'name' => $args['name'],
				'orderIndex' => $args['orderIndex'],
				'storeInstance' => $args['storeInstance'],
				'styles' => array(
					// Module.
					$elements->style(
						array(
							'attrName' => 'module',
							'styleProps' => array(
								'disabledOn' => array(
									'disabledModuleVisibility' => $args['settings']['disabledModuleVisibility'] ?? null,
								),
							),
						)
					),

					CssStyle::style(
						array(
							'selector' => $args['orderClass'],
							'attr' => $attrs['css'] ?? array(),
						)
					),

					$elements->style(
						array(
							'attrName' => 'title',
						)
					),

					$elements->style(
						array(
							'attrName' => 'price',
						)
					),

					$elements->style(
						array(
							'attrName' => 'content',
						)
					),

					$elements->style(
						array(
							'attrName' => 'dsm_image_styling',
						)
					),

					$elements->style(
						array(
							'attrName' => 'image_max_width',
						)
					),

					$elements->style(
						array(
							'attrName' => 'image_spacing',
						)
					),

					$elements->style(
						array(
							'attrName' => 'border-bottom-width',
						)
					),

					$elements->style(
						array(
							'attrName' => 'item_bottom_gap',
						)
					),

					$elements->style(
						array(
							'attrName' => 'dsm_price_styling',
						)
					),

					$elements->style(
						array(
							'attrName' => 'price_padding',
						)
					),

					$elements->style(
						array(
							'attrName' => 'dsm_image_styling',
						)
					),

					CommonStyle::style(
						array(
							'selector' => "{$args['orderClass']}  .dsm-pricelist-image",
							'attr' => $attrs['separator_style']['innerContent'] ?? array(),
							'property' => 'max-width',
						)
					),

					CommonStyle::style(
						array(
							'selector' => "{$args['orderClass']}  .dsm-pricelist-image",
							'attr' => $attrs['image_spacing']['innerContent'] ?? array(),
							'property' => 'margin-right',
						)
					),

					CommonStyle::style(
						array(
							'selector' => "{$args['orderClass']}  .dsm-pricelist-separator",
							'attr' => $attrs['separator_style']['innerContent'] ?? array(),
							'property' => 'border-bottom-style',
						)
					),

					CommonStyle::style(
						array(
							'selector' => "{$args['orderClass']}  .dsm-pricelist-separator",
							'attr' => $attrs['separator_weight']['innerContent'] ?? array(),
							'property' => 'border-bottom-width',
						)
					),

					CommonStyle::style(
						array(
							'selector' => "{$args['orderClass']}  .dsm-pricelist-separator",
							'attr' => $attrs['separator_color']['innerContent'] ?? array(),
							'property' => 'border-bottom-color',
						)
					),

					CommonStyle::style(
						array(
							'selector' => "{$args['orderClass']}  .dsm-pricelist-separator",
							'attr' => $attrs['separator_gap']['innerContent'] ?? array(),
							'property' => 'margin-left',
						)
					),

					CommonStyle::style(
						array(
							'selector' => "{$args['orderClass']}  .dsm-pricelist-separator",
							'attr' => $attrs['separator_gap']['innerContent'] ?? array(),
							'property' => 'margin-right',
						)
					),

					CommonStyle::style(
						array(
							'selector' => "{$args['orderClass']} .dsm_price_list_child",
							'attr' => $attrs['item_bottom_gap']['innerContent'] ?? array(),
							'property' => 'margin-bottom',
							'important' => true,
						)
					),

					CommonStyle::style(
						array(
							'selector' => "{$args['orderClass']} .dsm_price_list_child",
							'attr' => $attrs['content_orientation']['innerContent'] ?? array(),
							'property' => 'align-items',
						)
					),

					CommonStyle::style(
						array(
							'selector' => "{$args['orderClass']} .dsm-pricelist-price",
							'attr' => $attrs['price_background_color']['innerContent'] ?? array(),
							'property' => 'background-color',
						)
					),

					CommonStyle::style(
						array(
							'selector' => "{$args['orderClass']} .dsm-pricelist-image",
							'attr' => $attrs['image_max_width']['innerContent'] ?? array(),
							'property' => 'width',
						)
					),
				),
			)
		);
	}

	/**
	 * Render module script data.
	 *
	 * @param array $args - Arguments.
	 */
	public static function module_script_data($args)
	{
		$elements = $args['elements'];

		// Element Script Data Options.
		$elements->script_data(
			array(
				'attrName' => 'module',
			)
		);
	}

	/**
	 * Render module classnames.
	 *
	 * @param array $args - Arguments.
	 */
	public static function module_classnames($args)
	{
		$classnames_instance = $args['classnamesInstance'];
		$attrs = $args['attrs'];

		// Module.
		$classnames_instance->add(
			ElementClassnames::classnames(
				array(
					'attrs' => $attrs['module']['decoration'] ?? array(),
				)
			)
		);
	}

	/**
	 * Render module HTML output.
	 *
	 * @param array     $attrs - Modules Attributes.
	 * @param string    $content - Content.
	 * @param \WP_Block $block - Block.
	 * @param mixed     $elements - Elements.
	 */
	public static function render_callback($attrs, $content, $block, $elements)
	{

		$children_ids = $block->parsed_block['innerBlocks'] ? array_map(
			function ($inner_block) {
				return $inner_block['id'];
			},
			$block->parsed_block['innerBlocks']
		) : array();

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

		// Module Inner.
		// Essentially, this is the module content.
		// Were wrapping the title and content in a div with class `et_pb_module_inner`.
		$module_inner = HTMLUtility::render(
			array(
				'tag' => 'div',
				'attributes' => array(
					'class' => 'et_pb_module_inner',
				),
				'childrenSanitizer' => 'et_core_esc_previously',
				'children' => $content,
			)
		);

		// This are the module elements that will be rendered in the frontend.
		$module_elements = $elements->style_components(
			array(
				'attrName' => 'module',
			)
		);

		// This are the children of the module container, which are the module elements and the module inner.
		$module_container_children = $module_elements . $module_inner;

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

				// VB equivalent.
				'attrs' => $attrs,
				'parentAttrs' => $parent_attrs,
				'elements' => $elements,
				'id' => $block->parsed_block['id'],
				'moduleClassName' => '',
				'name' => $block->block_type->name,
				'classnamesFunction' => array(self::class, 'module_classnames'),
				'moduleCategory' => $block->block_type->category,
				'stylesComponent' => array(self::class, 'module_styles'),
				'scriptDataComponent' => array(self::class, 'module_script_data'),
				'children' => ElementComponents::component(
					array(
						'attrs' => $attrs['module']['decoration'] ?? array(),
						'id' => $block->parsed_block['id'],

						// FE only.
						'orderIndex' => $block->parsed_block['orderIndex'],
						'storeInstance' => $block->parsed_block['storeInstance'],
					)
				) . $module_container_children,
				'childrenIds' => $children_ids,
			)
		);
	}
}
