<?php
/**
 * Dynamic Tag: Product Sale.
 *
 * @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 Sale
 *
 * Provides a dynamic tag for Elementor to output the current product sale information.
 *
 * @since 6.4.5
 */
class Captwiki_Tag_Product_Sale 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-sale';
	}

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

		return esc_html__( 'Product Sale', '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(
			'discount_type',
			array(
				'label'   => esc_html__( 'Discount Type', 'captain-widgets-kit' ),
				'type'    => \Elementor\Controls_Manager::SELECT,
				'default' => 'amount',
				'options' => array(
					'amount'  => esc_html__( 'Discount Amount', 'captain-widgets-kit' ),
					'percent' => esc_html__( 'Discount Percentage', 'captain-widgets-kit' ),
					'custom'  => esc_html__( 'Custom', 'captain-widgets-kit' ),
				),
			)
		);
		$this->add_control(
			'custom_text',
			array(
				'label'     => esc_html__( 'Custom Text', 'captain-widgets-kit' ),
				'type'      => \Elementor\Controls_Manager::TEXT,
				'default'   => 'On Sale',
				'ai'        => false,
				'condition' => array(
					'discount_type' => 'custom',
				),
			)
		);
		$this->add_control(
			'show_discount',
			array(
				'label'     => esc_html__( 'Show Discount', 'captain-widgets-kit' ),
				'type'      => \Elementor\Controls_Manager::SWITCHER,
				'label_on'  => esc_html__( 'Yes', 'captain-widgets-kit' ),
				'label_off' => esc_html__( 'No', 'captain-widgets-kit' ),
				'default'   => 'no',
				'condition' => array(
					'discount_type' => 'custom',
				),
			)
		);
		$this->add_control(
			'discount_position',
			array(
				'label'     => esc_html__( 'Discount Position', 'captain-widgets-kit' ),
				'type'      => \Elementor\Controls_Manager::SELECT,
				'default'   => 'after',
				'options'   => array(
					'before' => esc_html__( 'Before Text', 'captain-widgets-kit' ),
					'after'  => esc_html__( 'After Text', 'captain-widgets-kit' ),
				),
				'condition' => array(
					'discount_type' => 'custom',
					'show_discount' => 'yes',
				),
			)
		);
	}

	/**
	 * Render the dynamic product title on frontend.
	 *
	 * Gets the current product ID and prints its title.
	 *
	 * @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;
		}

		$regular = (float) $product->get_regular_price();
		$sale    = (float) $product->get_sale_price();

		if ( $regular <= 0 || $sale <= 0 ) {
			return;
		}

		$discount_amount  = $regular - $sale;
		$discount_percent = round( ( $discount_amount / $regular ) * 100 );

		$type          = $this->get_settings( 'discount_type' );
		$custom_text   = $this->get_settings( 'custom_text' );
		$show_discount = $this->get_settings( 'show_discount' );
		$discount_pos  = $this->get_settings( 'discount_position' );

		switch ( $type ) {

			case 'amount':
																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																echo esc_html( wp_strip_all_tags( wc_price( $discount_amount ) ) );
				break;

			case 'percent':
																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																echo esc_html( $discount_percent . '%' );
				break;

			case 'custom':
																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																$discount = $discount_percent . '%';

				if ( 'yes' !== $show_discount ) {
					echo esc_html( $custom_text );
					return;
				}

				if ( 'before' === $discount_pos ) {
					$output = $discount . ' ' . $custom_text;
				} else {
					$output = $custom_text . ' ' . $discount;
				}

				echo esc_html( $output );
				break;
		}
	}
}
