<?php
namespace Sifency\Traits;
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}
use Elementor\Widget_Base;
use Elementor\Controls_Manager;
use Elementor\Repeater;
use Elementor\Utils;
use Elementor\Group_Control_Typography;
use Elementor\Group_Control_Background;
use Elementor\Group_Control_Border;
use Elementor\Group_Control_Box_Shadow;

trait Sifency_Product_Slider_Controls {
	protected function add_sifency_product_slider_controls( $args = [] ) {
		
        // --- Query Section ---
        $this->start_controls_section( 'section_query', [ 'label' => 'Query' ] );
        $this->add_control( 'source', [ 'label' => 'Source', 'type' => Controls_Manager::SELECT, 'default' => 'products', 'options' => [ 'products' => 'Latest Products', 'sale' => 'Sale', 'featured' => 'Featured' ] ]);
        $this->add_control( 'posts_per_page', [ 'label' => 'Products per Page', 'type' => Controls_Manager::NUMBER, 'default' => 8 ] );
        $this->add_control( 'orderby', [ 'label' => 'Order By', 'type' => Controls_Manager::SELECT, 'default' => 'date', 'options' => [ 'date' => 'Date', 'title' => 'Title', 'price' => 'Price', 'rand' => 'Random' ] ]);
        $this->add_control( 'order', [ 'label' => 'Order', 'type' => Controls_Manager::SELECT, 'default' => 'DESC', 'options' => [ 'ASC' => 'ASC', 'DESC' => 'DESC' ] ]);
        $this->end_controls_section();
        
        $this->start_controls_section('section_layout_settings', [
            'label' => __('Layout Settings', 'sifency-addons'),
        ]);

        $this->add_control('show_sale_flash', [
            'label' => __('Show Sale Flash', 'sifency-addons'),
            'type' => Controls_Manager::SWITCHER,
            'label_on' => __('Show', 'sifency-addons'),
            'label_off' => __('Hide', 'sifency-addons'),
            'return_value' => 'yes',
            'default' => 'yes',
        ]);

        $this->add_control('show_rating', [
            'label' => __('Show Rating', 'sifency-addons'),
            'type' => Controls_Manager::SWITCHER,
            'label_on' => __('Show', 'sifency-addons'),
            'label_off' => __('Hide', 'sifency-addons'),
            'return_value' => 'yes',
            'default' => 'yes',
        ]);

        $this->add_control('show_price', [
            'label' => __('Show Price', 'sifency-addons'),
            'type' => Controls_Manager::SWITCHER,
            'label_on' => __('Show', 'sifency-addons'),
            'label_off' => __('Hide', 'sifency-addons'),
            'return_value' => 'yes',
            'default' => 'yes',
        ]);

        $this->add_control('show_add_to_cart', [
            'label' => __('Show Add to Cart', '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_carousel_controls();
        
        //==================================================
        // Style Tab
        //==================================================

        // --- Card/Box Style Section ---
        $this->start_controls_section('section_card_style', [
            'label' => __('Card / Box', 'sifency-addons'),
            'tab'   => Controls_Manager::TAB_STYLE,
        ]);

        $this->add_control('card_bg_color', [
            'label' => __('Background Color', 'sifency-addons'),
            'type' => Controls_Manager::COLOR,
            'selectors' => [
                '{{WRAPPER}} .product' => 'background-color: {{VALUE}};',
            ],
        ]);
        
        $this->add_responsive_control('card_padding', [
            'label' => __('Padding', 'sifency-addons'),
            'type' => Controls_Manager::DIMENSIONS,
            'size_units' => ['px', '%', 'em'],
            'selectors' => [
                '{{WRAPPER}} .product' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
            ],
        ]);

        $this->add_group_control(Group_Control_Border::get_type(), [
            'name' => 'card_border',
            'label' => __('Border', 'sifency-addons'),
            'selector' => '{{WRAPPER}} .product',
        ]);

        $this->add_responsive_control('card_border_radius', [
            'label' => __('Border Radius', 'sifency-addons'),
            'type' => Controls_Manager::DIMENSIONS,
            'size_units' => ['px', '%'],
            'selectors' => [
                '{{WRAPPER}} .product' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
            ],
        ]);
        
        $this->add_group_control(Group_Control_Box_Shadow::get_type(), [
            'name' => 'card_box_shadow',
            'selector' => '{{WRAPPER}} .product',
        ]);
        
        $this->add_responsive_control('content_alignment', [
            'label' => __('Content Alignment', 'sifency-addons'),
            'type' => Controls_Manager::CHOOSE,
            'options' => [
                'left' => ['title' => __('Left', 'sifency-addons'), 'icon' => 'eicon-text-align-left'],
                'center' => ['title' => __('Center', 'sifency-addons'), 'icon' => 'eicon-text-align-center'],
                'right' => ['title' => __('Right', 'sifency-addons'), 'icon' => 'eicon-text-align-right'],
            ],
            'selectors' => [
                '{{WRAPPER}} .product' => 'text-align: {{VALUE}};',
            ],
        ]);

        $this->end_controls_section();
        
        // --- Sale Flash Style Section ---
        $this->start_controls_section('section_sale_flash_style', [
            'label' => __('Sale Flash', 'sifency-addons'),
            'tab'   => Controls_Manager::TAB_STYLE,
            'condition' => ['show_sale_flash' => 'yes'],
        ]);

        $this->add_control('sale_flash_bg_color', [
            'label' => __('Background Color', 'sifency-addons'),
            'type' => Controls_Manager::COLOR,
            'selectors' => [
                '{{WRAPPER}} .product .onsale' => 'background-color: {{VALUE}};',
            ],
        ]);
        
        $this->add_control('sale_flash_text_color', [
            'label' => __('Text Color', 'sifency-addons'),
            'type' => Controls_Manager::COLOR,
            'selectors' => [
                '{{WRAPPER}} .product .onsale' => 'color: {{VALUE}};',
            ],
        ]);

        $this->add_group_control(Group_Control_Typography::get_type(), [
            'name' => 'sale_flash_typography',
            'selector' => '{{WRAPPER}} .product .onsale',
        ]);

        $this->add_responsive_control('sale_flash_padding', [
            'label' => __('Padding', 'sifency-addons'),
            'type' => Controls_Manager::DIMENSIONS,
            'size_units' => ['px', 'em'],
            'selectors' => [
                '{{WRAPPER}} .product .onsale' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
            ],
        ]);

        $this->add_responsive_control('sale_flash_border_radius', [
            'label' => __('Border Radius', 'sifency-addons'),
            'type' => Controls_Manager::DIMENSIONS,
            'size_units' => ['px', '%'],
            'selectors' => [
                '{{WRAPPER}} .product .onsale' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
            ],
        ]);

        $this->end_controls_section();

        // --- Image Style Section ---
        $this->start_controls_section('section_image_style', [
            'label' => __('Image', 'sifency-addons'),
            'tab'   => Controls_Manager::TAB_STYLE,
        ]);
        
        $this->add_group_control(Group_Control_Border::get_type(), [
            'name' => 'image_border',
            'selector' => '{{WRAPPER}} .woocommerce-loop-product__link img',
        ]);
        
        $this->add_responsive_control('image_border_radius', [
            'label' => __('Border Radius', 'sifency-addons'),
            'type' => Controls_Manager::DIMENSIONS,
            'size_units' => ['px', '%'],
            'selectors' => [
                '{{WRAPPER}} .woocommerce-loop-product__link img' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
            ],
        ]);

        $this->add_responsive_control('image_spacing', [
            'label' => __('Spacing', 'sifency-addons'),
            'type' => Controls_Manager::SLIDER,
            'size_units' => ['px', 'em'],
            'range' => ['px' => ['min' => 0, 'max' => 100]],
            'selectors' => [
                '{{WRAPPER}} .woocommerce-loop-product__link' => 'margin-bottom: {{SIZE}}{{UNIT}};',
            ],
        ]);

        $this->end_controls_section();

        // --- Title Style Section ---
        $this->start_controls_section('section_title_style', [
            'label' => __('Title', 'sifency-addons'),
            'tab'   => Controls_Manager::TAB_STYLE,
        ]);

        $this->add_control('title_color', [
            'label' => __('Color', 'sifency-addons'),
            'type' => Controls_Manager::COLOR,
            'selectors' => [
                '{{WRAPPER}} .woocommerce-loop-product__title' => 'color: {{VALUE}};',
            ],
        ]);
        
        $this->add_control('title_hover_color', [
            'label' => __('Hover Color', 'sifency-addons'),
            'type' => Controls_Manager::COLOR,
            'selectors' => [
                '{{WRAPPER}} .product:hover .woocommerce-loop-product__title' => 'color: {{VALUE}};',
            ],
        ]);
        
        $this->add_group_control(Group_Control_Typography::get_type(), [
            'name' => 'title_typography',
            'selector' => '{{WRAPPER}} .woocommerce-loop-product__title',
        ]);

        $this->add_responsive_control('title_spacing', [
            'label' => __('Spacing', 'sifency-addons'),
            'type' => Controls_Manager::SLIDER,
            'size_units' => ['px', 'em'],
            'range' => ['px' => ['min' => 0, 'max' => 100]],
            'selectors' => [
                '{{WRAPPER}} .woocommerce-loop-product__title' => 'margin-bottom: {{SIZE}}{{UNIT}};',
            ],
        ]);

        $this->end_controls_section();

        // --- Rating Style Section ---
        $this->start_controls_section('section_rating_style', [
            'label' => __('Rating', 'sifency-addons'),
            'tab'   => Controls_Manager::TAB_STYLE,
            'condition' => ['show_rating' => 'yes'],
        ]);

        $this->add_control('rating_color', [
            'label' => __('Star Color', 'sifency-addons'),
            'type' => Controls_Manager::COLOR,
            'selectors' => [
                '{{WRAPPER}} .star-rating span::before' => 'color: {{VALUE}};',
            ],
        ]);

        $this->add_control('rating_empty_color', [
            'label' => __('Empty Star Color', 'sifency-addons'),
            'type' => Controls_Manager::COLOR,
            'selectors' => [
                '{{WRAPPER}} .star-rating::before' => 'color: {{VALUE}};',
            ],
        ]);

        $this->add_responsive_control('rating_spacing', [
            'label' => __('Spacing', 'sifency-addons'),
            'type' => Controls_Manager::SLIDER,
            'size_units' => ['px', 'em'],
            'range' => ['px' => ['min' => 0, 'max' => 100]],
            'selectors' => [
                '{{WRAPPER}} .star-rating' => 'margin-bottom: {{SIZE}}{{UNIT}};',
            ],
        ]);

        $this->end_controls_section();

        // --- Price Style Section ---
        $this->start_controls_section('section_price_style', [
            'label' => __('Price', 'sifency-addons'),
            'tab'   => Controls_Manager::TAB_STYLE,
            'condition' => ['show_price' => 'yes'],
        ]);

        $this->add_control('price_color', [
            'label' => __('Color', 'sifency-addons'),
            'type' => Controls_Manager::COLOR,
            'selectors' => [
                '{{WRAPPER}} .price, {{WRAPPER}} .price .amount' => 'color: {{VALUE}};',
            ],
        ]);

        $this->add_control('sale_price_color', [
            'label' => __('Sale Price Color', 'sifency-addons'),
            'type' => Controls_Manager::COLOR,
            'selectors' => [
                '{{WRAPPER}} .price ins, {{WRAPPER}} .price ins .amount' => 'color: {{VALUE}};',
            ],
        ]);

        $this->add_control('regular_price_color', [
            'label' => __('Regular Price Color (strikethrough)', 'sifency-addons'),
            'type' => Controls_Manager::COLOR,
            'selectors' => [
                '{{WRAPPER}} .price del, {{WRAPPER}} .price del .amount' => 'color: {{VALUE}};',
            ],
        ]);

        $this->add_group_control(Group_Control_Typography::get_type(), [
            'name' => 'price_typography',
            'selector' => '{{WRAPPER}} .price',
        ]);
        
        $this->add_responsive_control('price_spacing', [
            'label' => __('Spacing', 'sifency-addons'),
            'type' => Controls_Manager::SLIDER,
            'size_units' => ['px', 'em'],
            'range' => ['px' => ['min' => 0, 'max' => 100]],
            'selectors' => [
                '{{WRAPPER}} .price' => 'margin-bottom: {{SIZE}}{{UNIT}};',
            ],
        ]);

        $this->end_controls_section();

        // --- Add to Cart Button Style Section ---
        $this->start_controls_section('section_button_style', [
            'label' => __('Add to Cart Button', 'sifency-addons'),
            'tab'   => Controls_Manager::TAB_STYLE,
            'condition' => ['show_add_to_cart' => 'yes'],
        ]);

        $this->start_controls_tabs('tabs_button_style');

        // --- Normal Tab ---
        $this->start_controls_tab('tab_button_normal', ['label' => __('Normal', 'sifency-addons')]);
        
        $this->add_control('button_text_color', [
            'label' => __('Text Color', 'sifency-addons'),
            'type' => Controls_Manager::COLOR,
            'selectors' => [
                '{{WRAPPER}} .product .button' => 'color: {{VALUE}};',
            ],
        ]);

        $this->add_control('button_bg_color', [
            'label' => __('Background Color', 'sifency-addons'),
            'type' => Controls_Manager::COLOR,
            'selectors' => [
                '{{WRAPPER}} .product .button' => 'background-color: {{VALUE}};',
            ],
        ]);

        $this->add_group_control(Group_Control_Border::get_type(), [
            'name' => 'button_border',
            'selector' => '{{WRAPPER}} .product .button',
        ]);

        $this->end_controls_tab();

        // --- Hover Tab ---
        $this->start_controls_tab('tab_button_hover', ['label' => __('Hover', 'sifency-addons')]);

        $this->add_control('button_hover_text_color', [
            'label' => __('Text Color', 'sifency-addons'),
            'type' => Controls_Manager::COLOR,
            'selectors' => [
                '{{WRAPPER}} .product .button:hover, {{WRAPPER}} .product .button:focus' => 'color: {{VALUE}};',
            ],
        ]);

        $this->add_control('button_hover_bg_color', [
            'label' => __('Background Color', 'sifency-addons'),
            'type' => Controls_Manager::COLOR,
            'selectors' => [
                '{{WRAPPER}} .product .button:hover, {{WRAPPER}} .product .button:focus' => 'background-color: {{VALUE}};',
            ],
        ]);

        $this->add_control('button_hover_border_color', [
            'label' => __('Border Color', 'sifency-addons'),
            'type' => Controls_Manager::COLOR,
            'selectors' => [
                '{{WRAPPER}} .product .button:hover, {{WRAPPER}} .product .button:focus' => 'border-color: {{VALUE}};',
            ],
        ]);
        
        $this->end_controls_tab();
        $this->end_controls_tabs();

        $this->add_group_control(Group_Control_Typography::get_type(), [
            'name' => 'button_typography',
            'selector' => '{{WRAPPER}} .product .button',
            'separator' => 'before',
        ]);

        $this->add_responsive_control('button_border_radius', [
            'label' => __('Border Radius', 'sifency-addons'),
            'type' => Controls_Manager::DIMENSIONS,
            'size_units' => ['px', '%'],
            'selectors' => [
                '{{WRAPPER}} .product .button' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
            ],
        ]);

        $this->add_responsive_control('button_padding', [
            'label' => __('Padding', 'sifency-addons'),
            'type' => Controls_Manager::DIMENSIONS,
            'size_units' => ['px', 'em'],
            'selectors' => [
                '{{WRAPPER}} .product .button' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
            ],
        ]);

        $this->end_controls_section();
	}
}