<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit; }
use Sifency\Classes\Sifency_Addons_Widget_Base;
use Sifency\Classes\Sifency_Themebuilder_Widget_Base;
use Elementor\Widget_Base;
use Elementor\Controls_Manager;
use Elementor\Group_Control_Typography;
use Elementor\Group_Control_Border;
use Elementor\Group_Control_Box_Shadow;
use Elementor\Plugin as ElementorPlugin;
use Sifency\Traits\Sifency_Addons_Animation_Controls;
use Sifency\Traits\Sifency_Addons_Animation_Manager;
use Sifency\Traits\Sifency_Addons_Button_Style_Controls;
use Sifency\Traits\Sifency_Addons_Help_Controls;
use Sifency\Traits\Sifency_Addons_WooCommerce_Guard;
class Sifency_Widget_Add_To_Cart extends Sifency_Themebuilder_Widget_Base {
	use Sifency_Addons_Animation_Controls;
	use Sifency_Addons_Button_Style_Controls;
	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'          => 'product-addtocart',
			'title'         => 'Sifency Add to Cart',
			'icon'          => 'eicon-product-add-to-cart',
			'category'      => 'sifency-addons-woo-widgets',
			'keywords'      => array( 'add to cart', 'product', 'woocommerce', 'button' ),
			'style_depends' => array( 'sifency-product-single-style' ),
		);}
	private function get_product_for_editor() {
		if ( ElementorPlugin::$instance->documents->get_current() ) {
			$product_id = ElementorPlugin::$instance->documents->get_current()->get_main_id();
			if ( 'product' === get_post_type( $product_id ) ) {
				return wc_get_product( $product_id );
			}
		}
		$args           = array(
			'post_type'      => 'product',
			'posts_per_page' => 1,
		);
		$latest_product = get_posts( $args );
		if ( ! empty( $latest_product ) ) {
			return wc_get_product( $latest_product[0]->ID );
		}
		return false;
	}
	protected function register_controls() {
		if ( ! $this->check_and_guard_woocommerce() ) {
			return;
		}
		$this->start_controls_section( 'section_content', array( 'label' => 'Content' ) );
		$this->add_control(
			'show_quantity',
			array(
				'label'        => __( 'Show Quantity', 'sifency-addons' ),
				'type'         => Controls_Manager::SWITCHER,
				'label_on'     => __( 'Show', 'sifency-addons' ),
				'label_off'    => __( 'Hide', 'sifency-addons' ),
				'return_value' => 'yes',
				'default'      => 'yes',
			)
		);
		$this->end_controls_section();
		$this->add_sifency_addons_help_controls();
		$this->start_controls_section(
			'section_layout_style',
			array(
				'label' => 'Container',
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);
		$this->add_responsive_control(
			'alignment',
			array(
				'label'        => 'Alignment',
				'type'         => Controls_Manager::CHOOSE,
				'options'      => array(
					'left'   => array(
						'title' => 'Left',
						'icon'  => 'eicon-text-align-left',
					),
					'center' => array(
						'title' => 'Center',
						'icon'  => 'eicon-text-align-center',
					),
					'right'  => array(
						'title' => 'Right',
						'icon'  => 'eicon-text-align-right',
					),
				),
				'prefix_class' => 'sifency-add-to-cart-align-%s',
			)
		);
		$this->add_control(
			'layout',
			array(
				'label'        => 'Layout',
				'type'         => Controls_Manager::SELECT,
				'options'      => array(
					'inline'  => 'Inline',
					'stacked' => 'Stacked',
				),
				'default'      => 'inline',
				'prefix_class' => 'sifency-add-to-cart-layout-',
			)
		);
		$this->add_control(
			'button_width',
			array(
				'label'        => __( 'Button Width', 'sifency-addons' ),
				'type'         => Controls_Manager::SELECT,
				'options'      => array(
					'auto' => 'Auto',
					'full' => 'Full Width',
				),
				'default'      => 'auto',
				'prefix_class' => 'sifency-button-width-',
			)
		);
		$this->add_responsive_control(
			'spacing',
			array(
				'label'      => 'Spacing',
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( 'px', 'em' ),
				'selectors'  => array(
					'{{WRAPPER}}.sifency-add-to-cart-layout-inline .quantity' => 'margin-right: {{SIZE}}{{UNIT}}',
					'{{WRAPPER}}.sifency-add-to-cart-layout-stacked .quantity' => 'margin-bottom: {{SIZE}}{{UNIT}}',
				),
				'condition'  => array( 'show_quantity' => 'yes' ),
			)
		);
		$this->end_controls_section();
		$this->add_Sifency_Addons_Button_Style_Controls(
			array(
				'selector' => '.single_add_to_cart_button',
			)
		);
		$this->start_controls_section(
			'section_quantity_style',
			array(
				'label'     => 'Quantity',
				'tab'       => Controls_Manager::TAB_STYLE,
				'condition' => array( 'show_quantity' => 'yes' ),
			)
		);
		$this->add_group_control(
			Group_Control_Typography::get_type(),
			array(
				'name'     => 'quantity_typography',
				'selector' => '{{WRAPPER}} .quantity .qty',
			)
		);
		$this->add_control(
			'quantity_color',
			array(
				'label'     => 'Color',
				'type'      => Controls_Manager::COLOR,
				'selectors' => array( '{{WRAPPER}} .quantity .qty' => 'color: {{VALUE}};' ),
			)
		);
		$this->add_control(
			'quantity_bg_color',
			array(
				'label'     => 'Background',
				'type'      => Controls_Manager::COLOR,
				'selectors' => array( '{{WRAPPER}} .quantity .qty' => 'background-color: {{VALUE}};' ),
			)
		);
		$this->add_group_control(
			Group_Control_Border::get_type(),
			array(
				'name'     => 'quantity_border',
				'selector' => '{{WRAPPER}} .quantity .qty, {{WRAPPER}} .quantity',
			)
		); // Apply to wrapper too for modern look
		$this->add_responsive_control(
			'quantity_width',
			array(
				'label'     => 'Width',
				'type'      => Controls_Manager::SLIDER,
				'selectors' => array( '{{WRAPPER}} .quantity .qty' => 'width: {{SIZE}}px;' ),
			)
		);
		$this->add_responsive_control(
			'quantity_height',
			array(
				'label'     => 'Height',
				'type'      => Controls_Manager::SLIDER,
				'selectors' => array( '{{WRAPPER}} .quantity, {{WRAPPER}} .quantity .qty' => 'height: {{SIZE}}px;' ),
			)
		);
		$this->add_responsive_control(
			'quantity_padding',
			array(
				'label'      => 'Padding',
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', 'em' ),
				'selectors'  => array( '{{WRAPPER}} .quantity .qty' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};' ),
			)
		);
		$this->add_responsive_control(
			'quantity_border_radius',
			array(
				'label'     => 'Border Radius',
				'type'      => Controls_Manager::DIMENSIONS,
				'selectors' => array( '{{WRAPPER}} .quantity, {{WRAPPER}} .quantity .qty' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};' ),
			)
		);
		$this->end_controls_section();
		$this->start_controls_section(
			'section_quantity_stepper_style',
			array(
				'label'     => 'Quantity Stepper (+/-)',
				'tab'       => Controls_Manager::TAB_STYLE,
				'condition' => array( 'show_quantity' => 'yes' ),
			)
		);
		$this->add_control(
			'stepper_font_size',
			array(
				'label'     => 'Font Size',
				'type'      => Controls_Manager::SLIDER,
				'selectors' => array( '{{WRAPPER}} .quantity .plus, {{WRAPPER}} .quantity .minus' => 'font-size: {{SIZE}}px;' ),
			)
		);
		$this->start_controls_tabs( 'stepper_style_tabs' );
		$this->start_controls_tab( 'stepper_normal_tab', array( 'label' => 'Normal' ) );
		$this->add_control(
			'stepper_color',
			array(
				'label'     => 'Color',
				'type'      => Controls_Manager::COLOR,
				'selectors' => array( '{{WRAPPER}} .quantity .plus, {{WRAPPER}} .quantity .minus' => 'color: {{VALUE}};' ),
			)
		);
		$this->add_control(
			'stepper_bg_color',
			array(
				'label'     => 'Background',
				'type'      => Controls_Manager::COLOR,
				'selectors' => array( '{{WRAPPER}} .quantity .plus, {{WRAPPER}} .quantity .minus' => 'background-color: {{VALUE}};' ),
			)
		);
		$this->add_control(
			'stepper_border_color',
			array(
				'label'     => 'Separator Color',
				'type'      => Controls_Manager::COLOR,
				'selectors' => array( '{{WRAPPER}} .quantity .qty' => 'border-left-color: {{VALUE}}; border-right-color: {{VALUE}};' ),
			)
		);
		$this->end_controls_tab();
		$this->start_controls_tab( 'stepper_hover_tab', array( 'label' => 'Hover' ) );
		$this->add_control(
			'stepper_hover_color',
			array(
				'label'     => 'Color',
				'type'      => Controls_Manager::COLOR,
				'selectors' => array( '{{WRAPPER}} .quantity .plus:hover, {{WRAPPER}} .quantity .minus:hover' => 'color: {{VALUE}};' ),
			)
		);
		$this->add_control(
			'stepper_hover_bg_color',
			array(
				'label'     => 'Background',
				'type'      => Controls_Manager::COLOR,
				'selectors' => array( '{{WRAPPER}} .quantity .plus:hover, {{WRAPPER}} .quantity .minus:hover' => 'background-color: {{VALUE}};' ),
			)
		);
		$this->end_controls_tab();
		$this->end_controls_tabs();
		$this->end_controls_section();
		$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'] );
		// Accessing the global $product object from WooCommerce.
		global $product; // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
		if ( ElementorPlugin::$instance->editor->is_edit_mode() ) {
			if ( method_exists( $this, 'get_product_for_editor' ) ) {
				$product = $this->get_product_for_editor(); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
			}
		}
		if ( empty( $product ) ) {
			if ( ElementorPlugin::$instance->editor->is_edit_mode() ) {
				echo '<div class="sifency-placeholder">' . esc_html__( 'Add to Cart button will be displayed here on a single product page.', 'sifency-addons' ) . '</div>';
			}
			return;
		}

		$settings = $this->get_settings_for_display();

		$animation_manager = new Sifency_Addons_Animation_Manager( $settings );
		$wrapper_classes   = array( 'sifency-add-to-cart-wrapper' );

		if ( 'yes' !== $settings['show_quantity'] ) {
			$wrapper_classes[] = 'sifency-quantity-hidden';
		}

		$wrapper_attrs = $animation_manager->get_wrapper_attributes( $wrapper_classes );
		?>
		<div id="<?php echo esc_attr( $wrapper_id ); ?>" <?php echo wp_kses_post( $wrapper_attrs ); ?>>
			<?php
			woocommerce_template_single_add_to_cart();
			?>
		</div>
		<?php
	}
}
