<?php
use Elementor\Controls_Manager;
use Elementor\Group_Control_Typography;
use Elementor\Repeater;
use Elementor\Widget_Base;
use Elementor\Utils;

if ( ! defined( 'ABSPATH' ) ) {
    exit; // Exit if accessed directly.
}

class CRT_Scroll_Marquee_Hover extends Widget_Base {
    
    public function get_name() {
        return 'crt-scroll-marquee-hover';
    }

    public function get_title() {
        return esc_html__( 'Hover Image Marquee', 'crt-manage' );
    }

    public function get_icon() {
        return 'crt-icon eicon-scroll';
    }

    public function get_categories() {
        return [ 'crt_manage_theme' ];
    }

    public function get_keywords() {
        return [ 'scroll', 'marquee', 'gsap', 'slider', 'hover', 'image' ];
    }

    public function get_script_depends() {
        return [ 'crt-manage-lib-gsap' ]; // Requires GSAP framework
    }

    protected function register_controls() {

        $this->start_controls_section(
            'section_content',
            [
                'label' => esc_html__( 'Content', 'crt-manage' ),
            ]
        );

        $repeater = new Repeater();

        $repeater->add_control(
            'item_text',
            [
                'label' => esc_html__( 'Main Text', 'crt-manage' ),
                'type' => Controls_Manager::TEXT,
                'default' => esc_html__( 'Interactive Project', 'crt-manage' ),
                'label_block' => true,
            ]
        );

        $repeater->add_control(
            'item_subtext',
            [
                'label' => esc_html__( 'Subtext (Top)', 'crt-manage' ),
                'type' => Controls_Manager::TEXT,
                'default' => esc_html__( 'UI/UX', 'crt-manage' ),
            ]
        );

        $repeater->add_control(
            'item_image',
            [
                'label' => esc_html__( 'Hover Hidden Image', 'crt-manage' ),
                'type' => Controls_Manager::MEDIA,
                'default' => [
                    'url' => Utils::get_placeholder_image_src(),
                ],
            ]
        );
        
        $repeater->add_control(
            'item_link',
            [
                'label' => esc_html__( 'Link', 'crt-manage' ),
                'type' => Controls_Manager::URL,
                'dynamic' => [
                    'active' => true,
                ],
                'placeholder' => esc_html__( 'https://your-link.com', 'crt-manage' ),
            ]
        );

        $this->add_control(
            'items',
            [
                'label' => esc_html__( 'Marquee Items', 'crt-manage' ),
                'type' => Controls_Manager::REPEATER,
                'fields' => $repeater->get_controls(),
                'default' => [
                    [
                        'item_text' => esc_html__( 'Creative Concept', 'crt-manage' ),
                        'item_subtext' => esc_html__( '2026', 'crt-manage' ),
                    ],
                    [
                        'item_text' => esc_html__( 'Digital Art', 'crt-manage' ),
                        'item_subtext' => esc_html__( '2026', 'crt-manage' ),
                    ],
                    [
                        'item_text' => esc_html__( 'Web Design', 'crt-manage' ),
                        'item_subtext' => esc_html__( '2026', 'crt-manage' ),
                    ],
                ],
                'title_field' => '{{{ item_text }}}',
            ]
        );

        $this->add_control(
            'speed',
            [
                'label' => esc_html__( 'Scroll Scroll Sync Ratio', 'crt-manage' ),
                'type' => Controls_Manager::SLIDER,
                'range' => [
                    'px' => [
                        'min' => 1,
                        'max' => 10,
                        'step' => 0.1,
                    ],
                ],
                'default' => [
                    'size' => 2,
                ],
            ]
        );

        $this->add_control(
            'items_duplicate',
            [
                'label' => esc_html__( 'Duplication (For deep native looping)', 'crt-manage' ),
                'type' => Controls_Manager::NUMBER,
                'min' => 1,
                'max' => 10,
                'default' => 4,
            ]
        );

        $this->end_controls_section();

        // Style Section
        $this->start_controls_section(
            'section_style_content',
            [
                'label' => esc_html__( 'Typography & Style', 'crt-manage' ),
                'tab' => Controls_Manager::TAB_STYLE,
            ]
        );

        $this->add_control(
            'text_color_idle',
            [
                'label' => esc_html__( 'Text Color (Idle)', 'crt-manage' ),
                'type' => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .crt-smh-text' => 'color: {{VALUE}};',
                ],
            ]
        );

        $this->add_control(
            'text_color_hover',
            [
                'label' => esc_html__( 'Text Color (Hover)', 'crt-manage' ),
                'type' => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .crt-smh-item:hover .crt-smh-text' => 'color: {{VALUE}};',
                ],
            ]
        );
        
        $this->add_control(
            'subtext_color',
            [
                'label' => esc_html__( 'Subtext Color', 'crt-manage' ),
                'type' => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .crt-smh-subtext' => 'color: {{VALUE}};',
                ],
            ]
        );

        $this->add_control(
            'subtext_color_hover',
            [
                'label' => esc_html__( 'Subtext Color (Hover)', 'crt-manage' ),
                'type' => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .crt-smh-item:hover .crt-smh-subtext' => 'color: {{VALUE}};',
                ],
            ]
        );

        $this->add_group_control(
            Group_Control_Typography::get_type(),
            [
                'name' => 'typography_main',
                'label' => esc_html__( 'Main Text Typography', 'crt-manage' ),
                'selector' => '{{WRAPPER}} .crt-smh-text',
            ]
        );

        $this->add_group_control(
            Group_Control_Typography::get_type(),
            [
                'name' => 'typography_sub',
                'label' => esc_html__( 'Subtext Typography', 'crt-manage' ),
                'selector' => '{{WRAPPER}} .crt-smh-subtext',
            ]
        );

        $this->add_control(
            'image_height',
            [
                'label' => esc_html__( 'Hover Image Height', 'crt-manage' ),
                'type' => Controls_Manager::SLIDER,
                'size_units' => [ 'px', 'vh' ],
                'default' => [
                    'size' => 300,
                    'unit' => 'px',
                ],
                'range' => [
                    'px' => [ 'min' => 50, 'max' => 600 ],
                ],
                'selectors' => [
                    '{{WRAPPER}} .crt-smh-container' => '--hover-img-height: {{SIZE}}{{UNIT}};',
                ],
            ]
        );

        $this->add_responsive_control(
            'image_border_radius',
            [
                'label' => esc_html__( 'Hover Image Border Radius', 'crt-manage' ),
                'type' => Controls_Manager::DIMENSIONS,
                'size_units' => [ 'px', '%', 'em', 'rem' ],
                'selectors' => [
                    '{{WRAPPER}} .crt-smh-container' => '--hover-img-border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
                ],
            ]
        );

        $this->add_control(
            'marquee_padding',
            [
                'label' => esc_html__( 'Marquee Row Padding', 'crt-manage' ),
                'type' => Controls_Manager::DIMENSIONS,
                'size_units' => [ 'px', 'em', '%' ],
                'selectors' => [
                    '{{WRAPPER}} .crt-smh-container' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
                ],
            ]
        );
        
        $this->add_responsive_control(
            'item_gap',
            [
                'label' => esc_html__( 'Gap Right', 'crt-manage' ),
                'type' => Controls_Manager::SLIDER,
                'size_units' => [ 'px', 'vw' ],
                'range' => [
                    'px' => [ 'min' => 0, 'max' => 300 ],
                ],
                'default' => [
                    'size' => 80,
                    'unit' => 'px',
                ],
                'selectors' => [
                    '{{WRAPPER}} .crt-smh-item' => 'margin-right: {{SIZE}}{{UNIT}};',
                ],
            ]
        );

        $this->add_control(
            'subtext_position',
            [
                'label' => esc_html__( 'Subtext Position', 'crt-manage' ),
                'type' => Controls_Manager::SELECT,
                'default' => '-1',
                'options' => [
                    '-1'  => esc_html__( 'Start (Before Text)', 'crt-manage' ),
                    '1' => esc_html__( 'End (After Text)', 'crt-manage' ),
                ],
                'selectors' => [
                    '{{WRAPPER}} .crt-smh-subtext' => 'order: {{VALUE}};',
                ],
            ]
        );

        $this->end_controls_section();
    }

    protected function render() {
        $settings = $this->get_settings_for_display();
        
        if ( empty( $settings['items'] ) ) {
            return;
        }

        $speed = $settings['speed']['size'] ? $settings['speed']['size'] : 2;
        $copies = $settings['items_duplicate'] ? $settings['items_duplicate'] : 4;
        
        // Output inline architecture styles natively mapped
        echo '<style>
            .crt-smh-container { position: relative; overflow: hidden; width: 100vw; margin-left: calc(50% - 50vw); }
            .crt-smh-track { display: flex; flex-direction: row; align-items: center; white-space: nowrap; width: max-content; will-change: transform; }
            .crt-smh-item { position: relative; display: inline-flex; align-items: flex-start; gap: 30px; cursor: pointer; padding: 20px 0; text-decoration: none !important; }
            .crt-smh-subtext { font-size: 16px; margin-top: 5px; font-weight: 500; transition: color 0.3s ease; }
            .crt-smh-text { font-size: 100px; font-weight: 800; text-transform: uppercase; line-height: 1; transition: color 0.3s ease, opacity 0.3s ease; }
            
            .crt-smh-hover-img { position: fixed; top: 0; left: 0; pointer-events: none; z-index: 99999; object-fit: cover; border-radius: 12px; box-shadow: 0 30px 60px rgba(0,0,0,0.2); }
        </style>';
        ?>

        <div class="crt-smh-container" data-speed="<?php echo esc_attr( $speed ); ?>">
            <div class="crt-smh-track">
                <?php 
                for($i = 0; $i < $copies; $i++) {
                    foreach ( $settings['items'] as $index => $item ) : 
                        $link_url = ! empty( $item['item_link']['url'] ) ? $item['item_link']['url'] : '#';
                        $img_url  = ! empty( $item['item_image']['url'] ) ? $item['item_image']['url'] : '';
                        ?>
                        
                        <a href="<?php echo esc_url( $link_url ); ?>" class="crt-smh-item elementor-repeater-item-<?php echo esc_attr( $item['_id'] ); ?>" data-hover-img="<?php echo esc_url( $img_url ); ?>">
                            <?php if ( ! empty( $item['item_subtext'] ) ) : ?>
                                <span class="crt-smh-subtext"><?php echo wp_kses_post( $item['item_subtext'] ); ?></span>
                            <?php endif; ?>
                            
                            <?php if ( ! empty( $item['item_text'] ) ) : ?>
                                <span class="crt-smh-text"><?php echo wp_kses_post( $item['item_text'] ); ?></span>
                            <?php endif; ?>
                        </a>

                    <?php endforeach; 
                } ?>
            </div>
        </div>
        
        <?php
    }
}
