<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit; }
use Sifency\Classes\Sifency_Addons_Widget_Base;
use Sifency\Classes\Sifency_Themebuilder_Widget_Base;
use Elementor\Controls_Manager;
use Elementor\Group_Control_Typography;
use Sifency\Traits\Sifency_Addons_Animation_Controls;
use Sifency\Traits\Sifency_Addons_Animation_Manager;
use Sifency\Traits\Sifency_Addons_Master_Typography;
use Sifency\Traits\Sifency_Addons_Help_Controls;
use Sifency\Traits\Sifency_Addons_WooCommerce_Guard;
class Sifency_Widget_Price extends Sifency_Themebuilder_Widget_Base {
	use Sifency_Addons_Animation_Controls;
	use Sifency_Addons_Master_Typography;
	use Sifency_Addons_Help_Controls;
	use Sifency_Addons_WooCommerce_Guard;

	protected function get_allowed_template_types() {
		return 'product';
	}
	public function __construct( $data = array(), $args = null ) {
		parent::__construct( $data, $args );
		$this->wg_config = array(
			'name'          => 'sifency-product-price',
			'title'         => 'Sifency Product Price',
			'icon'          => 'eicon-price-list',
			'category'      => 'sifency-addons-woo-widgets',
			'style_depends' => array( 'sifency-frontend-style' ),
		);}

	protected function register_controls() {
		if ( ! $this->check_and_guard_woocommerce() ) {
			return;
		}
		$this->start_controls_section(
			'section_content_preview',
			array( 'label' => __( 'Preview Settings', 'sifency-addons' ) )
		);

		if ( \Elementor\Plugin::$instance->editor->is_edit_mode() ) {
			$this->add_control(
				'editor_preview_type',
				array(
					'label'       => __( 'Editor Preview Type', 'sifency-addons' ),
					'type'        => Controls_Manager::SELECT,
					'default'     => 'simple',
					'options'     => array(
						'simple'   => __( 'Simple Price', 'sifency-addons' ),
						'sale'     => __( 'Sale Price', 'sifency-addons' ),
						'variable' => __( 'Variable Price (Range)', 'sifency-addons' ),
					),
					'description' => __( 'This control is for preview purposes only.', 'sifency-addons' ),
				)
			);

			$this->add_control(
				'editor_regular_price',
				array(
					'label'     => __( 'Preview Regular Price', 'sifency-addons' ),
					'type'      => Controls_Manager::NUMBER,
					'default'   => 25.00,
					'min'       => 0,
					'step'      => 0.01,
					'condition' => array(
						'editor_preview_type' => array( 'simple', 'sale' ),
					),
				)
			);

			$this->add_control(
				'editor_sale_price',
				array(
					'label'     => __( 'Preview Sale Price', 'sifency-addons' ),
					'type'      => Controls_Manager::NUMBER,
					'default'   => 18.00,
					'min'       => 0,
					'step'      => 0.01,
					'condition' => array(
						'editor_preview_type' => 'sale',
					),
				)
			);
		}

		$this->add_responsive_control(
			'align',
			array(
				'label'     => __( 'Alignment', 'sifency-addons' ),
				'type'      => Controls_Manager::CHOOSE,
				'options'   => array(
					'left'   => array(
						'title' => __( 'Left', 'sifency-addons' ),
						'icon'  => 'eicon-text-align-left',
					),
					'center' => array(
						'title' => __( 'Center', 'sifency-addons' ),
						'icon'  => 'eicon-text-align-center',
					),
					'right'  => array(
						'title' => __( 'Right', 'sifency-addons' ),
						'icon'  => 'eicon-text-align-right',
					),
				),
				'selectors' => array( '{{WRAPPER}} .sifency-product-price' => 'text-align: {{VALUE}}' ),
			)
		);
		$this->end_controls_section();
		$this->add_sifency_addons_help_controls();

		// --- Style Section: Price ---
		$this->start_controls_section(
			'price_style_section',
			array(
				'label' => __( 'Price Styling', 'sifency-addons' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);

		// --- REGULAR PRICE ---
		$this->add_control(
			'regular_price_heading',
			array(
				'label' => __( 'Regular Price', 'sifency-addons' ),
				'type'  => Controls_Manager::HEADING,
			)
		);
		$this->add_control(
			'regular_price_color',
			array(
				'label'     => __( 'Color', 'sifency-addons' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array( '{{WRAPPER}} .sifency-product-price .price, {{WRAPPER}} .sifency-product-price .price del' => 'color: {{VALUE}}' ),
			)
		);
		$this->add_group_control(
			Group_Control_Typography::get_type(),
			array(
				'name'     => 'regular_price_typography',
				'selector' => '{{WRAPPER}} .sifency-product-price .price, {{WRAPPER}} .sifency-product-price .price del .amount',
			)
		);

		// --- SALE PRICE ---
		$this->add_control(
			'sale_price_heading',
			array(
				'label'     => __( 'Sale Price', 'sifency-addons' ),
				'type'      => Controls_Manager::HEADING,
				'separator' => 'before',
			)
		);
		$this->add_control(
			'sale_price_color',
			array(
				'label'     => __( 'Color', 'sifency-addons' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array( '{{WRAPPER}} .sifency-product-price .price ins .amount' => 'color: {{VALUE}}' ),
			)
		);
		$this->add_group_control(
			Group_Control_Typography::get_type(),
			array(
				'name'     => 'sale_price_typography',
				'selector' => '{{WRAPPER}} .sifency-product-price .price ins .amount',
			)
		);
		$this->add_responsive_control(
			'sale_price_spacing',
			array(
				'label'       => __( 'Spacing', 'sifency-addons' ),
				'type'        => Controls_Manager::SLIDER,
				'size_units'  => array( 'px', 'em' ),
				'range'       => array(
					'px' => array(
						'min' => 0,
						'max' => 50,
					),
				),
				'selectors'   => array(
					'{{WRAPPER}} .sifency-product-price .price del' => 'margin-right: {{SIZE}}{{UNIT}};',
				),
				'description' => __( 'Space between regular price and sale price.', 'sifency-addons' ),
			)
		);
		$this->end_controls_section();

		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'   => 'content_style',
				'label'    => 'Price Style',
				'selector' => '.sifency-content-style',
			)
		);
		$this->add_Sifency_Addons_Animation_Controls();

		$this->update_control(
			'sifency_animation_target',
			array(
				'type'    => Controls_Manager::HIDDEN,
				'default' => 'wrapper',
			)
		);
	}
	protected function render_dom( array $context ) {
		$wrapper_id = esc_attr( $context['dom_id'] );
		if ( \Elementor\Plugin::$instance->editor->is_edit_mode() ) {
			$this->render_editor_preview();
			return;
		}

		$this->render_live_price();
	}

	private function render_live_price() {
		$product = wc_get_product();
		if ( empty( $product ) ) {
			return; }
		$this->render_html( $product->get_price_html() );
	}

	private function render_editor_preview() {
		$settings     = $this->get_settings_for_display();
		$preview_type = $settings['editor_preview_type'];
		$price_html   = '';

		$regular_price = $settings['editor_regular_price'] ?? '25.00';
		$sale_price    = $settings['editor_sale_price'] ?? '18.00';

		switch ( $preview_type ) {
			case 'sale':
				$formatted_regular_price = wc_price( $regular_price );
				$formatted_sale_price    = wc_price( $sale_price );

				$price_html = sprintf(
					'<p class="price"><del aria-hidden="true">%1$s</del> <ins>%2$s</ins></p>',
					$formatted_regular_price,
					$formatted_sale_price
				);
				break;

			case 'variable':
				$price_html = sprintf(
					'<p class="price">%1$s &ndash; %2$s</p>',
					wc_price( 10.00 ),
					wc_price( 30.00 )
				);
				break;

			case 'simple':
			default:
				$price_html = sprintf(
					'<p class="price">%s</p>',
					wc_price( $regular_price )
				);
				break;
		}

		$this->render_html( $price_html );
	}

	private function render_html( $price_content ) {
		if ( empty( $price_content ) ) {
			if ( \Elementor\Plugin::$instance->editor->is_edit_mode() ) {
				echo '<div class="sifency-placeholder">' . esc_html__( 'Product Price', 'sifency-addons' ) . '</div>';
			}
			return;
		}
		$settings          = $this->get_settings_for_display();
		$animation_manager = new Sifency_Addons_Animation_Manager( $settings );
		$wrapper_classes   = array( 'sifency-product-price', 'sifency-content-style' );
		$wrapper_attrs     = $animation_manager->get_wrapper_attributes( $wrapper_classes );
		?>
		<div <?php echo wp_kses_post( $wrapper_attrs ); ?>>
			<?php echo wp_kses_post( $price_content ); ?>
		</div>
		<?php
	}
}
