<?php
namespace Elementor;

/**
 * Elementor Widget: Match List
 * @package Feron
 * @since 1.0.0
 */

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

class Match_List extends Widget_Base {

    public function get_name() {
        return 'feron-match-list-widget';
    }

    public function get_title() {
        return esc_html__( 'Match List', 'feron-core' );
    }

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

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

    protected function register_controls() {

        /**
         * Section Heading & Shapes
         */
        $this->start_controls_section(
            'section_heading',
            [
                'label' => __( 'Section Heading & Shapes', 'feron-core' ),
                'tab'   => Controls_Manager::TAB_CONTENT,
            ]
        );

        $this->add_control(
            'section_subtitle',
            [
                'label'   => __( 'Section Subtitle', 'feron-core' ),
                'type'    => Controls_Manager::TEXT,
                'default' => __( 'Top trending matches', 'feron-core' ),
            ]
        );

        $this->add_control(
            'section_title',
            [
                'label'   => __( 'Section Title', 'feron-core' ),
                'type'    => Controls_Manager::TEXTAREA,
                'default' => __( 'Upcoming Trending Matches', 'feron-core' ),
            ]
        );

        $this->add_control(
            'left_shape',
            [
                'label'   => __( 'Left Shape Image', 'feron-core' ),
                'type'    => Controls_Manager::MEDIA,
                'default' => [ 'url' => Utils::get_placeholder_image_src() ],
            ]
        );

        $this->add_control(
            'right_shape',
            [
                'label'   => __( 'Right Shape Image', 'feron-core' ),
                'type'    => Controls_Manager::MEDIA,
                'default' => [ 'url' => Utils::get_placeholder_image_src() ],
            ]
        );

        $this->add_control(
            'image',
            [
                'label'   => __( 'Arrow Image', 'feron-core' ),
                'type'    => Controls_Manager::MEDIA,
                'default' => [ 'url' => Utils::get_placeholder_image_src() ],
            ]
        );

        $this->add_control(
            'image2',
            [
                'label'   => __( 'Linear Image 1', 'feron-core' ),
                'type'    => Controls_Manager::MEDIA,
                'default' => [ 'url' => Utils::get_placeholder_image_src() ],
            ]
        );

        $this->add_control(
            'image3',
            [
                'label'   => __( 'Linear Image 2', 'feron-core' ),
                'type'    => Controls_Manager::MEDIA,
                'default' => [ 'url' => Utils::get_placeholder_image_src() ],
            ]
        );

        $this->end_controls_section();

        /**
         * Match Items
         */
        $this->start_controls_section(
            'content_section',
            [
                'label' => __( 'Match List Items', 'feron-core' ),
                'tab'   => Controls_Manager::TAB_CONTENT,
            ]
        );

        // Nested repeater for watch platforms
        $platform_repeater = new Repeater();

        $platform_repeater->add_control(
            'platform_icon',
            [
                'label'   => __( 'Platform Icon', 'feron-core' ),
                'type'    => Controls_Manager::ICONS,
                'default' => [
                    'value'   => 'fab fa-youtube',
                    'library' => 'fa-brands',
                ],
            ]
        );

        $platform_repeater->add_control(
            'platform_name',
            [
                'label'   => __( 'Platform Name', 'feron-core' ),
                'type'    => Controls_Manager::TEXT,
                'default' => __( 'YouTube', 'feron-core' ),
            ]
        );

        $platform_repeater->add_control(
            'platform_link',
            [
                'label'         => __( 'Platform Link', 'feron-core' ),
                'type'          => Controls_Manager::URL,
                'show_external' => true,
                'default'       => [
                    'url'         => '#',
                    'is_external' => true,
                    'nofollow'    => true,
                ],
            ]
        );

        $repeater = new Repeater();

        $repeater->add_control(
            'layout_style',
            [
                'label'   => __( 'Layout Style', 'feron-core' ),
                'type'    => Controls_Manager::SELECT,
                'default' => 'style1',
                'options' => [
                    'style1' => __( 'Image Left / Content Right', 'feron-core' ),
                    'style2' => __( 'Content Left / Image Right', 'feron-core' ),
                ],
            ]
        );

        $repeater->add_control(
            'match_image_1',
            [
                'label'   => __( 'Match Image 1', 'feron-core' ),
                'type'    => Controls_Manager::MEDIA,
                'default' => [ 'url' => Utils::get_placeholder_image_src() ],
            ]
        );

        $repeater->add_control(
            'match_image_2',
            [
                'label'   => __( 'Match Image 2', 'feron-core' ),
                'type'    => Controls_Manager::MEDIA,
                'default' => [ 'url' => Utils::get_placeholder_image_src() ],
            ]
        );

        $repeater->add_control(
            'vs_image',
            [
                'label'   => __( 'VS Image', 'feron-core' ),
                'type'    => Controls_Manager::MEDIA,
                'default' => [ 'url' => Utils::get_placeholder_image_src() ],
            ]
        );

        $repeater->add_control(
            'match_date',
            [
                'label'   => __( 'Match Date', 'feron-core' ),
                'type'    => Controls_Manager::TEXT,
                'default' => __( '30 May, 2025', 'feron-core' ),
            ]
        );

        $repeater->add_control(
            'match_time',
            [
                'label'   => __( 'Match Time', 'feron-core' ),
                'type'    => Controls_Manager::TEXT,
                'default' => __( '10:00 am - 12:30 pm', 'feron-core' ),
            ]
        );

        $repeater->add_control(
            'match_title',
            [
                'label'   => __( 'Match Title', 'feron-core' ),
                'type'    => Controls_Manager::TEXT,
                'default' => __( 'Aggressive & War-Themed', 'feron-core' ),
            ]
        );

        $repeater->add_control(
            'match_desc',
            [
                'label'   => __( 'Match Description', 'feron-core' ),
                'type'    => Controls_Manager::TEXTAREA,
                'default' => __( 'Exciting, high-quality immersive gameplay.', 'feron-core' ),
            ]
        );

        $repeater->add_control(
            'match_link',
            [
                'label'         => __( 'Match Link', 'feron-core' ),
                'type'          => Controls_Manager::URL,
                'show_external' => true,
                'default'       => [
                    'url'         => '#',
                    'is_external' => true,
                    'nofollow'    => true,
                ],
            ]
        );

        $repeater->add_control(
            'watch_platforms',
            [
                'label'       => __( 'Watch Platforms', 'feron-core' ),
                'type'        => Controls_Manager::REPEATER,
                'fields'      => $platform_repeater->get_controls(),
                'title_field' => '{{{ platform_name }}}',
            ]
        );

        $this->add_control(
            'matches',
            [
                'type'        => Controls_Manager::REPEATER,
                'fields'      => $repeater->get_controls(),
                'title_field' => '{{{ match_title }}}',
            ]
        );

        $this->end_controls_section();
    }

    protected function render() {
        $settings = $this->get_settings_for_display();
        ?>

        <section class="trending-match-section fix section-padding pt-0">

            <?php if ( ! empty( $settings['left_shape']['url'] ) ) : ?>
                <div class="left-shape float-bob-y1">
                    <img src="<?php echo esc_url( $settings['left_shape']['url'] ); ?>" alt="shape">
                </div>
            <?php endif; ?>

            <?php if ( ! empty( $settings['right_shape']['url'] ) ) : ?>
                <div class="right-shape">
                    <img src="<?php echo esc_url( $settings['right_shape']['url'] ); ?>" alt="shape">
                </div>
            <?php endif; ?>

            <div class="container">
                <div class="section-title-2 text-center">
                    <?php if ( ! empty( $settings['section_subtitle'] ) ) : ?>
                        <h6 class="wow fadeInUp" data-wow-delay=".3s">
                            <?php echo esc_html( $settings['section_subtitle'] ); ?>
                        </h6>
                    <?php endif; ?>
                    <?php if ( ! empty( $settings['section_title'] ) ) : ?>
                        <h2 class="wow fadeInUp" data-wow-delay=".5s">
                            <?php echo esc_html( $settings['section_title'] ); ?>
                        </h2>
                    <?php endif; ?>
                </div>

                <div class="trending-match-wrapper">

				 	<div class="vec-arrow">
					<?php  if ( !empty(esc_url($settings['image']['id']) )) : ?>   
						<img src="<?php echo wp_get_attachment_url($settings['image']['id']);?>" alt=""/>
					<?php endif;?>
                    </div>
                    <div class="linear-shape">
                    <?php  if ( !empty(esc_url($settings['image2']['id']) )) : ?>   
						<img src="<?php echo wp_get_attachment_url($settings['image2']['id']);?>" alt=""/>
					<?php endif;?>
                    </div>
                    <div class="linear-shape-2">
                    <?php  if ( !empty(esc_url($settings['image3']['id']) )) : ?>   
						<img src="<?php echo wp_get_attachment_url($settings['image3']['id']);?>" alt=""/>
					<?php endif;?>
                    </div>

                    <?php if ( ! empty( $settings['matches'] ) ) : ?>
                        <?php foreach ( $settings['matches'] as $match ) : ?>
                            <div class="trending-match-items wow fadeInUp" data-wow-delay=".3s">

                                <?php if ( $match['layout_style'] === 'style1' ) : ?>
                                    <!-- Style 1 -->
                                    <div class="trending-match-left">
                                        <div class="gt-match-logo">
                                            <?php if ( ! empty( $match['match_image_1']['url'] ) ) : ?>
                                                <img src="<?php echo esc_url( $match['match_image_1']['url'] ); ?>" class="gt-match-thumb" alt="img">
                                            <?php endif; ?>
                                            <?php if ( ! empty( $match['vs_image']['url'] ) ) : ?>
                                                <img src="<?php echo esc_url( $match['vs_image']['url'] ); ?>" alt="vs">
                                            <?php endif; ?>
                                            <?php if ( ! empty( $match['match_image_2']['url'] ) ) : ?>
                                                <img src="<?php echo esc_url( $match['match_image_2']['url'] ); ?>" class="gt-match-thumb" alt="img">
                                            <?php endif; ?>
                                        </div>

                                        <?php if ( ! empty( $match['watch_platforms'] ) ) : ?>
                                            <div class="gt-watch-now-items">
                                                <span><?php esc_html_e( 'Watch live on', 'feron-core' ); ?></span>
                                                <ul class="gt-watch-now-list">
                                                    <?php foreach ( $match['watch_platforms'] as $platform ) : ?>
                                                        <li>
                                                            <a href="<?php echo esc_url( $platform['platform_link']['url'] ); ?>"
                                                               <?php echo $platform['platform_link']['is_external'] ? ' target="_blank"' : ''; ?>
                                                               <?php echo $platform['platform_link']['nofollow'] ? ' rel="nofollow"' : ''; ?>>
                                                                <?php if ( ! empty( $platform['platform_icon']['value'] ) ) : ?>
                                                                    <i class="<?php echo esc_attr( $platform['platform_icon']['value'] ); ?>"></i>
                                                                <?php endif; ?>
                                                                <?php echo esc_html( $platform['platform_name'] ); ?>
                                                            </a>
                                                        </li>
                                                    <?php endforeach; ?>
                                                </ul>
                                            </div>
                                        <?php endif; ?>
                                    </div>

                                    <div class="trending-match-content">
                                        <ul class="gt-date-list">
                                            <li><i class="fa-light fa-calendar"></i> <?php echo esc_html( $match['match_date'] ); ?></li>
                                            <li><i class="fa-regular fa-clock"></i> <?php echo esc_html( $match['match_time'] ); ?></li>
                                        </ul>
                                        <h3><a href="<?php echo esc_url( $match['match_link']['url'] ); ?>"><?php echo esc_html( $match['match_title'] ); ?></a></h3>
                                        <p><?php echo esc_html( $match['match_desc'] ); ?></p>
                                    </div>

                                <?php else : ?>
                                    <!-- Style 2 -->
                                    <div class="trending-match-content">
                                        <ul class="gt-date-list">
                                            <li><i class="fa-light fa-calendar"></i> <?php echo esc_html( $match['match_date'] ); ?></li>
                                            <li><i class="fa-regular fa-clock"></i> <?php echo esc_html( $match['match_time'] ); ?></li>
                                        </ul>
                                        <h3><a href="<?php echo esc_url( $match['match_link']['url'] ); ?>"><?php echo esc_html( $match['match_title'] ); ?></a></h3>
                                        <p><?php echo esc_html( $match['match_desc'] ); ?></p>
                                    </div>

                                    <div class="trending-match-left">
                                        <div class="gt-match-logo">
                                            <?php if ( ! empty( $match['match_image_1']['url'] ) ) : ?>
                                                <img src="<?php echo esc_url( $match['match_image_1']['url'] ); ?>" class="gt-match-thumb" alt="img">
                                            <?php endif; ?>
                                            <?php if ( ! empty( $match['vs_image']['url'] ) ) : ?>
                                                <img src="<?php echo esc_url( $match['vs_image']['url'] ); ?>" alt="vs">
                                            <?php endif; ?>
                                            <?php if ( ! empty( $match['match_image_2']['url'] ) ) : ?>
                                                <img src="<?php echo esc_url( $match['match_image_2']['url'] ); ?>" class="gt-match-thumb" alt="img">
                                            <?php endif; ?>
                                        </div>

                                        <?php if ( ! empty( $match['watch_platforms'] ) ) : ?>
                                            <div class="gt-watch-now-items">
                                                <span><?php esc_html_e( 'Watch live on', 'feron-core' ); ?></span>
                                                <ul class="gt-watch-now-list">
                                                    <?php foreach ( $match['watch_platforms'] as $platform ) : ?>
                                                        <li>
                                                            <a href="<?php echo esc_url( $platform['platform_link']['url'] ); ?>"
                                                               <?php echo $platform['platform_link']['is_external'] ? ' target="_blank"' : ''; ?>
                                                               <?php echo $platform['platform_link']['nofollow'] ? ' rel="nofollow"' : ''; ?>>
                                                                <?php if ( ! empty( $platform['platform_icon']['value'] ) ) : ?>
                                                                    <i class="<?php echo esc_attr( $platform['platform_icon']['value'] ); ?>"></i>
                                                                <?php endif; ?>
                                                                <?php echo esc_html( $platform['platform_name'] ); ?>
                                                            </a>
                                                        </li>
                                                    <?php endforeach; ?>
                                                </ul>
                                            </div>
                                        <?php endif; ?>
                                    </div>
                                <?php endif; ?>

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

                </div>
            </div>
        </section>

        <?php
    }
}

Plugin::instance()->widgets_manager->register( new Match_List() );
