<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}
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_Animation_Controls;
use Sifency\Traits\Sifency_Animation_Manager;

class Sifency_Widget_Add_To_Cart extends Widget_Base {
    use Sifency_Animation_Controls;

    public function get_name() {
        return 'product-addtocart';
    }

    public function get_title() {
        return __('Sifency Add to Cart', 'sifency-addons');
    }

    public function get_icon() {
        return 'eicon-product-add-to-cart';
    }

    public function get_categories() {
        return ['sifency-addons-woo-widgets'];
    }

    public function get_keywords() {
        return ['product', 'add to cart', 'cart', 'button', 'woocommerce'];
    }

    public function get_style_depends() {
        return [ '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 = [ '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 ( ! class_exists('woocommerce') ) {
            $this->start_controls_section('woo_notice', [ 'label' => 'Notice' ]);
            $this->add_control('woo_notice_control', [ 'type' => Controls_Manager::RAW_HTML, 'raw' => '<div class="elementor-panel-alert elementor-panel-alert-warning">' . esc_html__('WooCommerce is not installed or active.', 'sifency-addons') . '</div>' ]);
            $this->end_controls_section();
            return;
        }

        $this->start_controls_section( 'section_content', [ 'label' => 'Content' ]);
        $this->add_control( 'show_quantity', [
            '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();

        // --- Layout Section ---
        $this->start_controls_section( 'section_layout_style', [ 'label' => 'Container', 'tab' => Controls_Manager::TAB_STYLE ]);
        $this->add_responsive_control( 'alignment', [
            'label' => 'Alignment',
            'type' => Controls_Manager::CHOOSE,
            'options' => [ 'left' => ['title' => 'Left', 'icon' => 'eicon-text-align-left'], 'center' => ['title' => 'Center', 'icon' => 'eicon-text-align-center'], 'right' => ['title' => 'Right', 'icon' => 'eicon-text-align-right'] ],
            'prefix_class' => 'sifency-add-to-cart-align-%s',
        ]);
        $this->add_control( 'layout', [
            'label' => 'Layout',
            'type' => Controls_Manager::SELECT,
            'options' => [ 'inline' => 'Inline', 'stacked' => 'Stacked' ],
            'default' => 'inline',
            'prefix_class' => 'sifency-add-to-cart-layout-',
        ]);
        $this->add_control('button_width', [
            'label' => __('Button Width', 'sifency-addons'),
            'type' => Controls_Manager::SELECT,
            'options' => [ 'auto' => 'Auto', 'full' => 'Full Width' ],
            'default' => 'auto',
            'prefix_class' => 'sifency-button-width-',
        ]);
        $this->add_responsive_control( 'spacing', [
            'label' => 'Spacing',
            'type' => Controls_Manager::SLIDER,
            'size_units' => ['px', 'em'],
            'selectors' => [
                '{{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' => [ 'show_quantity' => 'yes' ],
        ]);
        $this->end_controls_section();

        // --- Button Styling ---
        $this->start_controls_section( 'section_button_style', [ 'label' => 'Button', 'tab' => Controls_Manager::TAB_STYLE ]);
        $this->add_group_control( Group_Control_Typography::get_type(), [ 'name' => 'button_typography', 'selector' => '{{WRAPPER}} .single_add_to_cart_button' ] );
        $this->start_controls_tabs('button_style_tabs');
        $this->start_controls_tab( 'button_normal_tab', ['label' => 'Normal'] );
        $this->add_control( 'button_color', [ 'label' => 'Text Color', 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .single_add_to_cart_button' => 'color: {{VALUE}};' ] ]);
        $this->add_control( 'button_bg_color', [ 'label' => 'Background', 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .single_add_to_cart_button' => 'background-color: {{VALUE}};' ] ]);
        $this->add_group_control( Group_Control_Border::get_type(), [ 'name' => 'button_border', 'selector' => '{{WRAPPER}} .single_add_to_cart_button' ] );
        $this->add_group_control( Group_Control_Box_Shadow::get_type(), [ 'name' => 'button_box_shadow', 'selector' => '{{WRAPPER}} .single_add_to_cart_button' ] );
        $this->end_controls_tab();
        $this->start_controls_tab( 'button_hover_tab', ['label' => 'Hover'] );
        $this->add_control( 'button_hover_color', [ 'label' => 'Text Color', 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .single_add_to_cart_button:hover' => 'color: {{VALUE}};' ] ]);
        $this->add_control( 'button_hover_bg_color', [ 'label' => 'Background', 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .single_add_to_cart_button:hover' => 'background-color: {{VALUE}};' ] ]);
        $this->add_control( 'button_hover_border_color', [ 'label' => 'Border Color', 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .single_add_to_cart_button:hover' => 'border-color: {{VALUE}};' ], 'condition' => [ 'button_border_border!' => '' ] ]);
        $this->add_group_control( Group_Control_Box_Shadow::get_type(), [ 'name' => 'button_hover_box_shadow', 'selector' => '{{WRAPPER}} .single_add_to_cart_button:hover' ] );
        $this->end_controls_tab();
        $this->end_controls_tabs();
        $this->add_responsive_control( 'button_padding', [ 'label' => 'Padding', 'type' => Controls_Manager::DIMENSIONS, 'size_units' => ['px', 'em', '%'], 'selectors' => [ '{{WRAPPER}} .single_add_to_cart_button' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};' ], 'separator' => 'before' ]);
        $this->add_responsive_control( 'button_border_radius', [ 'label' => 'Border Radius', 'type' => Controls_Manager::DIMENSIONS, 'selectors' => [ '{{WRAPPER}} .single_add_to_cart_button' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};' ] ]);
        $this->end_controls_section();
        
        // --- Quantity Input Styling ---
        $this->start_controls_section( 'section_quantity_style', [ 'label' => 'Quantity', 'tab' => Controls_Manager::TAB_STYLE, 'condition' => [ 'show_quantity' => 'yes' ] ]);
        $this->add_group_control( Group_Control_Typography::get_type(), [ 'name' => 'quantity_typography', 'selector' => '{{WRAPPER}} .quantity .qty' ] );
        $this->add_control( 'quantity_color', [ 'label' => 'Color', 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .quantity .qty' => 'color: {{VALUE}};' ] ]);
        $this->add_control( 'quantity_bg_color', [ 'label' => 'Background', 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .quantity .qty' => 'background-color: {{VALUE}};' ] ]);
        $this->add_group_control( Group_Control_Border::get_type(), [ 'name' => 'quantity_border', 'selector' => '{{WRAPPER}} .quantity .qty, {{WRAPPER}} .quantity' ] ); // Apply to wrapper too for modern look
        $this->add_responsive_control( 'quantity_width', [ 'label' => 'Width', 'type' => Controls_Manager::SLIDER, 'selectors' => ['{{WRAPPER}} .quantity .qty' => 'width: {{SIZE}}px;'] ]);
        $this->add_responsive_control( 'quantity_height', [ 'label' => 'Height', 'type' => Controls_Manager::SLIDER, 'selectors' => ['{{WRAPPER}} .quantity, {{WRAPPER}} .quantity .qty' => 'height: {{SIZE}}px;'] ]);
        $this->add_responsive_control( 'quantity_padding', [ 'label' => 'Padding', 'type' => Controls_Manager::DIMENSIONS, 'size_units' => ['px', 'em'], 'selectors' => [ '{{WRAPPER}} .quantity .qty' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};' ] ]);
        $this->add_responsive_control( 'quantity_border_radius', [ 'label' => 'Border Radius', 'type' => Controls_Manager::DIMENSIONS, 'selectors' => [ '{{WRAPPER}} .quantity, {{WRAPPER}} .quantity .qty' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};' ] ]);
        $this->end_controls_section();

        // --- Quantity Stepper (+/-) Styling ---
        $this->start_controls_section( 'section_quantity_stepper_style', [ 'label' => 'Quantity Stepper (+/-)', 'tab' => Controls_Manager::TAB_STYLE, 'condition' => [ 'show_quantity' => 'yes' ] ]);
        $this->add_control( 'stepper_font_size', [ 'label' => 'Font Size', 'type' => Controls_Manager::SLIDER, 'selectors' => ['{{WRAPPER}} .quantity .plus, {{WRAPPER}} .quantity .minus' => 'font-size: {{SIZE}}px;'] ]);
        $this->start_controls_tabs('stepper_style_tabs');
        $this->start_controls_tab( 'stepper_normal_tab', ['label' => 'Normal'] );
        $this->add_control( 'stepper_color', [ 'label' => 'Color', 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .quantity .plus, {{WRAPPER}} .quantity .minus' => 'color: {{VALUE}};' ] ]);
        $this->add_control( 'stepper_bg_color', [ 'label' => 'Background', 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .quantity .plus, {{WRAPPER}} .quantity .minus' => 'background-color: {{VALUE}};' ] ]);
        $this->add_control( 'stepper_border_color', [ 'label' => 'Separator Color', 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .quantity .qty' => 'border-left-color: {{VALUE}}; border-right-color: {{VALUE}};' ] ]);
        $this->end_controls_tab();
        $this->start_controls_tab( 'stepper_hover_tab', ['label' => 'Hover'] );
        $this->add_control( 'stepper_hover_color', [ 'label' => 'Color', 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .quantity .plus:hover, {{WRAPPER}} .quantity .minus:hover' => 'color: {{VALUE}};' ] ]);
        $this->add_control( 'stepper_hover_bg_color', [ 'label' => 'Background', 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{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_animation_controls();
        $this->update_control(
            'sifency_animation_target',
            [ 'type' => Controls_Manager::HIDDEN, 'default' => 'wrapper' ]
        );
    }

    protected function render() {
        // 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_Animation_Manager($settings);
        $wrapper_classes = ['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 <?php echo wp_kses_post( $wrapper_attrs ); ?><?php  ?>>
            <?php
            woocommerce_template_single_add_to_cart();
            ?>
        </div>
        <?php
    }
}