<?php
/**
 * Class: Eac_Acf_Group_Text
 *
 * Méthode 'get_acf_supported_fields' pour la liste des champs 'text'
 *
 * @return Affiche les TEXTs d'un champ ACF de type 'GROUP' pour l'article courant
 *
 * @since 1.8.3
 */

namespace EACCustomWidgets\Includes\Acf\DynamicTags\Tags;

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly
}

use EACCustomWidgets\Includes\Acf\Eac_Acf_Lib;
use Elementor\Controls_Manager;
use Elementor\Core\DynamicTags\Tag;
use Elementor\Modules\DynamicTags\Module as TagsModule;

class Eac_Acf_Group_Text extends Tag {
	use \EACCustomWidgets\Includes\Traits\Panel_Template_Trait;
	use \EACCustomWidgets\Includes\Traits\Post_Main_Id_Trait;

	public function get_name(): string {
		return 'eac-addon-group-text-acf-values';
	}

	public function get_title(): string {
		return esc_html__( 'Group text', 'eac-components' );
	}

	public function get_group(): array {
		return array( 'eac-acf-groupe' );
	}

	public function get_categories(): array {
		return array(
			TagsModule::TEXT_CATEGORY,
		);
	}

	public function get_panel_template_setting_key(): string {
		return 'acf_group_text_key';
	}

	protected function register_controls(): void {

		$this->add_control(
			'acf_group_text_key',
			array(
				'label'       => esc_html__( 'Field', 'eac-components' ),
				'type'        => Controls_Manager::SELECT,
				'groups'      => Eac_Acf_Lib::get_acf_fields_options( $this->get_acf_supported_fields(), '', 'group' ),
				'label_block' => true,
			)
		);
	}

	/**
	 * render
	 *
	 * @param $group_key
	 * @param $sub_field_key
	 * @param $sub_meta_key
	 */
	public function render(): void {
		$field_value = '';
		$field       = array();
		$key         = $this->get_settings( 'acf_group_text_key' );

		if ( ! empty( $key ) ) {
			list($group_key, $sub_field_key, $sub_meta_key) = array_pad( explode( '::', $key ), 3, '' );
			// Fonction du trait Post_Main_Id_Trait
			$real_id = $this->get_post_template_id( $sub_field_key );

			/**
			 * Le nom du champ est = 'field_group_key_field_key'
			 * On calcule la meta_key
			 */
			$meta_key = Eac_Acf_Lib::get_acf_field_name( $sub_field_key, $sub_meta_key, $real_id );

			// Pas de meta_key pour le champ
			if ( empty( $meta_key ) ) {
				return;
			}

			if ( have_rows( $group_key ) ) {
				the_row();
				$field = get_field_object( $meta_key, $real_id );
			}
			reset_rows();

			if ( $field && ! empty( $field['value'] ) ) {
				$field_value = $field['value'];

				switch ( $field['type'] ) {
					case 'text':
						$field_value = sprintf( '%1$s %2$s %3$s', $field['prepend'], $field['value'], $field['append'] );
						break;
					case 'relationship':
					case 'post_object':
						$values = array();
						/** Fix cast $field_value en tableau */
						$field_value = is_array( $field_value ) ? $field_value : array( $field_value );

						if ( 'object' === $field['return_format'] ) {
							foreach ( $field_value as $value ) {
								$values[] = "<a href='" . esc_url( get_permalink( get_post( $value->ID )->ID ) ) . "'><span class='acf-relationship'>" . esc_html( $value->post_title ) . '</span></a><br>';
							}
						} else { // Format ID
							foreach ( $field_value as $value ) {
								$values[] = "<a href='" . esc_url( get_permalink( get_post( $value )->ID ) ) . "'><span class='acf-relationship'>" . esc_html( get_post( $value )->post_title ) . '</span></a><br>';
							}
						}
						$field_value = implode( ' ', $values );
						break;
					case 'radio':
					case 'button_group':
						if ( 'array' === $field['return_format'] ) {
							$field_value = $field_value['value'];
						}
						break;
					case 'select':
					case 'checkbox':
						$field_value = (array) $field_value;
						$values      = array();

						foreach ( $field_value as $value ) {
							if ( 'array' === $field['return_format'] ) {
								$values[] = $value['value'];
							} else {
								$values[] = $value;
							}
						}
						$field_value = implode( ', ', $values );
						break;
					case 'oembed':
						// On prend directement dans la BDD sans formatage
						$field_value = get_post_meta( $real_id, $meta_key, true );
						break;
					case 'date_picker':
					case 'date_time_picker':
						$date_time   = \DateTime::createFromFormat( $field['return_format'], $field_value, new \DateTimeZone( wp_timezone_string() ) );
						$field_value = $date_time instanceof \DateTime ? $date_time->format( 'Y-m-d H:i:s' ) : '';
						break;
				}
			} else {
				$field_value = get_post_meta( $real_id, $meta_key, true );
				if ( is_array( $field_value ) ) {
					$field_value = implode( ', ', $field_value ); }
			}
		}

		echo wp_kses_post( $field_value );
	}

	protected function get_acf_supported_fields(): array {
		return array(
			'text',
			'textarea',
			'wysiwyg',
			'select',
			'checkbox',
			'radio',
			'true_false',
			'date_picker',
			'date_time_picker',
			'oembed',
			'button_group',
			'relationship',
			'post_object',
			'url',
		);
	}

	public function print_panel_template() {
		$this->fix_print_panel_template();
	}
}
