<?php
/**
 * Flip Container element (metadata + mapping).
 *
 * A 3D-flip container with a two-tab editor (Front / Back), modelled on
 * WPBakery's Pageable Container. Each tab is a "face" drop zone that can hold
 * any elements. On the frontend the first face is the front, the second is the
 * back (see the nth-child rules in cafw-elements.css).
 *
 * Rendering + editor behaviour are handled by the WPBakery classes in
 * class-cafw-flip-shortcodes.php (loaded only when WPBakery is active).
 *
 * @package CraftAddonsForWPBakery
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Class CAFW_Element_Flip_Container
 */
class CAFW_Element_Flip_Container extends CAFW_Element_Base {

	/**
	 * {@inheritDoc}
	 */
	public function get_shortcode_tag() {
		return 'cafw_flip_container';
	}

	/**
	 * {@inheritDoc}
	 */
	public function get_label() {
		return esc_html__( 'Flip Container', 'craft-addons-for-wpbakery' );
	}

	/**
	 * {@inheritDoc}
	 */
	public function get_description() {
		return esc_html__( 'A 3D flip card, front and back.', 'craft-addons-for-wpbakery' );
	}

	/**
	 * {@inheritDoc}
	 */
	public function get_admin_icon() {
		return 'dashicons-images-alt2';
	}

	/**
	 * {@inheritDoc}
	 */
	public function get_editor_icon() {
		return 'cafw-vc-icon cafw-vc-icon-flip-container';
	}

	/**
	 * {@inheritDoc}
	 */
	public function get_params() {
		return array(
			array(
				'type'             => 'dropdown',
				'heading'          => esc_html__( 'Flip direction', 'craft-addons-for-wpbakery' ),
				'param_name'       => 'direction',
				'value'            => array(
					esc_html__( 'Right', 'craft-addons-for-wpbakery' ) => 'right',
					esc_html__( 'Left', 'craft-addons-for-wpbakery' )  => 'left',
					esc_html__( 'Up', 'craft-addons-for-wpbakery' )    => 'up',
					esc_html__( 'Down', 'craft-addons-for-wpbakery' )  => 'down',
				),
				'std'              => 'right',
				'edit_field_class' => 'vc_col-sm-6 vc_column',
			),
			array(
				'type'             => 'dropdown',
				'heading'          => esc_html__( 'Flip trigger', 'craft-addons-for-wpbakery' ),
				'param_name'       => 'trigger',
				'value'            => array(
					esc_html__( 'On hover', 'craft-addons-for-wpbakery' ) => 'hover',
					esc_html__( 'On click', 'craft-addons-for-wpbakery' ) => 'click',
				),
				'std'              => 'hover',
				'edit_field_class' => 'vc_col-sm-6 vc_column',
			),
			array(
				'type'             => 'textfield',
				'heading'          => esc_html__( 'Flip speed (seconds)', 'craft-addons-for-wpbakery' ),
				'param_name'       => 'speed',
				'value'            => '0.6',
				'description'      => esc_html__( 'How long the flip animation takes.', 'craft-addons-for-wpbakery' ),
				'edit_field_class' => 'vc_col-sm-6 vc_column',
			),
			array(
				'type'             => 'textfield',
				'heading'          => esc_html__( 'Height (px)', 'craft-addons-for-wpbakery' ),
				'param_name'       => 'height',
				'value'            => '300',
				'description'      => esc_html__( 'Both faces share this height so the card flips cleanly.', 'craft-addons-for-wpbakery' ),
				'edit_field_class' => 'vc_col-sm-6 vc_column',
			),
			// --- Front background (type-gated, like Elementor) ---
			array(
				'type'             => 'dropdown',
				'heading'          => esc_html__( 'Front background', 'craft-addons-for-wpbakery' ),
				'param_name'       => 'front_bg_type',
				'value'            => array(
					esc_html__( 'None', 'craft-addons-for-wpbakery' )     => '',
					esc_html__( 'Color', 'craft-addons-for-wpbakery' )    => 'color',
					esc_html__( 'Gradient', 'craft-addons-for-wpbakery' ) => 'gradient',
					esc_html__( 'Image', 'craft-addons-for-wpbakery' )    => 'image',
				),
				'std'              => '',
				'edit_field_class' => 'vc_col-sm-12 vc_column cafw-bg-sep',
			),
			array(
				'type'             => 'colorpicker',
				'heading'          => esc_html__( 'Front color', 'craft-addons-for-wpbakery' ),
				'param_name'       => 'front_bg',
				'dependency'       => array(
					'element' => 'front_bg_type',
					'value'   => 'color',
				),
				'edit_field_class' => 'vc_col-sm-6 vc_column',
			),
			array(
				'type'             => 'colorpicker',
				'heading'          => esc_html__( 'Front gradient start', 'craft-addons-for-wpbakery' ),
				'param_name'       => 'front_grad1',
				'dependency'       => array(
					'element' => 'front_bg_type',
					'value'   => 'gradient',
				),
				'edit_field_class' => 'vc_col-sm-4 vc_column',
			),
			array(
				'type'             => 'colorpicker',
				'heading'          => esc_html__( 'Front gradient end', 'craft-addons-for-wpbakery' ),
				'param_name'       => 'front_grad2',
				'dependency'       => array(
					'element' => 'front_bg_type',
					'value'   => 'gradient',
				),
				'edit_field_class' => 'vc_col-sm-4 vc_column',
			),
			array(
				'type'             => 'textfield',
				'heading'          => esc_html__( 'Angle (deg)', 'craft-addons-for-wpbakery' ),
				'param_name'       => 'front_grad_angle',
				'value'            => '135',
				'dependency'       => array(
					'element' => 'front_bg_type',
					'value'   => 'gradient',
				),
				'edit_field_class' => 'vc_col-sm-4 vc_column',
			),
			array(
				'type'             => 'attach_image',
				'heading'          => esc_html__( 'Front image', 'craft-addons-for-wpbakery' ),
				'param_name'       => 'front_image',
				'dependency'       => array(
					'element' => 'front_bg_type',
					'value'   => 'image',
				),
			),
			// --- Back background ---
			array(
				'type'             => 'dropdown',
				'heading'          => esc_html__( 'Back background', 'craft-addons-for-wpbakery' ),
				'param_name'       => 'back_bg_type',
				'value'            => array(
					esc_html__( 'None', 'craft-addons-for-wpbakery' )     => '',
					esc_html__( 'Color', 'craft-addons-for-wpbakery' )    => 'color',
					esc_html__( 'Gradient', 'craft-addons-for-wpbakery' ) => 'gradient',
					esc_html__( 'Image', 'craft-addons-for-wpbakery' )    => 'image',
				),
				'std'              => '',
				'edit_field_class' => 'vc_col-sm-12 vc_column cafw-bg-sep',
			),
			array(
				'type'             => 'colorpicker',
				'heading'          => esc_html__( 'Back color', 'craft-addons-for-wpbakery' ),
				'param_name'       => 'back_bg',
				'dependency'       => array(
					'element' => 'back_bg_type',
					'value'   => 'color',
				),
				'edit_field_class' => 'vc_col-sm-6 vc_column',
			),
			array(
				'type'             => 'colorpicker',
				'heading'          => esc_html__( 'Back gradient start', 'craft-addons-for-wpbakery' ),
				'param_name'       => 'back_grad1',
				'dependency'       => array(
					'element' => 'back_bg_type',
					'value'   => 'gradient',
				),
				'edit_field_class' => 'vc_col-sm-4 vc_column',
			),
			array(
				'type'             => 'colorpicker',
				'heading'          => esc_html__( 'Back gradient end', 'craft-addons-for-wpbakery' ),
				'param_name'       => 'back_grad2',
				'dependency'       => array(
					'element' => 'back_bg_type',
					'value'   => 'gradient',
				),
				'edit_field_class' => 'vc_col-sm-4 vc_column',
			),
			array(
				'type'             => 'textfield',
				'heading'          => esc_html__( 'Angle (deg)', 'craft-addons-for-wpbakery' ),
				'param_name'       => 'back_grad_angle',
				'value'            => '135',
				'dependency'       => array(
					'element' => 'back_bg_type',
					'value'   => 'gradient',
				),
				'edit_field_class' => 'vc_col-sm-4 vc_column',
			),
			array(
				'type'             => 'attach_image',
				'heading'          => esc_html__( 'Back image', 'craft-addons-for-wpbakery' ),
				'param_name'       => 'back_image',
				'dependency'       => array(
					'element' => 'back_bg_type',
					'value'   => 'image',
				),
			),
		);
	}

	/**
	 * Register: load the WPBakery classes (when available), map, and register
	 * the frontend shortcodes.
	 *
	 * @return void
	 */
	public function register() {
		if ( class_exists( 'WPBakeryShortCodesContainer' ) ) {
			require_once __DIR__ . '/class-cafw-flip-shortcodes.php';
		}
		$this->map();

		add_shortcode( 'cafw_flip_container', array( $this, 'render_shortcode' ) );
		add_shortcode( 'cafw_flip_face', array( $this, 'render_shortcode' ) );
	}

	/**
	 * Editor markup for the container (the tabs strip + panels area).
	 *
	 * @return string
	 */
	private function container_markup() {
		$body = <<<'HTML'
<div class="vc_general vc_tta vc_tta-tabs vc_tta-pageable vc_tta-color-backend-tabs-white vc_tta-style-flat vc_tta-shape-rounded vc_tta-spacing-1 vc_tta-tabs-position-top vc_tta-controls-align-left">
	<div class="vc_tta-tabs-container"><ul class="vc_tta-tabs-list"><li class="vc_tta-tab" data-vc-tab data-vc-target-model-id="{{ model_id }}" data-element_type="cafw_flip_face"><a href="javascript:;" data-vc-tabs data-vc-container=".vc_tta" data-vc-target="[data-model-id='{{ model_id }}']" data-vc-target-model-id="{{ model_id }}"><span class="vc_tta-title-text">{{ section_title }}</span></a></li></ul>
	</div>
	<div class="vc_tta-panels vc_clearfix {{container-class}}">
		{{ content }}
	</div>
</div>
HTML;
		return '<div class="vc_tta-container vc_tta-o-non-responsive" data-vc-action="collapse">' . $body . '</div>';
	}

	/**
	 * Editor markup for a face (a tab panel with its drop zone).
	 *
	 * @return string
	 */
	private function face_markup() {
		return <<<'HTML'
<div class="vc_tta-panel-heading">
	<h4 class="vc_tta-panel-title vc_tta-controls-icon-position-left"><a href="javascript:;" data-vc-target="[data-model-id='{{ model_id }}']" data-vc-accordion data-vc-container=".vc_tta-container"><span class="vc_tta-title-text">{{ section_title }}</span><i class="vc_tta-controls-icon vc_tta-controls-icon-plus"></i></a></h4>
</div>
<div class="vc_tta-panel-body">
	{{ editor_controls }}
	<div class="{{ container-class }}">
		{{ content }}
	</div>
</div>
HTML;
	}

	/**
	 * Map the container and the face child element with WPBakery.
	 *
	 * @return void
	 */
	public function map() {
		if ( ! function_exists( 'vc_map' ) ) {
			return;
		}

		$category = esc_html__( 'Craft Addons', 'craft-addons-for-wpbakery' );

		// Drop in starter content so a freshly added card is filled in (like our
		// other elements): an icon + heading on the front, copy + button on the back.
		$front_content = '[vc_icon icon_fontawesome="fa fa-solid fa-bolt" color="custom" custom_color="#502bf8" size="lg" align="center"]'
			. '[vc_column_text]<h3 style="text-align: center;">' . esc_html__( 'Front side', 'craft-addons-for-wpbakery' ) . '</h3>'
			. '<p style="text-align: center;">' . esc_html__( 'Hover the card to flip it and reveal the back.', 'craft-addons-for-wpbakery' ) . '</p>[/vc_column_text]';

		$back_content = '[vc_column_text]<h3 style="text-align: center;">' . esc_html__( 'Back side', 'craft-addons-for-wpbakery' ) . '</h3>'
			. '<p style="text-align: center;">' . esc_html__( 'Add any elements you like to the back of the card.', 'craft-addons-for-wpbakery' ) . '</p>[/vc_column_text]'
			. '[vc_btn title="' . esc_attr__( 'Learn more', 'craft-addons-for-wpbakery' ) . '" color="violet" align="center" style="flat"]';

		$default_content = '[cafw_flip_face title="' . esc_attr__( 'Front', 'craft-addons-for-wpbakery' ) . '"]' . $front_content . '[/cafw_flip_face]'
			. '[cafw_flip_face title="' . esc_attr__( 'Back', 'craft-addons-for-wpbakery' ) . '"]' . $back_content . '[/cafw_flip_face]';

		$admin_js = array();
		if ( function_exists( 'vc_asset_url' ) ) {
			$admin_js[] = vc_asset_url( 'lib/vc/vc_tabs/vc-tabs.min.js' );
		}

		vc_map(
			array(
				'name'                    => $this->get_label(),
				'base'                    => $this->get_shortcode_tag(),
				'category'                => $category,
				'description'             => $this->get_description(),
				'icon'                    => $this->get_editor_icon(),
				'php_class_name'          => 'CAFW_Flip_Container_Shortcode',
				'as_parent'               => array( 'only' => 'cafw_flip_face' ),
				'is_container'            => true,
				'show_settings_on_create' => false,
				'js_view'                 => 'VcBackendTtaPageableView',
				'custom_markup'           => $this->container_markup(),
				'default_content'         => $default_content,
				'admin_enqueue_js'        => $admin_js,
				'params'                  => array_merge(
					$this->get_params(),
					array(
						array(
							'type'       => 'hidden',
							'param_name' => 'no_fill_content_area',
							'std'        => true,
						),
						array(
							'type'       => 'hidden',
							'param_name' => 'active_section',
							'std'        => '1',
						),
						array(
							'type'       => 'hidden',
							'param_name' => 'tab_position',
							'std'        => 'top',
						),
					),
					$this->get_shared_params()
				),
			)
		);

		vc_map(
			array(
				'name'                      => esc_html__( 'Flip Face', 'craft-addons-for-wpbakery' ),
				'base'                      => 'cafw_flip_face',
				'category'                  => $category,
				'icon'                      => $this->get_editor_icon(),
				'php_class_name'            => 'CAFW_Flip_Face_Shortcode',
				'allowed_container_element' => 'vc_row',
				'is_container'              => true,
				'show_settings_on_create'   => false,
				'as_child'                  => array( 'only' => 'cafw_flip_container' ),
				'js_view'                   => 'VcBackendTtaSectionView',
				'custom_markup'             => $this->face_markup(),
				'params'                    => array_merge(
					array(
						array(
							'type'        => 'textfield',
							'heading'     => esc_html__( 'Tab title', 'craft-addons-for-wpbakery' ),
							'param_name'  => 'title',
							'description' => esc_html__( 'Only shown in the editor (e.g. Front / Back).', 'craft-addons-for-wpbakery' ),
							'admin_label' => true,
						),
						array(
							'type'       => 'el_id',
							'param_name' => 'tab_id',
							'settings'   => array( 'auto_generate' => true ),
							'heading'    => esc_html__( 'Section ID', 'craft-addons-for-wpbakery' ),
						),
					),
					$this->get_shared_params()
				),
			)
		);
	}

	/**
	 * Frontend shortcode callback for the container and the faces.
	 *
	 * @param array  $atts    Shortcode atts.
	 * @param string $content Inner content.
	 * @param string $tag     Shortcode tag being rendered.
	 * @return string
	 */
	public function render_shortcode( $atts, $content, $tag ) {
		$classes = array(
			'cafw_flip_container' => 'CAFW_Flip_Container_Shortcode',
			'cafw_flip_face'      => 'CAFW_Flip_Face_Shortcode',
		);

		if ( isset( $classes[ $tag ] ) && class_exists( $classes[ $tag ] ) ) {
			$settings = class_exists( 'WPBMap' ) ? WPBMap::getShortCode( $tag ) : array();
			if ( empty( $settings ) ) {
				$settings = array( 'base' => $tag );
			}
			$shortcode = new $classes[ $tag ]( $settings );
			return $shortcode->content( is_array( $atts ) ? $atts : array(), $content );
		}

		return do_shortcode( (string) $content );
	}

	/**
	 * Not used - rendering happens via the WPBakery classes.
	 *
	 * @param array  $atts    Atts.
	 * @param string $content Content.
	 * @param string $el_id   Element ID.
	 * @return string
	 */
	public function render( $atts, $content, $el_id ) {
		return '';
	}
}
