<?php
/**
 * PPOM Inputs Meta Manager Class
 *
 * It control the inputs meta data. It show all the global inputs settings.
 *
 * @version  1.0
 */

/*
**========== Block direct access ===========
*/
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

class PPOM_InputManager {

	/**
	 * Current field definition array for the active render context.
	 *
	 * @var array<string, mixed>
	 */
	public static $input_meta;

	/**
	 * PPOM input type slug (e.g. text, select).
	 *
	 * @var string
	 */
	public $input_type;

	/**
	 * Stores the active field definition and type for accessor methods.
	 *
	 * @param array<string, mixed> $input_meta Field definition from the meta group.
	 * @param string               $input_type Input type slug.
	 */
	public function __construct( $input_meta, $input_type ) {

		self::$input_meta = $input_meta;

		$this->input_type = $input_type;
	}


	/**
	 * Field Title
	 *
	 * @hook ppom_{$field_type}_input_meta_title
	 *
	 * @return string
	 */
	function title() {

		$title = isset( self::$input_meta['title'] ) ? stripslashes( self::$input_meta['title'] ) : '';

		$title = ppom_wpml_translate( $title, 'PPOM' );

		return apply_filters( 'ppom_input_meta_title', $title, self::$input_meta );
	}


	/**
	 * Field Desciption
	 *
	 * @hook ppom_{$field_type}_input_meta_desc
	 * @hook ppom_description_content
	 *
	 * @return string Shortcode-expanded HTML/description.
	 */
	function desc() {

		$desc = isset( self::$input_meta['description'] ) ? stripslashes( self::$input_meta['description'] ) : '';

		$desc = ppom_wpml_translate( $desc, 'PPOM' );

		// old Filter
		$desc = apply_filters( 'ppom_description_content', $desc, self::$input_meta );
		$desc = apply_filters( 'ppom_input_meta_desc', $desc, self::$input_meta );

		return do_shortcode( $desc );
	}


	/**
	 * Field Required
	 *
	 * @hook ppom_{$field_type}_input_meta_required
	 *
	 * @return mixed Truthy when the field is required.
	 */
	function required() {

		$required = isset( self::$input_meta['required'] ) ? self::$input_meta['required'] : '';

		return apply_filters( 'ppom_input_meta_required', $required, self::$input_meta );
	}


	/**
	 * Whether description should display as a tooltip.
	 *
	 * @return mixed
	 */
	function enable_tooltip() {

		$required = isset( self::$input_meta['desc_tooltip'] ) ? self::$input_meta['desc_tooltip'] : '';

		return $required;
	}


	/**
	 * Field dataname (Field Unique ID)
	 *
	 * @hook ppom_{$field_type}_input_meta_data_name
	 *
	 * @return string Sanitized `data_name` or derived from title.
	 */
	function data_name() {

		$data_name = isset( self::$input_meta['data_name'] ) ? sanitize_key( self::$input_meta['data_name'] ) : $this->title();

		return apply_filters( 'ppom_input_meta_data_name', $data_name, self::$input_meta );
	}


	/**
	 * Field Placeholder
	 *
	 * @hook ppom_{$field_type}_input_meta_placeholder
	 *
	 * @return string
	 */
	function placeholder() {

		$placeholder = isset( self::$input_meta['placeholder'] ) ? stripslashes( self::$input_meta['placeholder'] ) : '';

		$placeholder = ppom_wpml_translate( $placeholder, 'PPOM' );

		return apply_filters( 'ppom_input_meta_placeholder', $placeholder, self::$input_meta );
	}


	/**
	 * Field Error Message
	 *
	 * @hook ppom_{$field_type}_input_meta_error_msg
	 *
	 * @return string
	 */
	function error_msg() {

		$error_msg = isset( self::$input_meta['error_message'] ) ? self::$input_meta['error_message'] : '';

		$error_msg = ppom_wpml_translate( $error_msg, 'PPOM' );

		return apply_filters( 'ppom_input_meta_error_msg', $error_msg, self::$input_meta );
	}


	/**
	 * Field Label
	 *
	 * Show Asterisk If Require On
	 *
	 * Show Description If Not Null
	 *
	 * @hook ppom_field_description
	 * @hook ppom_{$field_type}_input_meta_label_html
	 *
	 * @param bool $tooltip  Append description next to the label when true.
	 * @param bool $desc     Apply `ppom_field_description` to the description fragment.
	 * @param bool $asterisk Append required marker when applicable.
	 *
	 * @return string HTML fragment for the label area.
	 */
	function field_label( $tooltip = true, $desc = true, $asterisk = true ) {

		$asterisk_symbol = ( ! empty( $this->required() ) && $this->title() != '' ) ? '<span class="show_required"> *</span>' : '';

		$show_desc = ( ! empty( $this->desc() ) ) ? '<span class="show_description ppom-input-desc">' . $this->desc() . '</span>' : '';

		if ( $desc ) {
			$show_desc = apply_filters( 'ppom_field_description', $show_desc, self::$input_meta );
		}

		$field_label = $this->title();
		// $field_label = $this->title() . $asterisk_symbol . $show_desc;

		if ( $asterisk ) {
			$field_label = $field_label . $asterisk_symbol;
		}

		if ( $tooltip ) {
			$field_label = $field_label . $show_desc;
		}

		return apply_filters( 'ppom_input_meta_label_html', $field_label, self::$input_meta );
	}


	/**
	 * Field Desciption With Tooltip
	 *
	 * @hook ppom_input_meta_tooltip_desc
	 *
	 * @return string HTML fragment.
	 */
	function tooltip() {

		$show_desc = ( ! empty( $this->desc() ) ) ? '<span class="show_description ppom-input-desc">' . $this->desc() . '</span>' : '';
		$show_desc = apply_filters( 'ppom_field_description', $show_desc, self::$input_meta );

		return apply_filters( 'ppom_input_meta_tooltip_desc', $show_desc, self::$input_meta );
	}


	/**
	 * Field Multiple Options
	 *
	 * Checkbox|Radio|Select|Image|Pallete
	 *
	 * @hook ppom_{$field_type}_input_meta_multi_options
	 *
	 * @return array<int|string, mixed>
	 */
	function options() {

		$options = isset( self::$input_meta['options'] ) ? self::$input_meta['options'] : array();

		if ( is_array( $options ) ) {
			$options = array_map( 'ppom_translation_options', $options );
		}

		return apply_filters( 'ppom_input_meta_multi_options', $options, self::$input_meta );
	}


	/**
	 * Images Options
	 *
	 * @hook ppom_{$field_type}_input_meta_images
	 *
	 * @return array<int|string, mixed>
	 */
	function images() {

		$images = isset( self::$input_meta['images'] ) ? self::$input_meta['images'] : array();
		if ( ! is_array( $images ) ) {
			$images        = stripslashes( $images );
			$decode_images = json_decode( $images, true );
			$images        = is_array( $decode_images ) ? $decode_images : $images;
		}
		return apply_filters( 'ppom_input_meta_images', $images, self::$input_meta );
	}


	/**
	 * Audio/Video Options
	 *
	 * @hook ppom_{$field_type}_input_meta_audio
	 *
	 * @return array<int|string, mixed>
	 */
	function audio_video() {

		$audios = isset( self::$input_meta['audio'] ) ? self::$input_meta['audio'] : array();
		if ( ! is_array( $audios ) ) {
			$audios        = stripslashes( $audios );
			$decode_values = json_decode( $audios, true );
			$audios        = is_array( $decode_values ) ? $decode_values : $audios;
		}
		return apply_filters( 'ppom_input_meta_audio', $audios, self::$input_meta );
	}


	/*
	===================================
		Wrapper Classes Section
	===================================*/


	/**
	 * Field inner Wrapper Classes
	 *
	 * @hook ppom_input_wrapper_class
	 *
	 * @return string Space-separated class list.
	 */
	function field_inner_wrapper_classes() {

		$classes         = array( 'form-group' );
		$wrapper_classes = implode( ' ', $classes );

		// return apply_filters_deprecated( 'ppom_input_wrapper_class', array( $wrapper_classes, self::$input_meta ), '21.3', 'ppom_input_wrapper_classes' );
		return apply_filters( 'ppom_input_wrapper_class', $wrapper_classes, self::$input_meta );
	}


	/**
	 * Field Label Classes
	 *
	 * @hook ppom_{$this->input_type}_input_label_classes
	 *
	 * @return string Space-separated class list.
	 */
	function label_classes() {

		$classes = array( 'form-control-label' );

		$label_classes = apply_filters( 'ppom_input_label_classes', $classes, self::$input_meta );

		$label_classes = implode( ' ', $label_classes );

		return $label_classes;
	}


	/**
	 * Field Classes Array
	 *
	 * @hook ppom_{$this->input_type}_input_meta_classes
	 * @hook ppom_input_classes
	 *
	 * @return list<string>
	 */
	function input_classes_array() {

		$classes = isset( self::$input_meta['class'] ) ? explode( ',', self::$input_meta['class'] ) : array();

		if ( ! empty( $classes ) ) {
			$classes[] = 'form-control';
		} else {
			$classes = array( 'form-control' );
		}

		if ( $this->input_type == 'color' ) {
			$classes[] = 'text';
		} else {
			$classes[] = $this->input_type;
		}

		$classes[] = 'ppom-input';
		// TODO: re-check again
		// if ($this->input_type != 'image') {
		// }

		if ( $this->required() ) {
			$classes[] = 'ppom-required';
		}

		if ( ( $this->input_type == 'radio' && ( $key = array_search( 'form-control', $classes ) ) !== false ) ||
			$this->input_type == 'checkbox' && ( $key = array_search( 'form-control', $classes ) ) !== false ) {
			unset( $classes[ $key ] );
			$classes[] = 'ppom-check-input';
		}

		if ( $this->input_type == 'select' && ( $key = array_search( 'form-control', $classes ) ) !== false ) {
			unset( $classes[ $key ] );
			$classes[] = 'form-select';
		}

		$classes = array_filter( apply_filters( 'ppom_input_meta_classes', $classes, self::$input_meta ) );

		return $classes;
	}


	/**
	 * Field Classes
	 *
	 * @hook ppom_{$this->input_type}_input_meta_classes
	 * @hook ppom_input_classes
	 *
	 * @return string Space-separated class list.
	 */
	function input_classes() {

		$classes = $this->input_classes_array();

		// $input_classes = apply_filters_deprecated( 'ppom_input_classes', array( $classes, self::$input_meta ), '21.3', 'ppom_form_input_classes' );
		$input_classes = apply_filters( 'ppom_input_classes', $classes, self::$input_meta );

		$input_classes = implode( ' ', $input_classes );

		return $input_classes;
	}


	/**
	 * Radio Input label classes
	 *
	 * @hook ppom_radio_input_label_classes
	 *
	 * @return string
	 */
	function radio_label_classes() {

		$classes = array( 'form-check-label' );

		$label_class = apply_filters( 'ppom_radio_input_label_classes', $classes, self::$input_meta );

		$label_class = implode( ' ', $label_class );

		return $label_class;
	}


	/**
	 * Checkbox Input label classes
	 *
	 * @hook ppom_checkbox_input_label_classes
	 *
	 * @return string
	 */
	function checkbox_label_classes() {

		$classes = array( 'form-check-label' );

		$label_class = apply_filters( 'ppom_checkbox_input_label_classes', $classes, self::$input_meta );

		$label_class = implode( ' ', $label_class );

		return $label_class;
	}


	/**
	 * Generate Field Attribute Name Key
	 *
	 * @hook ppom_{$this->input_type}_input_name_attr
	 *
	 * @return string `ppom[fields][...]` name attribute.
	 */
	function form_name() {

		$form_name = 'ppom[fields][' . esc_attr( $this->data_name() ) . ']';
		if ( $this->input_type == 'checkbox' ) {
			$form_name .= '[]';
		}

		return apply_filters( 'ppom_input_name_attr', $form_name, self::$input_meta );
	}


	/**
	 * Get input meta value by key
	 *
	 * @hook ppom_{$this->input_type}_input_meta_value_by_key
	 *
	 * @param string     $key     Meta key on the field definition.
	 * @param mixed|null $default Fallback when missing or empty string.
	 *
	 * @return mixed
	 */
	function get_meta_value( $key, $default = null ) {

		$value = ! is_null( $default ) ? $default : '';
		if ( isset( self::$input_meta[ $key ] ) && self::$input_meta[ $key ] != '' ) {
			$value = self::$input_meta[ $key ];
		}

		$value = apply_filters( "ppom_{$this->input_type}_input_meta_value_by_key", $value, $key, self::$input_meta );

		return apply_filters( 'ppom_field_meta_value', $value, $key, self::$input_meta );
	}
}
