<?php
/**
 * Elementor Widget: GB Sites Partenaires
 */

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

class Guide_Benin_Partners_Widget extends \Elementor\Widget_Base {

    public function get_name() {
        return 'gb_partners';
    }

    public function get_title() {
        return __( 'GB Sites Partenaires', 'guide-benin' );
    }

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

    public function get_categories() {
        return array( 'guide-benin' );
    }

    protected function register_controls() {
        $this->start_controls_section(
            'section_content',
            array(
                'label' => __( 'Contenu Partenaires', 'guide-benin' ),
            )
        );

        $this->add_control(
            'tagline',
            array(
                'label'   => __( 'Sous-titre', 'guide-benin' ),
                'type'    => \Elementor\Controls_Manager::TEXT,
                'default' => 'Collaborations',
            )
        );

        $this->add_control(
            'title',
            array(
                'label'   => __( 'Titre', 'guide-benin' ),
                'type'    => \Elementor\Controls_Manager::TEXT,
                'default' => 'Les Sites Partenaires',
            )
        );

        $this->add_control(
            'subtitle',
            array(
                'label'   => __( 'Texte secondaire', 'guide-benin' ),
                'type'    => \Elementor\Controls_Manager::TEXT,
                'default' => 'Découvrez nos partenaires et initiatives',
            )
        );

        // Repeater for Partners list
        $repeater = new \Elementor\Repeater();

        $repeater->add_control(
            'partner_name',
            array(
                'label'   => __( 'Nom du site', 'guide-benin' ),
                'type'    => \Elementor\Controls_Manager::TEXT,
                'default' => 'Site Partenaire',
            )
        );

        $repeater->add_control(
            'partner_logo',
            array(
                'label'   => __( 'Logo du partenaire', 'guide-benin' ),
                'type'    => \Elementor\Controls_Manager::MEDIA,
            )
        );

        $repeater->add_control(
            'partner_url',
            array(
                'label'   => __( 'URL du site', 'guide-benin' ),
                'type'    => \Elementor\Controls_Manager::URL,
                'placeholder' => 'https://example.com',
            )
        );

        $this->add_control(
            'partners_list',
            array(
                'label'       => __( 'Liste des Partenaires', 'guide-benin' ),
                'type'        => \Elementor\Controls_Manager::REPEATER,
                'fields'      => $repeater->get_controls(),
                'title_field' => '{{{ partner_name }}}',
                'default'     => array(),
            )
        );

        $this->end_controls_section();
    }

    protected function render() {
        $settings = $this->get_settings_for_display();
        $partners = ! empty( $settings['partners_list'] ) ? $settings['partners_list'] : array();

        // Fallback placeholder items if empty
        if ( empty( $partners ) ) {
            $placeholder_img = 'https://dahomeydiscovery.com/wp-content/uploads/2026/05/Attractions-banner.jpg';
            for ( $i = 1; $i <= 6; $i++ ) {
                $partners[] = array(
                    'partner_name' => 'Partenaire ' . $i,
                    'partner_logo' => array( 'url' => $placeholder_img ),
                    'partner_url'  => array( 'url' => '#' ),
                );
            }
        }

        $widget_id = $this->get_id();
        $needs_scroll = count($partners) > 5;
        ?>
        <style>
        #gb-partners-<?php echo esc_attr( $widget_id ); ?> .gb-partner-item {
            width: 170px;
            height: 170px;
            border-radius: 18px;
            background: #fff;
            border: 1px solid #EFE2CE;
            display: flex;
            align-items: center;
            justify-content: center;
            overflow: hidden;
            margin-right: 20px;
            transition: all 0.28s cubic-bezier(0.4, 0, 0.2, 1);
            flex-shrink: 0;
            box-shadow: 0 4px 10px rgba(36, 16, 9, 0.02);
            padding: 18px;
        }
        #gb-partners-<?php echo esc_attr( $widget_id ); ?> .gb-partner-item:hover {
            transform: translateY(-6px);
            box-shadow: 0 16px 36px rgba(36, 16, 9, 0.08);
            border-color: #FB9A02;
        }
        #gb-partners-<?php echo esc_attr( $widget_id ); ?> .gb-partner-img {
            max-width: 85%;
            max-height: 85%;
            object-fit: contain;
            filter: grayscale(100%);
            opacity: 0.65;
            transition: all 0.28s;
        }
        #gb-partners-<?php echo esc_attr( $widget_id ); ?> .gb-partner-item:hover .gb-partner-img {
            filter: grayscale(0%);
            opacity: 1;
        }

        /* Continuous scroll animations if needed */
        @keyframes gbScrollTrack-<?php echo esc_attr( $widget_id ); ?> {
            0% {
                transform: translateX(0);
            }
            100% {
                /* We offset by half of the duplicated list width */
                transform: translateX(-50%);
            }
        }

        #gb-partners-<?php echo esc_attr( $widget_id ); ?> .gb-partners-track {
            display: flex;
            width: max-content;
            <?php if ( $needs_scroll ) : ?>
            animation: gbScrollTrack-<?php echo esc_attr( $widget_id ); ?> 20s linear infinite;
            <?php endif; ?>
        }
        #gb-partners-<?php echo esc_attr( $widget_id ); ?> .gb-partners-track:hover {
            animation-play-state: paused;
        }
        </style>

        <section id="gb-partners-<?php echo esc_attr( $widget_id ); ?>" style="max-width:1180px; margin:0 auto; padding:60px 56px 90px; text-align:center; overflow:hidden;">
          <span style="font-weight:600; font-size:13px; letter-spacing:0.22em; color:#FB9A02; text-transform:uppercase;"><?php echo esc_html( $settings['tagline'] ); ?></span>
          <h3 style="font-family:'Baloo 2',sans-serif; font-weight:800; font-size:34px; letter-spacing:-0.015em; color:#241009; margin:10px 0 6px;"><?php echo esc_html( $settings['title'] ); ?></h3>
          <div style="font-family:'Baloo 2',sans-serif; font-weight:700; font-size:18px; background:linear-gradient(95deg,#FB9A02,#E8431E); -webkit-background-clip:text; background-clip:text; -webkit-text-fill-color:transparent; color:transparent; margin-bottom:42px;"><?php echo esc_html( $settings['subtitle'] ); ?></div>
          
          <div style="width:100%; overflow:hidden; position:relative; display:flex; <?php if ( ! $needs_scroll ) { echo 'justify-content:center;'; } ?>">
            <div class="gb-partners-track">
              <!-- Render list -->
              <?php foreach ( $partners as $p ) : 
                  $logo_url = ! empty( $p['partner_logo']['url'] ) ? $p['partner_logo']['url'] : '';
                  $url = ! empty( $p['partner_url']['url'] ) ? $p['partner_url']['url'] : '#';
                  ?>
                  <a href="<?php echo esc_url($url); ?>" target="_blank" class="gb-partner-item" title="<?php echo esc_attr($p['partner_name']); ?>">
                    <?php if ( ! empty($logo_url) ) : ?>
                        <img class="gb-partner-img" src="<?php echo esc_url($logo_url); ?>" alt="<?php echo esc_attr($p['partner_name']); ?>" />
                    <?php else : ?>
                        <span style="font-weight:700; font-size:15px; color:#5D351C; text-align:center;"><?php echo esc_html($p['partner_name']); ?></span>
                    <?php endif; ?>
                  </a>
              <?php endforeach; ?>

              <!-- Duplicate list for infinite loop if scrolling is needed -->
              <?php if ( $needs_scroll ) : ?>
                  <?php foreach ( $partners as $p ) : 
                      $logo_url = ! empty( $p['partner_logo']['url'] ) ? $p['partner_logo']['url'] : '';
                      $url = ! empty( $p['partner_url']['url'] ) ? $p['partner_url']['url'] : '#';
                      ?>
                      <a href="<?php echo esc_url($url); ?>" target="_blank" class="gb-partner-item" title="<?php echo esc_attr($p['partner_name']); ?>">
                        <?php if ( ! empty($logo_url) ) : ?>
                            <img class="gb-partner-img" src="<?php echo esc_url($logo_url); ?>" alt="<?php echo esc_attr($p['partner_name']); ?>" />
                        <?php else : ?>
                            <span style="font-weight:700; font-size:15px; color:#5D351C; text-align:center;"><?php echo esc_html($p['partner_name']); ?></span>
                        <?php endif; ?>
                      </a>
                  <?php endforeach; ?>
              <?php endif; ?>
            </div>
          </div>
        </section>
        <?php
    }
}
