<?php

/**
 * Advanced Tabs Child 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\Packages\ModuleUtils\ChildrenUtils;
use ET\Builder\Packages\Module\Layout\Components\StyleCommon\CommonStyle;
use ET\Builder\Packages\Module\Options\Css\CssStyle;
use ET\Builder\Packages\StyleLibrary\Utils\StyleDeclarations;
use DiviSupreme\Extensions\IconLibraries\LegacyModuleIconStyles;


/**
 * Class that handles "AdvancedTabsChild Module" module.
 */
class AdvancedTabsChild 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/advanced-tabs-child';

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

	public static function tab_icon_style_declaration($params)
	{
		return LegacyModuleIconStyles::build_pseudo_icon_declaration( $params );
	}

	public static function tab_active_icon_style_declaration($params)
	{
		return LegacyModuleIconStyles::build_pseudo_icon_declaration( $params );
	}

	public static function tab_content_icon_style_declaration($params)
	{
		return LegacyModuleIconStyles::build_pseudo_icon_declaration( $params );
	}


	public static function content_image_icon_placement_declaration($params)
	{
		$attr = $params['attrValue'] ?? array();

		$style_declarations = new StyleDeclarations(
			array(
				'returnType' => 'string',
				'important' => array(
					'flex-direction' => true,
				),
			)
		);

		if ('top' === $attr) {
			$style_declarations->add('flex-direction', 'column');
		}

		return $style_declarations->value();
	}

	/**
	 * 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' => 'tab_title',
						)
					),

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

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

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

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

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

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

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

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

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

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

					// Font-icon ::before glyphs: AdvancedTabs::render_child_tab_font_icon_styles (parent render) so FE
					// still gets rules when Divi skips child stylesComponent (e.g. static CSS).

					CommonStyle::style(
						array(
							'selector' => "{$args['orderClass']} .dsm-inner-content-wrapper",
							'attr' => $attrs['dsm_content_image_icon_placement']['innerContent'] ?? array(),
							'declarationFunction' => array(static::class, 'content_image_icon_placement_declaration'),
						)
					),

					CommonStyle::style(
						array(
							'selector' => ".dsm-advanced-tabs-container {$args['orderClass']} .dsm-icon-wrapper .et-pb-icon.dsm_icon, .dsm-advanced-tabs-container {$args['orderClass']} .dsm-active-icon-wrapper .et-pb-icon.dsm-active-icon",
							'attr' => $attrs['dsm_tabs_icon_size']['innerContent'] ?? array(),
							'property' => 'font-size',
						)
					),

					CommonStyle::style(
						array(
							'selector' => ".dsm-advanced-tabs-container {$args['orderClass']} .dsm-icon-wrapper .et-pb-icon.dsm_icon",
							'attr' => $attrs['dsm_tabs_icon_color']['innerContent'] ?? array(),
							'property' => 'color',
						)
					),

					CommonStyle::style(
						array(
							'selector' => ".dsm-advanced-tabs-container {$args['orderClass']}.dsm-tab.dsm-active .dsm-active-icon-wrapper .et-pb-icon.dsm-active-icon, .dsm-advanced-tabs-container {$args['orderClass']}.dsm-tab.dsm-active .dsm-active-icon-wrapper .et-pb-icon.dsm-active-icon::before, .dsm-advanced-tabs-container {$args['orderClass']}.dsm-tab.dsm-active .dsm-icon-wrapper .et-pb-icon.dsm_icon, .dsm-advanced-tabs-container {$args['orderClass']}.dsm-tab.dsm-active .dsm-icon-wrapper .et-pb-icon.dsm_icon::before",
							'attr' => $attrs['dsm_tabs_active_icon_color']['innerContent'] ?? array(),
							'property' => 'color',
						)
					),

					CommonStyle::style(
						array(
							'selector' => ".dsm-advanced-tabs-container {$args['orderClass']}.dsm-content-wrapper .dsm-image img",
							'attr' => $attrs['dsm_content_image_width']['innerContent'] ?? array(),
							'property' => 'width',
						)
					),

					CommonStyle::style(
						array(
							'selector' => ".dsm-advanced-tabs-container {$args['orderClass']}.dsm-tab .dsm-image",
							'attr' => $attrs['dsm_tabs_image_width']['innerContent'] ?? array(),
							'property' => 'width',
						)
					),

					CommonStyle::style(
						array(
							'selector' => ".dsm-advanced-tabs-container {$args['orderClass']}.dsm-content-wrapper .dsm-image, .dsm-advanced-tabs-container {$args['orderClass']}.dsm-content-wrapper .dsm-icon",
							'attr' => $attrs['dsm_content_image_icon_alignment']['innerContent'] ?? array(),
							'property' => 'text-align',
						)
					),

					CommonStyle::style(
						array(
							'selector' => ".dsm-advanced-tabs-container {$args['orderClass']}.dsm-content-wrapper .et-pb-icon.dsm_content_icon, .dsm-advanced-tabs-container {$args['orderClass']}.dsm-content-wrapper .dsm-icon",
							'attr' => $attrs['dsm_content_icon_size']['innerContent'] ?? array(),
							'property' => 'font-size',
						)
					),

					CommonStyle::style(
						array(
							'selector' => ".dsm-advanced-tabs-container {$args['orderClass']}.dsm-content-wrapper .et-pb-icon.dsm_content_icon, .dsm-advanced-tabs-container {$args['orderClass']}.dsm-content-wrapper .dsm-icon",
							'attr' => $attrs['dsm_content_icon_color']['innerContent'] ?? array(),
							'property' => 'color',
						)
					),
				),
			)
		);
	}

	/**
	 * 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, $child_modules_content, $block, $elements)
	{
		$children_ids = ChildrenUtils::extract_children_ids($block);

		// 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' => '',
			)
		);

		// 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,
				'elements' => $elements,
				'id' => $block->parsed_block['id'],
				'moduleClassName' => 'd5_tut_simple_quick_module',
				'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'),
				'childrenIds' => $children_ids,
				'children' => $module_container_children,
			)
		);
	}
}
