<?php
if ( ! class_exists( '\Elementor\Plugin' ) ) {
    return;
}
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\Repeater;
use \Elementor\Icons_Manager;
use \Elementor\Group_Control_Image_Size;
use \Elementor\Group_Control_Background;
use \Elementor\Group_Control_Css_Filter;
use Sifency\Traits\Sifency_Marquee_Controls;
use Sifency\Traits\Sifency_Animation_Controls;
use Sifency\Traits\Sifency_Core_Text_Style;
use Sifency\Traits\Sifency_Icon_Style_Controls;
use Sifency\Traits\Sifency_Button_Icon_Style_Controls;
use Sifency\Traits\Sifency_Button_Style_Controls;
use Sifency\Traits\Sifency_Item_Style_Controls;
use Sifency\Traits\Sifency_Image_Style_Controls;
use Sifency\Traits\Sifency_Animation_Manager;

class Sifency_Widget_Marquee extends Widget_Base {
    use Sifency_Marquee_Controls;
    use Sifency_Animation_Controls;
	use Sifency_Core_Text_Style;
	use Sifency_Icon_Style_Controls;
	use Sifency_Button_Icon_Style_Controls;
	use Sifency_Button_Style_Controls;
	use Sifency_Item_Style_Controls;
	use Sifency_Image_Style_Controls;

    public function get_name() {
        return 'sifency-widget-text-slider';
    }

    public function get_title() {
        return esc_html__('Sifency Marquee', 'sifency-addons');
    }

    public function get_icon() {
        return 'eicon-post-slider';
    }

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

    public function get_style_depends() {
	    return [ 'sifency-typo-style' , 'sifency-frontend-style' , 'sifency-icon', 'sifency-marquee-style' ];
    }
    public function get_script_depends() {
        return [ 'sifency-frontend-script', 'sifency-marquee-script' ];
    }
    protected function _register_controls() {
		$this->add_sifency_marquee_controls();
		$this->add_sifency_item_style_controls();
        $this->add_sifency_advanced_text_style([
            'prefix'   => 'content_style',
            'label'    => 'Marquee Text Style',
            'selector' => '.sifency-content-style',
            'target_children' => false,
        ]);
		$this->add_sifency_icon_style_controls();
		$this->add_sifency_image_style_controls();
		$this->add_sifency_animation_controls();
    }

    protected function render() {
            $settings = $this->get_settings_for_display();
            $animation_manager = new Sifency_Animation_Manager($settings);

            $wrapper_classes = ['sifency-text-slider-wrapper'];
            if (!empty($settings['wrap_class'])) {
                $wrapper_classes[] = esc_attr($settings['wrap_class']);
            }
            
            if (!empty($settings['widget_id'])) {
                $this->add_render_attribute('wrapper', 'id', $settings['widget_id']);
            }

            $wrapper_attrs = $animation_manager->get_wrapper_attributes($wrapper_classes);
            $this->add_render_attribute('wrapper', 'class', $wrapper_classes);

            $direction = $settings['slider_direction'] === 'right' ? 'right' :
                        ($settings['slider_direction'] === 'top' ? 'top' :
                        ($settings['slider_direction'] === 'bottom' ? 'bottom' : 'left'));

            $pause_class = $settings['pause_on_hover'] === 'yes' ? 'marquee-pause-on-hover' : '';
            $loop_count = $settings['loop_count'] > 0 ? 'data-loop-count="' . esc_attr($settings['loop_count']) . '"' : '';
            $speed = !empty($settings['speed']['size']) ? $settings['speed']['size'] : 20;
            $flex_direction = in_array($direction, ['top', 'bottom']) ? 'column' : 'row';

            ?>
            <div <?php echo wp_kses_post($this->get_render_attribute_string('wrapper')); ?> <?php echo wp_kses_post( $wrapper_attrs ); ?>>
                <div class="sifency-marquee-container">
                    <div class="marquee <?php echo esc_attr($pause_class); ?>" data-direction="<?php echo esc_attr($direction); ?>" data-speed="<?php echo esc_attr($speed); ?>" <?php 
                    echo esc_html($loop_count); ?>>
                        <div class="marquee-content-wrapper">
                            <ul class="marquee-content" style="flex-direction: <?php echo esc_attr($flex_direction); ?>;">
                                <?php
                                if (!empty($settings['text_list'])) {
                                    $count = count($settings['text_list']);
                                    foreach ($settings['text_list'] as $index => $item) {
                                        $li_classes = ['cs-marquee-content', 'sifency-item-style'];
                                        if (!empty($item['item_class'])) {
                                            $li_classes[] = $item['item_class'];
                                        }
                                        $item_attrs = $animation_manager->get_item_attributes($li_classes);
                                        $link_url = $item['item_link']['url'] ?? '';
                                        $link_attrs = '';
                                        if (!empty($link_url)) {
                                            $link_key = 'link-' . $item['_id'];
                                            
                                            $this->add_link_attributes($link_key, $item['item_link']);
                                            $link_attrs = $this->get_render_attribute_string($link_key);
                                        }
                                        ?>
                                        <li <?php echo wp_kses_post($item_attrs); ?>>
                                            <?php
                                            if (!empty($link_url)) {
                                                echo'<a ' . wp_kses_post( $link_attrs ).'>';
                                            }
                                            ?>
                                            <span class="marquee-inner-text">
                                                <?php
                                                if (!empty($item['icon']['value'])) {
                                                    echo '<div class="sifency-icon-style">';
                                                    \Elementor\Icons_Manager::render_icon($item['icon'], ['aria-hidden' => 'true']);
                                                    echo '</div>';
                                                }
                                                if (!empty($item['item_image']['url'])) {
                                                    $image_html = Group_Control_Image_Size::get_attachment_image_html($item, 'thumbnail', 'item_image');
                                                    if ($image_html) {
                                                        echo wp_kses_post('<div class="sifency-image-container sifency-item-image">' . $image_html . '<div class="sifency-image-overlay"></div></div>');
                                                    }
                                                }
                                                echo '<div class="sifency-content-style">';
                                                echo esc_html($item['text']);
                                                echo '</div>';
                                                ?>
                                            </span>
                                            <?php
                                            if (!empty($link_url)) {
                                                echo '</a>';
                                            }

                                            if ($index < $count - 1 && $settings['separator_type'] !== 'none') {
                                                $has_separator = false;
                                                $separator_content = '';

                                                switch ($settings['separator_type']) {
                                                    case 'icon':
                                                        if (!empty($settings['separator_icon']['value'])) {
                                                            ob_start();
                                                            \Elementor\Icons_Manager::render_icon($settings['separator_icon'], ['aria-hidden' => 'true']);
                                                            $separator_content = ob_get_clean();
                                                            $has_separator = true;
                                                        }
                                                        break;
                                                    case 'text':
                                                        if (!empty($settings['separator_text'])) {
                                                            $separator_content = esc_html($settings['separator_text']);
                                                            $has_separator = true;
                                                        }
                                                        break;
                                                    case 'image':
                                                        if (!empty($settings['separator_image']['url'])) {
                                                            $separator_content = '<img src="' . esc_url($settings['separator_image']['url']) . '" alt="' . esc_attr__('Separator', 'sifency-addons') . '">';
                                                            $has_separator = true;
                                                        }
                                                        break;
                                                }

                                                if ($has_separator) {
                                                    echo '<span class="marquee-separator">' . wp_kses_post( $separator_content ) . '</span>';
                                                }
                                            }
                                            ?>
                                        </li>
                                        <?php
                                    }
                                }
                                ?>
                            </ul>
                        </div>
                    </div>
                </div>
            </div>
            <?php
        }
    }