<?php
namespace Elementor;

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

/**
 * Elementor image carousel widget.
 *
 * Elementor widget that displays a set of images in a rotating carousel or
 * slider.
 *
 * @since 1.0.0
 */
class Pakrco_Elementor_Image_Marquee extends Widget_Base {

    /**
     * Get widget name.
     *
     * Retrieve image carousel widget name.
     *
     * @since 1.0.0
     * @access public
     *
     * @return string Widget name.
     */
    public function get_name() {
        return 'image-marquee';
    }

    /**
     * Get widget title.
     *
     * Retrieve image carousel widget title.
     *
     * @since 1.0.0
     * @access public
     *
     * @return string Widget title.
     */
    public function get_title() {
        return esc_html__( 'Image Marquee', 'pakrco' );
    }

    /**
     * Get widget icon.
     *
     * Retrieve image carousel widget icon.
     *
     * @since 1.0.0
     * @access public
     *
     * @return string Widget icon.
     */
    public function get_icon() {
        return 'eicon-slider-push';
    }

    /**
     * Get widget keywords.
     *
     * Retrieve the list of keywords the widget belongs to.
     *
     * @since 2.1.0
     * @access public
     *
     * @return array Widget keywords.
     */
    public function get_keywords() {
        return [ 'image', 'photo', 'brand', 'carousel', 'slider' ];
    }

    public function get_script_depends() {
        return [ 'pakrco-elementor-image-marquee' ];
    }

    /**
     * Register image carousel widget controls.
     *
     * Adds different input fields to allow the user to change and customize the widget settings.
     *
     * @since 3.1.0
     * @access protected
     */
    protected function register_controls() {
        $this->start_controls_section(
            'section_image_carousel',
            [
                'label' => esc_html__( 'Image Carousel', 'pakrco' ),
            ]
        );

        $this->add_control(
            'carousel',
            [
                'label' => esc_html__( 'Add Images', 'pakrco' ),
                'type' => Controls_Manager::GALLERY,
                'default' => [],
                'show_label' => false,
                'dynamic' => [
                    'active' => true,
                ],
            ]
        );

        $this->add_group_control(
            Group_Control_Image_Size::get_type(),
            [
                'name' => 'thumbnail', // Usage: `{name}_size` and `{name}_custom_dimension`, in this case `thumbnail_size` and `thumbnail_custom_dimension`.
                'separator' => 'none',
            ]
        );

        $slides_to_show = range( 1, 10 );
        $slides_to_show = array_combine( $slides_to_show, $slides_to_show );

        $this->add_responsive_control(
            'slides_to_show',
            [
                'label' => esc_html__( 'Slides to Show', 'pakrco' ),
                'type' => Controls_Manager::SELECT,
                'options' => $slides_to_show,
                'render_type' => 'template',
                'default' => 4,
                'selectors' => [
                    '{{WRAPPER}}' => '--e-image-carousel-slides-to-show: {{VALUE}}',
                ],
            ]
        );

        $this->add_responsive_control('height', [
            'label'          => esc_html__('Height', 'pakrco'),
            'type'           => Controls_Manager::SLIDER,
            'default'        => [
                    'unit' => 'px',
                    'size' => 400
            ],
            'tablet_default' => [
                'unit' => 'px',
            ],
            'mobile_default' => [
                'unit' => 'px',
            ],
            'size_units'     => ['px'],
            'range'          => [
                'px' => [
                    'min' => 1,
                    'max' => 1600,
                ],
            ],
            'selectors'      => [
                '{{WRAPPER}} .pakrco-image-marquee, {{WRAPPER}} .flickity-viewport' => 'height: {{SIZE}}{{UNIT}}!important;',
            ],
            'render_type'    => 'template',
        ]);

        $this->add_responsive_control('gutter', [
            'label'          => esc_html__('Gutter', 'pakrco'),
            'type'           => Controls_Manager::SLIDER,
            'default'        => [
                'unit' => 'px',
                'size' => 0
            ],
            'tablet_default' => [
                'unit' => 'px',
            ],
            'mobile_default' => [
                'unit' => 'px',
            ],
            'size_units'     => ['px'],
            'range'          => [
                'px' => [
                    'min' => 1,
                    'max' => 60,
                ],
            ],
            'selectors'      => [
                '{{WRAPPER}} .swiper-slide' => 'padding: 0 calc({{SIZE}}{{UNIT}} / 2);',
            ],
            'render_type'    => 'template',
        ]);

        $this->add_control(
            'link_to',
            [
                'label' => esc_html__( 'Link', 'pakrco' ),
                'type' => Controls_Manager::SELECT,
                'default' => 'none',
                'options' => [
                    'none' => esc_html__( 'None', 'pakrco' ),
                    'file' => esc_html__( 'Media File', 'pakrco' ),
                    'custom' => esc_html__( 'Custom URL', 'pakrco' ),
                    'custom-link-item' => esc_html__( 'Custom URL in Image', 'pakrco' ),
                ],
            ]
        );

        $this->add_control(
            'link',
            [
                'label' => esc_html__( 'Link', 'pakrco' ),
                'type' => Controls_Manager::URL,
                'placeholder' => esc_html__( 'https://your-link.com', 'pakrco' ),
                'condition' => [
                    'link_to' => 'custom',
                ],
                'show_label' => false,
            ]
        );

        $this->add_control(
            'open_lightbox',
            [
                'label' => esc_html__( 'Lightbox', 'pakrco' ),
                'type' => Controls_Manager::SELECT,
                'default' => 'default',
                'options' => [
                    'default' => esc_html__( 'Default', 'pakrco' ),
                    'yes' => esc_html__( 'Yes', 'pakrco' ),
                    'no' => esc_html__( 'No', 'pakrco' ),
                ],
                'condition' => [
                    'link_to' => 'file',
                ],
            ]
        );

        $this->end_controls_section();



        $this->start_controls_section(
            'section_style_image',
            [
                'label' => esc_html__( 'Image', 'pakrco' ),
                'tab' => Controls_Manager::TAB_STYLE,
            ]
        );

        $this->add_responsive_control(
            'gallery_vertical_align',
            [
                'label' => esc_html__( 'Vertical Align', 'pakrco' ),
                'type' => Controls_Manager::CHOOSE,
                'options' => [
                    'flex-start' => [
                        'title' => esc_html__( 'Start', 'pakrco' ),
                        'icon' => 'eicon-v-align-top',
                    ],
                    'center' => [
                        'title' => esc_html__( 'Center', 'pakrco' ),
                        'icon' => 'eicon-v-align-middle',
                    ],
                    'flex-end' => [
                        'title' => esc_html__( 'End', 'pakrco' ),
                        'icon' => 'eicon-v-align-bottom',
                    ],
                ],
                'condition' => [
                    'slides_to_show!' => '1',
                ],
                'selectors' => [
                    '{{WRAPPER}} .swiper-wrapper' => 'display: flex; align-items: {{VALUE}};',
                ],
            ]
        );

        $this->add_control(
            'image_spacing',
            [
                'label' => esc_html__( 'Spacing', 'pakrco' ),
                'type' => Controls_Manager::SELECT,
                'options' => [
                    '' => esc_html__( 'Default', 'pakrco' ),
                    'custom' => esc_html__( 'Custom', 'pakrco' ),
                ],
                'default' => '',
                'condition' => [
                    'slides_to_show!' => '1',
                ],
            ]
        );

        $this->add_control(
            'image_spacing_custom',
            [
                'label' => esc_html__( 'Image Spacing', 'pakrco' ),
                'type' => Controls_Manager::SLIDER,
                'range' => [
                    'px' => [
                        'max' => 100,
                    ],
                ],
                'default' => [
                    'size' => 20,
                ],
                'show_label' => false,
                'condition' => [
                    'image_spacing' => 'custom',
                    'slides_to_show!' => '1',
                ],
                'frontend_available' => true,
                'render_type' => 'none',
                'separator' => 'after',
            ]
        );

        $this->add_group_control(
            Group_Control_Border::get_type(),
            [
                'name' => 'image_border',
                'selector' => '{{WRAPPER}} .elementor-image-carousel-wrapper .elementor-image-carousel .swiper-slide-image',
            ]
        );

        $this->add_control(
            'image_border_radius',
            [
                'label' => esc_html__( 'Border Radius', 'pakrco' ),
                'type' => Controls_Manager::DIMENSIONS,
                'size_units' => [ 'px', '%' ],
                'selectors' => [
                    '{{WRAPPER}} .elementor-image-carousel-wrapper .elementor-image-carousel .swiper-slide-image' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
                ],
            ]
        );

        $this->end_controls_section();

    }

    /**
     * Render image carousel widget output on the frontend.
     *
     * Written in PHP and used to generate the final HTML.
     *
     * @since 1.0.0
     * @access protected
     */
    protected function render() {
        $settings = $this->get_settings_for_display();

        if ( empty( $settings['carousel'] ) ) {
            return;
        }

        $slides = [];

        foreach ( $settings['carousel'] as $index => $attachment ) {
            $image_url = Group_Control_Image_Size::get_attachment_image_src( $attachment['id'], 'thumbnail', $settings );

            if ( ! $image_url && isset( $attachment['url'] ) ) {
                $image_url = $attachment['url'];
            }

            $image_html = '<img class="swiper-slide-image" src="' . esc_attr( $image_url ) . '" alt="' . esc_attr( Control_Media::get_image_alt( $attachment ) ) . '" />';

            $link_tag = '';

            $link = $this->get_link_url( $attachment, $settings );

            if ( $link ) {
                $link_key = 'link_' . $index;

                $this->add_lightbox_data_attributes( $link_key, $attachment['id'], $settings['open_lightbox'], $this->get_id() );

                if ( Plugin::$instance->editor->is_edit_mode() ) {
                    $this->add_render_attribute( $link_key, [
                        'class' => 'elementor-clickable',
                    ] );
                }

                $this->add_link_attributes( $link_key, $link );

                $link_tag = '<a ' . $this->get_render_attribute_string( $link_key ) . '>';
            }

            $image_caption = $this->get_image_caption( $attachment );
            ob_start();
            $slide_html =  $link_tag . '<figure class="swiper-slide-inner">' . $image_html;

            if ( ! empty( $image_caption ) ) {
                $slide_html .= '<figcaption class="elementor-image-carousel-caption">' . wp_kses_post( $image_caption ) . '</figcaption>';
            }

            $slide_html .= '</figure>';

            if ( $link ) {
                $slide_html .= '</a>';
            }

            printf('%s', $slide_html);

            $slides[] = '<div class="swiper-slide">' . pakrco_render_skeleton('skeleton-image-marquee', 1, [], false) . '</div>';

        }

        if ( empty( $slides ) ) {
            return;
        }

        ?>
        <div class="pakrco-image-marquee">
            <?php // PHPCS - $slides contains the slides content, all the relevent content is escaped above. ?>
            <?php echo implode( '', $slides ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
        </div>
        <?php
    }

    /**
     * Retrieve image carousel link URL.
     *
     * @since 1.0.0
     * @access private
     *
     * @param array $attachment
     * @param object $instance
     *
     * @return array|string|false An array/string containing the attachment URL, or false if no link.
     */
    private function get_link_url( $attachment, $instance ) {
        if ( 'none' === $instance['link_to'] ) {
            return false;
        }

        if ( 'custom' === $instance['link_to'] ) {
            if ( empty( $instance['link']['url'] ) ) {
                return false;
            }

            return $instance['link'];
        }

        if('custom-link-item' === $instance['link_to']){
            return [
                'url' => get_post_meta($attachment['id'], 'lb_custom_media_link', true)
            ];
        }

        return [
            'url' => wp_get_attachment_url( $attachment['id'] ),
        ];
    }

    /**
     * Retrieve image carousel caption.
     *
     * @since 1.2.0
     * @access private
     *
     * @param array $attachment
     *
     * @return string The caption of the image.
     */
    private function get_image_caption( $attachment ) {
        $caption_type = $this->get_settings_for_display( 'caption_type' );

        if ( empty( $caption_type ) ) {
            return '';
        }

        $attachment_post = get_post( $attachment['id'] );

        if ( 'caption' === $caption_type ) {
            return $attachment_post->post_excerpt;
        }

        if ( 'title' === $caption_type ) {
            return $attachment_post->post_title;
        }

        return $attachment_post->post_content;
    }
}


$widgets_manager->register_widget_type(new Pakrco_Elementor_Image_Marquee());
