<?php
namespace Sifency\Traits;
if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}

use Elementor\Controls_Manager;
use Elementor\Group_Control_Background;
use Elementor\Group_Control_Border;
use Elementor\Group_Control_Box_Shadow;
use Elementor\Group_Control_Image_Size;
use Elementor\Group_Control_Typography;
use Elementor\Group_Control_Css_Filter;

trait Sifency_Marquee_Controls {

	protected function add_sifency_marquee_controls( $args = [] ) {
        $this->start_controls_section(
            'content_section',
            [
                'label' => esc_html__('Content', 'sifency-addons'),
                'tab' => Controls_Manager::TAB_CONTENT,
            ]
        );
        $repeater = new \Elementor\Repeater();
        
        $repeater->add_control(
            'icon',
            [
                'label' => esc_html__('Icon', 'sifency-addons'),
                'type' => Controls_Manager::ICONS,
            ]
        );

        $repeater->add_control(
            'item_image',
            [
                'label' => esc_html__('Image', 'sifency-addons'),
                'type' => Controls_Manager::MEDIA,
            ]
        );

        $repeater->add_group_control(
            Group_Control_Image_Size::get_type(),
            [
                'name' => 'item_image',
                'default' => 'thumbnail',
                'condition' => [
                    'item_image[url]!' => '',
                ],
            ]
        );

        $repeater->add_control(
            'text',
            [
                'label' => esc_html__('Text', 'sifency-addons'),
                'type' => Controls_Manager::TEXT,
                'default' => esc_html__('Default Text', 'sifency-addons'),
            ]
        );

        $repeater->add_control(
            'item_link',
            [
                'label' => esc_html__('Link', 'sifency-addons'),
                'type' => Controls_Manager::URL,
                'placeholder' => esc_html__('https://your-link.com', 'sifency-addons'),
                'show_external' => true,
                'default' => [
                    'url' => '',
                    'is_external' => false,
                    'nofollow' => false,
                ],
            ]
        );

        $this->add_control(
            'text_list',
            [
                'label' => esc_html__('Text Items', 'sifency-addons'),
                'type' => Controls_Manager::REPEATER,
                'fields' => $repeater->get_controls(),
                'default' => [
                    [
                        'text' => esc_html__('Default Text 1', 'sifency-addons'),
                    ],
                    [
                        'text' => esc_html__('Default Text 2', 'sifency-addons'),
                    ],
                ],
                'title_field' => '{{{ text }}}',
            ]
        );

        $this->add_control(
            'slider_direction',
            [
                'label' => esc_html__('Slider Direction', 'sifency-addons'),
                'type' => Controls_Manager::SELECT,
                'default' => 'left',
                'options' => [
                    'left' => esc_html__('Right to Left', 'sifency-addons'),
                    'right' => esc_html__('Left to Right', 'sifency-addons'),
                    'top' => esc_html__('Bottom to Top', 'sifency-addons'),
                    'bottom' => esc_html__('Top to Bottom', 'sifency-addons'),
                ],
                'separator' => 'before',
            ]
        );

        $this->add_control(
            'pause_on_hover',
            [
                'label' => esc_html__('Pause on Hover', 'sifency-addons'),
                'type' => Controls_Manager::SWITCHER,
                'label_on' => esc_html__('Yes', 'sifency-addons'),
                'label_off' => esc_html__('No', 'sifency-addons'),
                'return_value' => 'yes',
                'default' => 'yes',
            ]
        );

        $this->add_control(
            'loop_count',
            [
                'label' => esc_html__('Loop Count', 'sifency-addons'),
                'type' => Controls_Manager::NUMBER,
                'min' => 0,
                'max' => 100,
                'step' => 1,
                'default' => 0,
                'description' => esc_html__('0 for infinite loops', 'sifency-addons'),
            ]
        );

        $this->add_control(
            'separator_type',
            [
                'label' => esc_html__('Separator Type', 'sifency-addons'),
                'type' => Controls_Manager::SELECT,
                'default' => 'icon',
                'options' => [
                    'none' => esc_html__('None', 'sifency-addons'),
                    'icon' => esc_html__('Icon', 'sifency-addons'),
                    'text' => esc_html__('Text', 'sifency-addons'),
                    'image' => esc_html__('Image', 'sifency-addons'),
                ],
            ]
        );

        $this->add_control(
            'separator_text',
            [
                'label' => esc_html__('Separator Text', 'sifency-addons'),
                'type' => Controls_Manager::TEXT,
                'default' => '•',
                'condition' => [
                    'separator_type' => 'text',
                ],
            ]
        );

        $this->add_control(
            'separator_icon',
            [
                'label' => esc_html__('Separator Icon', 'sifency-addons'),
                'type' => Controls_Manager::ICONS,
                'default' => [
                    'value' => 'fas fa-star',
                    'library' => 'solid',
                ],
                'condition' => [
                    'separator_type' => 'icon',
                ],
            ]
        );

        $this->add_control(
            'separator_image',
            [
                'label' => esc_html__('Separator Image', 'sifency-addons'),
                'type' => Controls_Manager::MEDIA,
                'condition' => [
                    'separator_type' => 'image',
                ],
            ]
        );

        $this->add_control(
            'separator_spacing',
            [
                'label' => esc_html__('Separator Spacing', 'sifency-addons'),
                'type' => Controls_Manager::SLIDER,
                'size_units' => ['px', 'em'],
                'range' => [
                    'px' => [
                        'min' => 0,
                        'max' => 100,
                        'step' => 1,
                    ],
                    'em' => [
                        'min' => 0,
                        'max' => 10,
                        'step' => 0.1,
                    ],
                ],
                'default' => [
                    'unit' => 'px',
                    'size' => 20,
                ],
                'selectors' => [
                    '{{WRAPPER}} .marquee-separator' => 'margin-left: {{SIZE}}{{UNIT}}; margin-right: {{SIZE}}{{UNIT}};',
                ],
                'conditions' => [
                    'relation' => 'or',
                    'terms' => [
                        [
                            'name' => 'separator_type',
                            'operator' => '==',
                            'value' => 'icon'
                        ],
                        [
                            'name' => 'separator_type',
                            'operator' => '==',
                            'value' => 'text'
                        ],
                        [
                            'name' => 'separator_type',
                            'operator' => '==',
                            'value' => 'image'
                        ],
                    ],
                ],
            ]
        );

        $this->end_controls_section();

        // Container Style Section
        $this->start_controls_section(
            'container_style_section',
            [
                'label' => esc_html__('Container', 'sifency-addons'),
                'tab' => Controls_Manager::TAB_STYLE,
            ]
        );

        $this->add_responsive_control(
            'container_height',
            [
                'label' => esc_html__('Height', 'sifency-addons'),
                'type' => Controls_Manager::SLIDER,
                'size_units' => ['px', 'vh'],
                'range' => [
                    'px' => [
                        'min' => 0,
                        'max' => 500,
                        'step' => 1,
                    ],
                    'vh' => [
                        'min' => 0,
                        'max' => 100,
                    ],
                ],
                'selectors' => [
                    '{{WRAPPER}} .sifency-marquee-container' => 'height: {{SIZE}}{{UNIT}};',
                ],
            ]
        );

        $this->end_controls_section();

        // Marquee Style Section
        $this->start_controls_section(
            'marquee_style_section',
            [
                'label' => esc_html__('Marquee', 'sifency-addons'),
                'tab' => Controls_Manager::TAB_STYLE,
            ]
        );

        $this->add_responsive_control(
            'marquee_height',
            [
                'label' => esc_html__('Height', 'sifency-addons'),
                'type' => Controls_Manager::SLIDER,
                'size_units' => ['px', 'vh'],
                'range' => [
                    'px' => [
                        'min' => 0,
                        'max' => 500,
                        'step' => 1,
                    ],
                    'vh' => [
                        'min' => 0,
                        'max' => 100,
                    ],
                ],
                'selectors' => [
                    '{{WRAPPER}} .marquee' => 'height: {{SIZE}}{{UNIT}};',
                ],
            ]
        );

        $this->add_group_control(
            Group_Control_Background::get_type(),
            [
                'name' => 'marquee_background',
                'label' => esc_html__('Background', 'sifency-addons'),
                'types' => ['classic', 'gradient'],
                'selector' => '{{WRAPPER}} .marquee',
            ]
        );

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

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

        $this->add_group_control(
            Group_Control_Box_Shadow::get_type(),
            [
                'name' => 'marquee_box_shadow',
                'label' => esc_html__('Box Shadow', 'sifency-addons'),
                'selector' => '{{WRAPPER}} .marquee',
            ]
        );

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

        $this->end_controls_section();

        // Content Style Section
        $this->start_controls_section(
            'marquee_content_style_section',
            [
                'label' => esc_html__('Content', 'sifency-addons'),
                'tab' => Controls_Manager::TAB_STYLE,
            ]
        );

        $this->add_control(
            'speed',
            [
                'label' => esc_html__('Animation Speed (seconds)', 'sifency-addons'),
                'type' => Controls_Manager::SLIDER,
                'size_units' => ['s'],
                'range' => [
                    's' => [
                        'min' => 1,
                        'max' => 100,
                        'step' => 1,
                    ],
                ],
                'default' => [
                    'unit' => 's',
                    'size' => 20,
                ],
            ]
        );

        $this->end_controls_section();
        
        // Text Alignment Section
        $this->start_controls_section(
            'text_alignment_section',
            [
                'label' => esc_html__('Item Alignment', 'sifency-addons'),
                'tab' => Controls_Manager::TAB_STYLE,
            ]
        );

        // Horizontal Alignment
        $this->add_responsive_control(
            'marquee_content_align',
            [
                'label' => esc_html__('Horizontal Alignment', 'sifency-addons'),
                'type' => Controls_Manager::CHOOSE,
                'options' => [
                    'flex-start' => [
                        'title' => esc_html__('Left', 'sifency-addons'),
                        'icon' => 'eicon-text-align-left',
                    ],
                    'center' => [
                        'title' => esc_html__('Center', 'sifency-addons'),
                        'icon' => 'eicon-text-align-center',
                    ],
                    'flex-end' => [
                        'title' => esc_html__('Right', 'sifency-addons'),
                        'icon' => 'eicon-text-align-right',
                    ],
                ],
                'selectors' => [
                    '{{WRAPPER}} .cs-marquee-content, {{WRAPPER}} .cs-marquee-content > a' => 'justify-content: {{VALUE}};',
                    '{{WRAPPER}} .marquee-inner-text' => 'text-align: {{VALUE}};',
                ],
            ]
        );

        // Vertical Alignment
        $this->add_responsive_control(
            'vertical_align',
            [
                'label' => esc_html__('Vertical Alignment', 'sifency-addons'),
                'type' => Controls_Manager::CHOOSE,
                'options' => [
                    'flex-start' => [
                        'title' => esc_html__('Top', 'sifency-addons'),
                        'icon' => 'eicon-v-align-top',
                    ],
                    'center' => [
                        'title' => esc_html__('Middle', 'sifency-addons'),
                        'icon' => 'eicon-v-align-middle',
                    ],
                    'flex-end' => [
                        'title' => esc_html__('Bottom', 'sifency-addons'),
                        'icon' => 'eicon-v-align-bottom',
                    ],
                ],
                'selectors' => [
                    '{{WRAPPER}} .cs-marquee-content, {{WRAPPER}} .cs-marquee-content > a' => 'display: flex; align-items: {{VALUE}};',
                ],
            ]
        );

        // Text Direction (for RTL support)
        $this->add_control(
            'text_direction',
            [
                'label' => esc_html__('Text Direction', 'sifency-addons'),
                'type' => Controls_Manager::CHOOSE,
                'options' => [
                    'ltr' => [
                        'title' => esc_html__('Left to Right', 'sifency-addons'),
                        'icon' => 'eicon-h-align-left',
                    ],
                    'rtl' => [
                        'title' => esc_html__('Right to Left', 'sifency-addons'),
                        'icon' => 'eicon-h-align-right',
                    ],
                ],
                'selectors' => [
                    '{{WRAPPER}} .marquee-inner-text' => 'direction: {{VALUE}};',
                ],
            ]
        );

        $this->end_controls_section();

        // Separator Style Section
        $this->start_controls_section(
            'separator_style_section',
            [
                'label' => esc_html__('Separator', 'sifency-addons'),
                'tab' => Controls_Manager::TAB_STYLE,
                'conditions' => [
                    'relation' => 'or',
                    'terms' => [
                        [
                            'name' => 'separator_type',
                            'operator' => '==',
                            'value' => 'icon'
                        ],
                        [
                            'name' => 'separator_type',
                            'operator' => '==',
                            'value' => 'text'
                        ],
                        [
                            'name' => 'separator_type',
                            'operator' => '==',
                            'value' => 'image'
                        ],
                    ],
                ],
            ]
        );

        $this->add_control(
            'separator_color',
            [
                'label' => esc_html__('Color', 'sifency-addons'),
                'type' => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .marquee-separator' => 'color: {{VALUE}};',
                    '{{WRAPPER}} .marquee-separator svg' => 'fill: {{VALUE}};',
                ],
                'conditions' => [
                    'relation' => 'or',
                    'terms' => [
                        [
                            'name' => 'separator_type',
                            'operator' => '==',
                            'value' => 'icon'
                        ],
                        [
                            'name' => 'separator_type',
                            'operator' => '==',
                            'value' => 'text'
                        ],
                    ],
                ],
            ]
        );

        $this->add_control(
            'separator_size',
            [
                'label' => esc_html__('Size', 'sifency-addons'),
                'type' => Controls_Manager::SLIDER,
                'size_units' => ['px', 'em'],
                'range' => [
                    'px' => [
                        'min' => 1,
                        'max' => 100,
                        'step' => 1,
                    ],
                    'em' => [
                        'min' => 0.1,
                        'max' => 10,
                        'step' => 0.1,
                    ],
                ],
                'default' => [
                    'unit' => 'px',
                    'size' => 16,
                ],
                'selectors' => [
                    '{{WRAPPER}} .marquee-separator' => 'font-size: {{SIZE}}{{UNIT}};',
                    '{{WRAPPER}} .marquee-separator svg' => 'width: {{SIZE}}{{UNIT}}; height: {{SIZE}}{{UNIT}};',
                ],
                'conditions' => [
                    'relation' => 'or',
                    'terms' => [
                        [
                            'name' => 'separator_type',
                            'operator' => '==',
                            'value' => 'icon'
                        ],
                        [
                            'name' => 'separator_type',
                            'operator' => '==',
                            'value' => 'text'
                        ],
                    ],
                ],
            ]
        );

        $this->add_control(
            'image_separator_width',
            [
                'label' => esc_html__('Width', 'sifency-addons'),
                'type' => Controls_Manager::SLIDER,
                'size_units' => ['px', '%'],
                'range' => [
                    'px' => [
                        'min' => 1,
                        'max' => 1000,
                        'step' => 1,
                    ],
                    '%' => [
                        'min' => 1,
                        'max' => 100,
                        'step' => 1,
                    ],
                ],
                'default' => [
                    'unit' => 'px',
                    'size' => 30,
                ],
                'selectors' => [
                    '{{WRAPPER}} .marquee-separator img' => 'width: {{SIZE}}{{UNIT}};',
                ],
                'condition' => [
                    'separator_type' => 'image',
                ],
            ]
        );

        $this->add_control(
            'image_separator_height',
            [
                'label' => esc_html__('Height', 'sifency-addons'),
                'type' => Controls_Manager::SLIDER,
                'size_units' => ['px'],
                'range' => [
                    'px' => [
                        'min' => 1,
                        'max' => 500,
                        'step' => 1,
                    ],
                ],
                'selectors' => [
                    '{{WRAPPER}} .marquee-separator img' => 'height: {{SIZE}}{{UNIT}};',
                ],
                'condition' => [
                    'separator_type' => 'image',
                ],
            ]
        );

        $this->add_group_control(
            Group_Control_Border::get_type(),
            [
                'name' => 'separator_border',
                'label' => esc_html__('Border', 'sifency-addons'),
                'selector' => '{{WRAPPER}} .marquee-separator',
            ]
        );

        $this->add_control(
            'separator_border_radius',
            [
                'label' => esc_html__('Border Radius', 'sifency-addons'),
                'type' => Controls_Manager::DIMENSIONS,
                'size_units' => ['px', '%'],
                'selectors' => [
                    '{{WRAPPER}} .marquee-separator' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
                ],
            ]
        );

        $this->add_group_control(
            Group_Control_Box_Shadow::get_type(),
            [
                'name' => 'separator_box_shadow',
                'label' => esc_html__('Box Shadow', 'sifency-addons'),
                'selector' => '{{WRAPPER}} .marquee-separator',
            ]
        );

        $this->end_controls_section();
		
	}
}