<?php
/**
 * Module: JetEngine Dynamic Terms.
 */
namespace Jet_Engine_Divi_Views\Components\Jet_Engine\Modules;

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

use ET\Builder\Framework\DependencyManagement\Interfaces\DependencyInterface;
use ET\Builder\Packages\ModuleLibrary\ModuleRegistration;
use Jet_Engine_Divi_Views\Module_Render;
use ET\Builder\FrontEnd\Module\Style;
use Jet_Engine_Divi_Views\Utils;
use Jet_Engine_Divi_Views\Components\Jet_Engine\Current_Object_Trait;

class Dynamic_Terms implements DependencyInterface {

	use Current_Object_Trait;

	/**
	 * Module's style components.
	 *
	 * This function is equivalent of JS function ModuleStyles located in
	 * module/styles.tsx.
	 *
	 * @param array $args {
	 *     An array of arguments.
	 *
	 *      @type string $id                Module ID. In VB, the ID of module is UUIDV4. In FE, the ID is order index.
	 *      @type string $name              Module name.
	 *      @type string $attrs             Module attributes.
	 *      @type string $parentAttrs       Parent attrs.
	 *      @type string $orderClass        Selector class name.
	 *      @type string $parentOrderClass  Parent selector class name.
	 *      @type string $wrapperOrderClass Wrapper selector class name.
	 *      @type string $settings          Custom settings.
	 *      @type string $state             Attributes state.
	 *      @type string $mode              Style mode.
	 *      @type ModuleElements $elements  ModuleElements instance.
	 * }
	 */
	public static function module_styles( $args ) {
		$elements                    = $args['elements'];
		$settings                    = $args['settings'] ?? [];
		$default_printed_style_attrs = $args['defaultPrintedStyleAttrs'] ?? [];
		$attrs                       = $args['attrs'] ?? [];
		$order_class                 = $args['orderClass'] ?? '';

		$selectors = [
			'wrapper'     => "{$order_class} .jet-listing-dynamic-terms",
			'link'        => "{$order_class} .jet-listing-dynamic-terms__link",
			'link_hover'  => "{$order_class} .jet-listing-dynamic-terms__link:hover",
			'icon'        => "{$order_class} .jet-listing-dynamic-terms__icon",
			'icon_path'   => "{$order_class} .jet-listing-dynamic-terms__icon svg path",
			'icon_svg'    => "{$order_class} .jet-listing-dynamic-terms__icon svg",
			'icon_ltr'    => "body:not(.rtl) {$order_class} .jet-listing-dynamic-terms__icon",
			'icon_rtl'    => "body.rtl {$order_class} .jet-listing-dynamic-terms__icon",
			'delimiter'   => "{$order_class} .jet-listing-dynamic-terms__delimiter",
			'prefix'      => "{$order_class} .jet-listing-dynamic-terms__prefix",
			'prefix_ltr'  => "body:not(.rtl) {$order_class} .jet-listing-dynamic-terms__prefix",
			'prefix_rtl'  => "body.rtl {$order_class} .jet-listing-dynamic-terms__prefix",
			'suffix'      => "{$order_class} .jet-listing-dynamic-terms__suffix",
			'suffix_ltr'  => "body:not(.rtl) {$order_class} .jet-listing-dynamic-terms__suffix",
			'suffix_rtl'  => "body.rtl {$order_class} .jet-listing-dynamic-terms__suffix",
		];

		$link_typography = Utils\extract_typography_attrs( $attrs['module']['decoration']['linkTypography'] ?? null );
		$link_typography_styles = Utils\build_typography_styles(
			$selectors['link'],
			$link_typography
		);

		$link_border = Utils\extract_border_attrs( $attrs['module']['decoration']['linkBorder'] ?? null );
		$link_border_styles = Utils\build_border_styles(
			$selectors['link'],
			$link_border
		);

		$link_spacing = Utils\extract_spasing_attrs( $attrs['module']['decoration']['linkSpacing'] ?? null );
		$link_spacing_styles = Utils\build_spacing_styles(
			$selectors['link'],
			$link_spacing
		);

		$prefix_typography = Utils\extract_typography_attrs( $attrs['module']['decoration']['prefixTypography'] ?? null );
		$prefix_typography_styles = Utils\build_typography_styles(
			$selectors['prefix'],
			$prefix_typography
		);

		$suffix_typography = Utils\extract_typography_attrs( $attrs['module']['decoration']['suffixTypography'] ?? null );
		$suffix_typography_styles = Utils\build_typography_styles(
			$selectors['suffix'],
			$suffix_typography
		);

		Style::add(
			[
				'id'            => $args['id'],
				'name'          => $args['name'],
				'orderIndex'    => $args['orderIndex'],
				'storeInstance' => $args['storeInstance'],
				'styles'        => [
					$elements->style(
						[
							'attrName'   => 'module',
							'styleProps' => [
								'defaultPrintedStyleAttrs' => $default_printed_style_attrs['module']['decoration'] ?? [],
								'disabledOn'               => [
									'disabledModuleVisibility' => $settings['disabledModuleVisibility'] ?? null,
								],
								'advancedStyles'           => array_merge(
									$link_typography_styles,
									$link_border_styles,
									$link_spacing_styles,
									$prefix_typography_styles,
									$suffix_typography_styles,
									[
										[
											'componentName' => 'divi/common',
											'props'         => [
												'selector' => $selectors['wrapper'],
												'attr'     => $attrs['module']['decoration']['termsAlignment'] ?? [],
												'property' => 'text-align',
											],
										],
										[
											'componentName' => 'divi/common',
											'props'         => [
												'selector' => $selectors['link'],
												'attr'     => $attrs['module']['decoration']['linkBg'] ?? [],
												'property' => 'background-color',
											],
										],
										[
											'componentName' => 'divi/common',
											'props'         => [
												'selector' => $selectors['link'],
												'attr'     => $attrs['module']['decoration']['linkColor'] ?? [],
												'property' => 'color',
											],
										],
										[
											'componentName' => 'divi/common',
											'props'         => [
												'selector' => $selectors['link_hover'],
												'attr'     => $attrs['module']['decoration']['linkBgHover'] ?? [],
												'property' => 'background-color',
											],
										],
										[
											'componentName' => 'divi/common',
											'props'         => [
												'selector' => $selectors['link_hover'],
												'attr'     => $attrs['module']['decoration']['linkColorHover'] ?? [],
												'property' => 'color',
											],
										],
										[
											'componentName' => 'divi/common',
											'props'         => [
												'selector' => $selectors['link_hover'],
												'attr'     => $attrs['module']['decoration']['linkHoverBorderColor'] ?? [],
												'property' => 'border-color',
											],
										],
										[
											'componentName' => 'divi/common',
											'props'         => [
												'selector' => $selectors['icon'],
												'attr'     => $attrs['module']['decoration']['iconColor'] ?? [],
												'property' => 'color',
											],
										],
										[
											'componentName' => 'divi/common',
											'props'         => [
												'selector' => $selectors['icon_path'],
												'attr'     => $attrs['module']['decoration']['iconColor'] ?? [],
												'property' => 'fill',
											],
										],
										[
											'componentName' => 'divi/common',
											'props'         => [
												'selector' => $selectors['icon_svg'],
												'attr'     => $attrs['module']['decoration']['iconSize'] ?? [],
												'property' => 'width',
											],
										],
										[
											'componentName' => 'divi/common',
											'props'         => [
												'selector' => $selectors['icon_ltr'],
												'attr'     => $attrs['module']['decoration']['iconGap'] ?? [],
												'property' => 'margin-right',
											],
										],
										[
											'componentName' => 'divi/common',
											'props'         => [
												'selector' => $selectors['icon_rtl'],
												'attr'     => $attrs['module']['decoration']['iconGap'] ?? [],
												'property' => 'margin-left',
											],
										],
										[
											'componentName' => 'divi/common',
											'props'         => [
												'selector' => $selectors['delimiter'],
												'attr'     => $attrs['module']['decoration']['delimiterColor'] ?? [],
												'property' => 'color',
											],
										],
										[
											'componentName' => 'divi/common',
											'props'         => [
												'selector' => $selectors['delimiter'],
												'attr'     => $attrs['module']['decoration']['delimiterSize'] ?? [],
												'property' => 'font-size',
											],
										],
										[
											'componentName' => 'divi/common',
											'props'         => [
												'selector' => $selectors['delimiter'],
												'attr'     => $attrs['module']['decoration']['delimiterLGap'] ?? [],
												'property' => 'margin-left',
											],
										],
										[
											'componentName' => 'divi/common',
											'props'         => [
												'selector' => $selectors['delimiter'],
												'attr'     => $attrs['module']['decoration']['delimiterRGap'] ?? [],
												'property' => 'margin-right',
											],
										],
										[
											'componentName' => 'divi/common',
											'props'         => [
												'selector' => $selectors['prefix'],
												'attr'     => $attrs['module']['decoration']['prefixColor'] ?? [],
												'property' => 'color',
											],
										],
										[
											'componentName' => 'divi/common',
											'props'         => [
												'selector' => $selectors['prefix_ltr'],
												'attr'     => $attrs['module']['decoration']['prefixGap'] ?? [],
												'property' => 'margin-right',
											],
										],
										[
											'componentName' => 'divi/common',
											'props'         => [
												'selector' => $selectors['prefix_rtl'],
												'attr'     => $attrs['module']['decoration']['prefixGap'] ?? [],
												'property' => 'margin-left',
											],
										],
										[
											'componentName' => 'divi/common',
											'props'         => [
												'selector' => $selectors['suffix'],
												'attr'     => $attrs['module']['decoration']['suffixColor'] ?? [],
												'property' => 'color',
											],
										],
										[
											'componentName' => 'divi/common',
											'props'         => [
												'selector' => $selectors['suffix_ltr'],
												'attr'     => $attrs['module']['decoration']['suffixGap'] ?? [],
												'property' => 'margin-left',
											],
										],
										[
											'componentName' => 'divi/common',
											'props'         => [
												'selector' => $selectors['suffix_rtl'],
												'attr'     => $attrs['module']['decoration']['suffixGap'] ?? [],
												'property' => 'margin-right',
											],
										],
									]
								),
							],
						]
					),
				],
			]
		);
	}

	/**
	 * Set script data of used module options.
	 *
	 * @since ??
	 *
	 * @param array $args {
	 *   Array of arguments.
	 *
	 *   @type string $id       Module id.
	 *   @type string $selector Module selector.
	 *   @type array  $attrs    Module attributes.
	 * }
	 */
	public static function module_script_data( $args ) {
	}

	/**
	 * Module classnames function for Dynamic module.
	 *
	 * This function is equivalent of JS function moduleClassnames located in
	 * src/components/dynamic-module/module-classnames.ts.
	 *
	 * @since ??
	 *
	 * @param array $args {
	 *     An array of arguments.
	 *
	 *     @type object $classnamesInstance Instance of ET\Builder\Packages\Module\Layout\Components\Classnames.
	 *     @type array  $attrs              Block attributes data that being rendered.
	 * }
	 */
	public static function module_classnames( $args ) {
	}

	/**
	 * Dynamic module render callback which outputs server side rendered HTML on the Front-End.
	 *
	 * @since ??
	 *
	 * @param array          $attrs    Block attributes that were saved by VB.
	 * @param string         $content  Block content.
	 * @param \WP_Block      $block    Parsed block object that being rendered.
	 * @param ModuleElements $elements ModuleElements instance.
	 *
	 * @return string HTML rendered of module.
	 */
	public static function render_callback( $attrs, $content, $block, $elements ) {
		$prepared_attrs = [];

		$object_was_set = self::set_current_object( $attrs );

		foreach ( $attrs as $key => $value ) {
			if ( isset( $value['innerContent']['desktop']['value'] ) ) {
				$prepared_attrs[ $key ] = $value['innerContent']['desktop']['value'];
			}
		}

		$normalize_icon_pair = function ( $icon_key, $icon_url_key ) use ( &$prepared_attrs ) {

			if ( ! empty( $prepared_attrs[ $icon_url_key ] ) && is_array( $prepared_attrs[ $icon_url_key ] ) ) {
				if ( isset( $prepared_attrs[ $icon_url_key ]['url'] ) ) {
					$prepared_attrs[ $icon_url_key ] = $prepared_attrs[ $icon_url_key ]['url'];
				}
			}

			if ( ! empty( $prepared_attrs[ $icon_key ] ) && is_array( $prepared_attrs[ $icon_key ] ) ) {
				if ( isset( $prepared_attrs[ $icon_key ]['id'] ) ) {
					$prepared_attrs[ $icon_key ] = absint( $prepared_attrs[ $icon_key ]['id'] );
				} elseif ( isset( $prepared_attrs[ $icon_key ]['url'] ) ) {
					$prepared_attrs[ $icon_url_key ] = $prepared_attrs[ $icon_key ]['url'];
					$prepared_attrs[ $icon_key ]     = 0;
				}
			}

			if ( ! empty( $prepared_attrs[ $icon_key ] ) && is_string( $prepared_attrs[ $icon_key ] ) ) {
				$icon_string = trim( $prepared_attrs[ $icon_key ] );
				if ( filter_var( $icon_string, FILTER_VALIDATE_URL ) ) {
					$prepared_attrs[ $icon_url_key ] = $icon_string;
					$prepared_attrs[ $icon_key ]     = 0;
				} else {
					$prepared_attrs[ $icon_key ] = absint( $icon_string );
				}
			}

			if ( ! empty( $prepared_attrs[ $icon_key ] ) && empty( $prepared_attrs[ $icon_url_key ] ) ) {
				$icon_url = wp_get_attachment_url( absint( $prepared_attrs[ $icon_key ] ) );
				if ( $icon_url ) {
					$prepared_attrs[ $icon_url_key ] = $icon_url;
				}
			}

			if ( empty( $prepared_attrs[ $icon_key ] ) && ! empty( $prepared_attrs[ $icon_url_key ] ) && is_string( $prepared_attrs[ $icon_url_key ] ) ) {
				$attachment_id = attachment_url_to_postid( $prepared_attrs[ $icon_url_key ] );
				if ( $attachment_id ) {
					$prepared_attrs[ $icon_key ] = absint( $attachment_id );
				}
			}
		};

		$normalize_icon_pair( 'selected_terms_icon', 'selected_terms_icon_url' );

		$normalize_toggle = function ( $value ) {
			if ( true === $value || 1 === $value || '1' === $value || 'true' === $value || 'yes' === $value || 'on' === $value ) {
				return true;
			}

			if ( false === $value || 0 === $value || '0' === $value || '' === $value || null === $value || 'false' === $value || 'no' === $value || 'off' === $value ) {
				return false;
			}

			return (bool) $value;
		};

		$prepared_attrs['show_all_terms'] = isset( $prepared_attrs['show_all_terms'] ) ? $normalize_toggle( $prepared_attrs['show_all_terms'] ) : true;
		$prepared_attrs['terms_linked']   = isset( $prepared_attrs['terms_linked'] ) ? $normalize_toggle( $prepared_attrs['terms_linked'] ) : true;
		$prepared_attrs['hide_if_empty']  = isset( $prepared_attrs['hide_if_empty'] ) ? $normalize_toggle( $prepared_attrs['hide_if_empty'] ) : false;

		if ( ! isset( $prepared_attrs['terms_num'] ) ) {
			$prepared_attrs['terms_num'] = 1;
		}

		if ( ! isset( $prepared_attrs['terms_delimiter'] ) ) {
			$prepared_attrs['terms_delimiter'] = ',';
		}

		if ( ! isset( $prepared_attrs['orderby'] ) ) {
			$prepared_attrs['orderby'] = 'name';
		}

		if ( ! isset( $prepared_attrs['order'] ) ) {
			$prepared_attrs['order'] = 'ASC';
		}

		if ( ! isset( $prepared_attrs['object_context'] ) ) {
			$prepared_attrs['object_context'] = 'default_object';
		}

		$render = self::get_jet_engine_render_instance( $prepared_attrs );
		$divi_module_render = new Module_Render();

		$content = $divi_module_render->render_content(
			$render,
			$attrs,
			$block,
			$elements,
			self::class
		);

		if ( $object_was_set ) {
			self::reset_current_object();
		}

		return $content;
	}

	/**
	 * Get JetEngine render class instance
	 */
	public static function get_jet_engine_render_instance( $attributes ) {
		return jet_engine()->listings->get_render_instance( self::get_jet_engine_render_name(), $attributes );
	}

	/**
	 * Return name of JetEngine renderer for current module
	 */
	public static function get_jet_engine_render_name() {
		return 'dynamic-terms';
	}

	/**
	 * Loads `DynamicImage` and registers Front-End render callback and REST API Endpoints.
	 *
	 * @since ??
	 *
	 * @return void
	 */
	public function load() {

		$module_json_folder_path = JET_ENGINE_DIVI_VIEWS_MODULES_JSON_PATH . 'dynamic-terms/';

		add_action(
			'init',
			function() use ( $module_json_folder_path ) {
				ModuleRegistration::register_module(
					$module_json_folder_path,
					[
						'render_callback' => [ Dynamic_Terms::class, 'render_callback' ],
					]
				);
			}
		);
	}
}
