<?php
/**
 * Dynamic Tag: Product Shipping.
 *
 * @package captain-widgets-kit
 */

use Elementor\Core\DynamicTags\Tag;
use Elementor\Modules\DynamicTags\Module;
if ( ! defined( 'ABSPATH' ) ) {
	exit;
	// Exit if accessed directly.
}

/**
 * Plus Addons Dynamic Tag - Product Shipping
 *
 * Provides a dynamic tag for Elementor to output the current product shipping information.
 *
 * @since 6.4.5
 */
class Captwiki_Tag_Product_Shipping extends Tag {


	/**
	 * Unique dynamic tag name used internally by Elementor.
	 *
	 * @since 6.4.5
	 * @return string
	 */
	public function get_name(): string {

		return 'captwiki-tag-product-shipping';
	}

	/**
	 * Label shown in Elementor Dynamic Tags list.
	 *
	 * @since 6.4.5
	 * @return string
	 */
	public function get_title(): string {

		return esc_html__( 'Product Shipping', 'captain-widgets-kit' );
	}

	/**
	 * Registers the group under which this tag will appear.
	 *
	 * @since 6.4.5
	 * @return array
	 */
	public function get_group(): array {

		return array( 'captwiki-woocommerce' );
	}

	/**
	 * Defines the category type (Text) for this dynamic tag.
	 *
	 * @since 6.4.5
	 * @return array
	 */
	public function get_categories(): array {

		return array(
			Module::TEXT_CATEGORY,
		);
	}

	/**
	 * Indicates that settings panel should be shown (even if empty).
	 *
	 * @since 6.4.5
	 * @return bool
	 */
	public function is_settings_required() {

		return true;
	}


	/**
	 * Register controls for this dynamic tag.
	 *
	 * @since 6.4.5
	 * @return void
	 */
	protected function register_controls(): void {

		$this->add_control(
			'shipping_type',
			array(
				'label'   => esc_html__( 'Shipping Type', 'captain-widgets-kit' ),
				'type'    => \Elementor\Controls_Manager::SELECT,
				'default' => 'weight',
				'options' => array(
					'weight'     => esc_html__( 'Weight', 'captain-widgets-kit' ),
					'dimensions' => esc_html__( 'Dimensions', 'captain-widgets-kit' ),
					'class'      => esc_html__( 'Shipping Class', 'captain-widgets-kit' ),
					'custom'     => esc_html__( 'Custom Text', 'captain-widgets-kit' ),
				),
			)
		);
		$this->add_control(
			'dimension_type',
			array(
				'label'     => esc_html__( 'Dimension Type', 'captain-widgets-kit' ),
				'type'      => \Elementor\Controls_Manager::SELECT,
				'default'   => 'all',
				'options'   => array(
					'all'    => esc_html__( 'All', 'captain-widgets-kit' ),
					'length' => esc_html__( 'Length', 'captain-widgets-kit' ),
					'width'  => esc_html__( 'Width', 'captain-widgets-kit' ),
					'height' => esc_html__( 'Height', 'captain-widgets-kit' ),
				),
				'condition' => array(
					'shipping_type' => 'dimensions',
				),
			)
		);
		$this->add_control(
			'custom_text',
			array(
				'label'       => esc_html__( 'Custom Text', 'captain-widgets-kit' ),
				'type'        => \Elementor\Controls_Manager::TEXT,
				'placeholder' => esc_html__( 'Enter custom shipping text', 'captain-widgets-kit' ),
				'ai'          => false,
				'condition'   => array(
					'shipping_type' => 'custom',
				),
			)
		);
		$this->add_control(
			'show_label',
			array(
				'label'        => esc_html__( 'Show Label?', 'captain-widgets-kit' ),
				'type'         => \Elementor\Controls_Manager::SWITCHER,
				'label_on'     => esc_html__( 'Yes', 'captain-widgets-kit' ),
				'label_off'    => esc_html__( 'No', 'captain-widgets-kit' ),
				'return_value' => 'yes',
				'default'      => '',
			)
		);
		$this->add_control(
			'label_text',
			array(
				'label'     => esc_html__( 'Label Text', 'captain-widgets-kit' ),
				'type'      => \Elementor\Controls_Manager::TEXT,
				'ai'        => false,
				'default'   => esc_html__( 'Shipping', 'captain-widgets-kit' ),
				'condition' => array(
					'show_label' => 'yes',
				),
			)
		);
		$this->add_control(
			'label_position',
			array(
				'label'     => esc_html__( 'Label Position', 'captain-widgets-kit' ),
				'type'      => \Elementor\Controls_Manager::SELECT,
				'default'   => 'before',
				'options'   => array(
					'before' => esc_html__( 'Before', 'captain-widgets-kit' ),
					'after'  => esc_html__( 'After', 'captain-widgets-kit' ),
				),
				'condition' => array(
					'show_label' => 'yes',
				),
			)
		);
	}

	/**
	 * Render the dynamic product shipping on frontend.
	 *
	 * Gets the current product ID and prints its shipping information.
	 *
	 * @since 6.4.5
	 * @return void
	 */
	public function render(): void {

		$product_id = get_the_ID();
		if ( ! $product_id ) {
			return;
		}

		$product = wc_get_product( $product_id );
		if ( ! $product ) {
			return;
		}

		$shipping_type  = $this->get_settings( 'shipping_type' );
		$dimension_type = $this->get_settings( 'dimension_type' );
		$custom_text    = $this->get_settings( 'custom_text' );

		$show_label     = $this->get_settings( 'show_label' );
		$label_text     = $this->get_settings( 'label_text' );
		$label_position = $this->get_settings( 'label_position' );

		$value = '';

		switch ( $shipping_type ) {

			case 'weight':
																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																									$weight = $product->get_weight();
				if ( $weight ) {
					$value = wc_format_weight( $weight );
				}
				break;

			case 'dimensions':
																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																									$length = $product->get_length();
				$width  = $product->get_width();
				$height = $product->get_height();

				if ( 'length' === $dimension_type ) {
					$value = $length;
				} elseif ( 'width' === $dimension_type ) {
					$value = $width;
				} elseif ( 'height' === $dimension_type ) {
					$value = $height;
				} else {
					$value = wc_format_dimensions( array( $length, $width, $height ) );
				}
				break;

			case 'class':
																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																									$class_slug = $product->get_shipping_class();
				if ( $class_slug ) {
					$term  = get_term_by( 'slug', $class_slug, 'product_shipping_class' );
					$value = $term ? $term->name : $class_slug;
				}
				break;

			case 'custom':
																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																									$value = $custom_text;
				break;
		}

		if ( empty( $value ) ) {
				return;
		}

		if ( 'yes' === $show_label ) {
			if ( 'before' === $label_position ) {
				$value = $label_text . ' ' . $value;
			} else {
				$value = $value . ' ' . $label_text;
			}
		}

		echo wp_kses_post( $value );
	}
}
