<?php

namespace TotalThemeCore\WPBakery\Params;

defined( 'ABSPATH' ) || exit;

/**
 * WPBakery Param => Preset textfield.
 */
final class Preset_Textfield {

	/**
	 * Custom Param output.
	 */
	public static function output( $settings, $value ) {
		$is_preset = false;
		$choices   = $settings['choices'] ?? $settings['param_name'] ?? [];

		if ( class_exists( '\TotalThemeCore\Vcex\Setting_Choices' ) ) {
			$choices = (new \TotalThemeCore\Vcex\Setting_Choices( $choices, $settings ))->get_choices();
		}

		// Gap fix
		if ( 'gap' === $choices ) {
			if ( '0px' === $value ) {
				$value = 'none';
			} elseif ( in_array( $value, $choices ) ) {
				$value = absint( $value );
			}
		}

		// Check if the current value is a preset
		if ( ! $is_preset && ( ! $value || array_key_exists( $value, $choices ) ) ) {
			$is_preset = true;
		}

		// Begin output
		$html = '<div class="vcex-param-preset-textfield">';

			if ( $choices && is_array( $choices ) ) {
				$html .= '<div class="vcex-param-preset-textfield__fields">';
					$hidden = $is_preset ? '' : ' style="display: none;"';
					$html .= '<div class="vcex-param-preset-textfield__preset"' . $hidden . '>';
						$html .= self::select( $value, $choices );
					$html .= '</div>';
					$hidden = $is_preset ? ' style="display: none;"' : '';
					$html .= '<div class="vcex-param-preset-textfield__custom"' . $hidden . '>';
						$html .= self::custom_input( $is_preset, $value, $settings );
					$html .= '</div>';
				$html .= '</div>';
				$html .= self::toggle_button( $is_preset, $value );
			}

			// This is needed or else nothing gets saved
			$html .= '<input name="' . esc_attr( $settings['param_name'] ) . '" class="' . esc_attr( 'wpb_vc_param_value ' . $settings['param_name'] . ' ' . $settings['type'] . '_field' ) . '" type="hidden" value="' . esc_attr( $value ) . '">';

		$html .= '</div>';

		if ( empty( $settings['description'] )
			&& isset( $settings['choices'] )
			&& is_string( $settings['choices'] )
			&& function_exists( 'vcex_shortcode_param_description' )
		) {
			$desc = vcex_shortcode_param_description( $settings['choices'] );
			if ( $desc ) {
				$html .= '<div class="vc_description vc_clearfix">' . wp_kses( $desc, [ 'br' => [] ] ) . '</div>';
			}
		}

		return $html;
	}

	/**
	 * Returns the select html.
	 */
	protected static function select( $value, $choices ) {
		$html = '<select class="vcex-param-preset-textfield__select wpb-select">';
			foreach ( $choices as $choice_k => $choice_name ) {
				$html .= '<option value="' . esc_attr( $choice_k ) . '" ' . selected( $value, $choice_k, false ) . '>' . esc_html( $choice_name ) . '</option>';
			}
		$html .= '</select>';
		return $html;
	}

	/**
	 * Returns the toggle button html.
	 */
	protected static function toggle_button( $is_preset, $value ) {
		$toggle_pressed = ( ! $is_preset && $value ) ? 'true' : 'false';
		return '<button type="button" class="vcex-param-preset-textfield__toggle" aria-pressed="' . $toggle_pressed . '" aria-label="' . esc_attr__( 'Enter Custom Value.', 'total-theme-core' ) . '" title="' . esc_attr__( 'Enter Custom Value.', 'total-theme-core' ) . '"><svg class="vcex-param-toggle-icon" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="currentColor" height="20" width="20"><rect x="0" fill="none" width="20" height="20"/><g><path d="M13.89 3.39l2.71 2.72c.46.46.42 1.24.03 1.64l-8.01 8.02-5.56 1.16 1.16-5.58s7.6-7.63 7.99-8.03c.39-.39 1.22-.39 1.68.07zm-2.73 2.79l-5.59 5.61 1.11 1.11 5.54-5.65zm-2.97 8.23l5.58-5.6-1.07-1.08-5.59 5.6z"/></g></svg><svg class="vcex-param-toggle-icon-active" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="currentColor" height="20" width="20"><rect x="0" fill="none" width="20" height="20"/><g><path d="M12 5H7V2L1 6l6 4V7h5c2.2 0 4 1.8 4 4s-1.8 4-4 4H7v2h5c3.3 0 6-2.7 6-6s-2.7-6-6-6z"/></g></svg></button>';
	}

	/**
	 * Returns the custom input field.
	 */
	protected static function custom_input( $is_preset, $value, $settings ) {
		$custom_val  = ! $is_preset ? $value : '';
		$placeholder = isset( $settings['placeholder'] ) ? 'placeholder="' . esc_attr( $settings['placeholder'] ) . '"' : '';
		$responsive = $settings['responsive_input'] ?? false;
		if ( $responsive ) {
			return self::responsive_input( $custom_val, $settings );
		} else {
			return '<input class="vcex-param-preset-textfield__input wpb-textinput" type="text" value="' . esc_attr( $custom_val ) . '"' . $placeholder . '>';
		}
	}

	/**
	 * Returns a resppnsive input field.
	 */
	protected static function responsive_input( $value, $settings ) {
		if ( $value && ! str_contains( $value, ':' ) ) {
			$ogvalue = $value;
			$value   = "d:{$value}";
		}

		$medias = [
			'd'  => [
				'label' => esc_html__( 'Desktop', 'total-theme-core' ),
				'icon'  => 'dashicons dashicons-desktop',
			],
			'tl' => [
				'label' => esc_html__( 'Tablet Landscape', 'total-theme-core' ),
				'icon'  => 'dashicons dashicons-tablet',
			],
			'tp' => [
				'label' => esc_html__( 'Tablet Portrait', 'total-theme-core' ),
				'icon'  => 'dashicons dashicons-tablet',
			],
			'pl' => [
				'label' => esc_html__( 'Phone Landscape', 'total-theme-core' ),
				'icon'  => 'dashicons dashicons-smartphone',
			],
			'pp' => [
				'label' => esc_html__( 'Phone Portrait', 'total-theme-core' ),
				'icon'  => 'dashicons dashicons-smartphone',
			],
		];

		foreach ( $medias as $key => $val ) {
			$defaults[ $key ] = '';
		}

		$field_values = $defaults;
		foreach ( explode( '|', $value ) as $pair ) {
			[ $key, $val ] = array_pad( explode( ':', $pair, 2 ), 2, null );
			if ( '' !== $key && null !== $val && array_key_exists( $key, $field_values ) ) {
				$field_values[ trim( $key ) ] = trim( $val );
			}
		}

		$html = '<div class="vcex-param-responsive-field">';
			foreach ( $medias as $key => $val ) {
				$field_name = $settings['param_name'] . '[' . $key . ']';
				$icon_class = $val['icon'];
				$html .= '<div class="vcex-param-responsive-field__item">';
					if ( 'pl' === $key || 'tl' === $key ) {
						$icon_class .= ' dashicons--flip';
					}
					$html .= '<div class="vcex-param-responsive-field__icon"><span class="' . esc_attr( $icon_class ) . '"></span></div>';
					$html .= '<label for="' . esc_attr( $field_name ) .'" class="screen-reader-text">' . $medias[ $key ]['label'] . '</label>';
					$html .= '<input id="' . esc_attr( $field_name ) .'" data-vcex-device="' . esc_attr( $key ) . '" value="' . esc_attr( $field_values[ $key ] ) . '" type="text" placeholder="-">';
				$html .= '</div>';
			}
		$html .= '</div>';

		if ( ! empty( $ogvalue ) ) {
			$value = $ogvalue;
		}

		$html .= '<input class="vcex-param-preset-textfield__input" value="' . esc_attr( $value ) . '" type="hidden">';

		return $html;
	}

}
