<?php

namespace ElementPack\Modules\PortfolioCarousel\Widgets;

use Elementor\Controls_Manager;
use Elementor\Group_Control_Border;
use Elementor\Group_Control_Image_Size;
use Elementor\Group_Control_Typography;
use Elementor\Group_Control_Box_Shadow;
use Elementor\Group_Control_Text_Shadow;
use Elementor\Group_Control_Background;

use ElementPack\Utils;
use ElementPack\Base\Module_Base;
use ElementPack\Traits\Global_Swiper_Controls;
use ElementPack\Includes\Controls\GroupQuery\Group_Control_Query;
use ElementPack\Modules\PortfolioCarousel\Skins;
use WP_Query;

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

class Portfolio_Carousel extends Module_Base {
    use Group_Control_Query;
    use Global_Swiper_Controls;

    private $_query = null;

    public function get_name() {
        return 'bdt-portfolio-carousel';
    }

    public function get_title() {
        return BDTEP . esc_html__('Portfolio Carousel', 'bdthemes-element-pack');
    }

    public function get_icon() {
        return 'bdt-wi-portfolio-carousel';
    }

    public function get_categories() {
        return ['element-pack'];
    }

    public function get_keywords() {
        return ['portfolio', 'gallery', 'blog', 'recent', 'news', 'works', 'portfolio-carousel'];
    }

    public function get_style_depends(): array {
        return $this->ep_is_edit_mode() ? [ 'swiper', 'ep-styles' ] : [ 'swiper', 'ep-font', 'ep-portfolio-carousel' ];
    }

    public function get_script_depends(): array {
        return $this->ep_is_edit_mode() ? [ 'swiper', 'imagesloaded', 'ep-scripts' ] : [ 'swiper', 'imagesloaded', 'ep-portfolio-carousel' ];
    }

    public function register_skins() {
        $this->add_skin(new Skins\Skin_Abetis($this));
        $this->add_skin(new Skins\Skin_Fedara($this));
        $this->add_skin(new Skins\Skin_Trosia($this));
        $this->add_skin(new Skins\Skin_Janes($this));
    }

    public function on_import($element) {
        if (!get_post_type_object($element['settings']['posts_post_type'])) {
            $element['settings']['posts_post_type'] = 'post';
        }
        return $element;
    }

    public function get_query() {
        return $this->_query;
    }

    public function register_controls() {
        $this->register_section_controls();
    }

    public function has_widget_inner_wrapper(): bool {
        return ! \Elementor\Plugin::$instance->experiments->is_feature_active( 'e_optimized_markup' );
    }

    private function register_section_controls() {

        $this->start_controls_section(
            'section_carousel_layout',
            [
                'label' => esc_html__( 'Layout', 'bdthemes-element-pack' ),
            ]
        );

        //swiper carousel columns & item gap controls
		$this->register_carousel_column_gap_controls();

        $this->add_control(
            'match_height',
            [
                'label' => esc_html__( 'Item Match Height', 'bdthemes-element-pack' ),
                'type'  => Controls_Manager::SWITCHER,
            ]
        );

        $this->add_group_control(
            Group_Control_Image_Size::get_type(),
            [
                'name'         => 'thumbnail_size',
                'label'        => esc_html__('Image Size', 'bdthemes-element-pack'),
                'exclude'      => ['custom'],
                'default'      => 'medium',
                'prefix_class' => 'bdt-portfolio--thumbnail-size-',
            ]
        );

        $this->end_controls_section();

        //New Query Builder Settings
        $this->start_controls_section(
            'section_post_query_builder',
            [
                'label' => esc_html__( 'Query', 'bdthemes-element-pack' ),
                'tab' => Controls_Manager::TAB_CONTENT,
            ]
        );

        $this->register_query_builder_controls();

        $this->update_control(
            'posts_source',
            [
                'type' => Controls_Manager::SELECT,
                'default' => 'portfolio'
            ]
        );

        $this->update_control(
            'posts_per_page',
            [
                'default' => 9,
            ]
        );

        $this->end_controls_section();

        $this->start_controls_section(
            'section_layout_additional',
            [
                'label' => esc_html__('Additional Options', 'bdthemes-element-pack'),
                'tab' => Controls_Manager::TAB_CONTENT,
            ]
        );

        $this->add_control(
            'show_title',
            [
                'label' => esc_html__('Title', 'bdthemes-element-pack'),
                'type' => Controls_Manager::SWITCHER,
                'default' => 'yes',
            ]
        );

        $this->add_control(
            'title_tag',
            [
                'label' => esc_html__('Title HTML Tag', 'bdthemes-element-pack'),
                'type' => Controls_Manager::SELECT,
                'options' => element_pack_title_tags(),
                'default' => 'h4',
                'condition' => [
                    'show_title' => 'yes',
                ],
            ]
        );

        $this->add_control(
            'show_excerpt',
            [
                'label' => esc_html__('Show Text', 'bdthemes-element-pack'),
                'type' => Controls_Manager::SWITCHER,
            ]
        );

        $this->add_control(
            'excerpt_limit',
            [
                'label' => esc_html__('Text Limit', 'bdthemes-element-pack'),
                'description' => esc_html__('It\'s just work for main content, but not working with excerpt. If you set 0 so you will get full main content.', 'bdthemes-element-pack'),
                'type' => Controls_Manager::NUMBER,
                'default' => 10,
                'condition' => [
                    'show_excerpt' => 'yes',
                ],
            ]
        );

        $this->add_control(
            'ellipsis',
            [
                'label' => esc_html__('Ellipsis', 'bdthemes-element-pack'),
                'type' => Controls_Manager::TEXT,
                'dynamic' => ['active' => true],
                'condition' => [
                    'show_excerpt' => 'yes',
					'excerpt_limit!' => [0, ''],
                ],
				'ai' => [
                    'active' => false,
                ],
            ]
        );

        $this->add_control(
            'strip_shortcode',
            [
                'label'   => esc_html__('Strip Shortcode', 'bdthemes-element-pack'),
                'type'    => Controls_Manager::SWITCHER,
                'default' => 'yes',
                'condition'   => [
                    'show_excerpt' => 'yes',
                ],
            ]
        );

        $this->add_control(
            'show_category',
            [
                'label' => esc_html__('Show Category', 'bdthemes-element-pack'),
                'type' => Controls_Manager::SWITCHER,
            ]
        );

        $this->add_control(
            'show_link',
            [
                'label' => esc_html__('Show Link', 'bdthemes-element-pack'),
                'type' => Controls_Manager::SELECT,
                'default' => 'both',
                'options' => [
                    'post' => esc_html__('Details Link', 'bdthemes-element-pack'),
                    'lightbox' => esc_html__('Lightbox Link', 'bdthemes-element-pack'),
                    'both' => esc_html__('Both', 'bdthemes-element-pack'),
                    'none' => esc_html__('None', 'bdthemes-element-pack'),
                ],
            ]
        );

        $this->add_control(
            'external_link',
            [
                'label' => esc_html__('Show in new Tab (Details Link/Title)', 'bdthemes-element-pack'),
                'type' => Controls_Manager::SWITCHER,
                'conditions' => [
                    'relation' => 'or',
                    'terms' => [
                        [
                            'name' => 'show_title',
                            'operator' => '==',
                            'value' => 'yes'
                        ],
                        [
                            'name' => 'show_link',
                            'operator' => '==',
                            'value' => ['post', 'both'],
                        ],
                    ]
                ],
            ]
        );

        $this->add_control(
            'link_type',
            [
                'label' => esc_html__('Link Type', 'bdthemes-element-pack'),
                'type' => Controls_Manager::SELECT,
                'default' => 'icon',
                'options' => [
                    'icon' => esc_html__('Icon', 'bdthemes-element-pack'),
                    'text' => esc_html__('Text', 'bdthemes-element-pack'),
                ],
                'condition' => [
                    'show_link!' => 'none',
                ]
            ]
        );

        $this->add_control(
			'post_link_text',
			[
				'label'       => esc_html__('Details Link Text', 'bdthemes-element-pack'),
				'type'        => Controls_Manager::TEXT,
                'dynamic'     => [ 'active' => true ],
				'default'     => esc_html__('VIEW', 'bdthemes-element-pack'),
				'condition' => [
					'show_link' => [ 'post', 'both' ],
					'link_type'		=> 'text',
				],
				'label_block' => false
			]
		);

        $this->add_control(
			'lightbox_link_text',
			[
				'label'       => esc_html__('Lightbox Link Text', 'bdthemes-element-pack'),
				'type'        => Controls_Manager::TEXT,
                'dynamic'     => [ 'active' => true ],
				'default'     => esc_html__('ZOOM', 'bdthemes-element-pack'),
				'condition' => [
					'show_link' => [ 'lightbox', 'both' ],
					'link_type'		=> 'text',
				],
				'label_block' => false
			]
		);

        $this->add_control(
            'lightbox_animation',
            [
                'label' => esc_html__('Lightbox Animation', 'bdthemes-element-pack'),
                'type' => Controls_Manager::SELECT,
                'default' => 'slide',
                'options' => [
                    'slide' => esc_html__('Slide', 'bdthemes-element-pack'),
                    'fade' => esc_html__('Fade', 'bdthemes-element-pack'),
                    'scale' => esc_html__('Scale', 'bdthemes-element-pack'),
                ],
                'condition' => [
                    'show_link' => ['both', 'lightbox'],
                ],
                'separator' => 'before',
            ]
        );

        $this->add_control(
            'lightbox_autoplay',
            [
                'label' => esc_html__( 'Lightbox Autoplay', 'bdthemes-element-pack' ),
                'type' => Controls_Manager::SWITCHER,
                'condition' => [
                    'show_link' => ['both', 'lightbox'],
                ]
            ]
        );

        $this->add_control(
            'lightbox_pause',
            [
                'label' => esc_html__( 'Lightbox Pause on Hover', 'bdthemes-element-pack' ),
                'type' => Controls_Manager::SWITCHER,
                'condition' => [
                    'show_link' => ['both', 'lightbox'],
                    'lightbox_autoplay' => 'yes'
                ],

            ]
        );

        $this->end_controls_section();

        //Navigation Controls
        $this->start_controls_section(
            'section_content_navigation',
            [
                'label' => esc_html__( 'Navigation', 'bdthemes-element-pack' ),
            ]
        );

        //Global Navigation Controls
        $this->register_navigation_controls();

        $this->end_controls_section();

        //Global Carousel Settings Controls
        $this->register_carousel_settings_controls();

        //Style
        $this->start_controls_section(
            'section_design_layout',
            [
                'label' => esc_html__('Items', 'bdthemes-element-pack'),
                'tab' => Controls_Manager::TAB_STYLE,
            ]
        );

        $this->add_control(
            'overlay_style_headline',
            [
                'label'     => esc_html__('Overlay', 'bdthemes-element-pack'),
                'type'      => Controls_Manager::HEADING,
                'condition' => [
                    '_skin!' => ['bdt-janes', 'bdt-trosia'],
                ],
            ]
        );

        $this->add_group_control(
            Group_Control_Background::get_type(),
            [
                'name' => 'overlay_skin_abetis_background',
                'label' => esc_html__( 'Background', 'bdthemes-element-pack' ),
                'types' => ['classic', 'gradient'],
                'selector' => '{{WRAPPER}} .bdt-portfolio-carousel.skin-abetis .bdt-portfolio-inner:before, {{WRAPPER}} .bdt-portfolio-carousel.skin-fedara .bdt-portfolio-inner:before',
                'condition' => [
                    '_skin' => ['bdt-abetis', 'bdt-fedara']
                ],
            ]
        );

        $this->add_control(
            'overlay_primary_background',
            [
                'label' => esc_html__('Primary Color', 'bdthemes-element-pack'),
                'type' => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .bdt-portfolio-carousel.skin-default .bdt-portfolio-content-inner:before' => 'background: {{VALUE}};',
                ],
                'condition' => [
                    '_skin' => '',
                ],
            ]
        );

        $this->add_control(
            'overlay_secondary_background',
            [
                'label' => esc_html__('Secondary Color', 'bdthemes-element-pack'),
                'type' => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .bdt-portfolio-carousel.skin-default .bdt-portfolio-content-inner:after' => 'background: {{VALUE}};',
                ],
                'condition' => [
                    '_skin' => '',
                ],
            ]
        );

        $this->add_control(
            'portfolio_content_style_headline',
            [
                'label' => esc_html__('Content', 'bdthemes-element-pack'),
                'type' => Controls_Manager::HEADING,
                'separator' => 'before',
            ]
        );

        $this->add_responsive_control(
            'content_width',
            [
                'label'     => esc_html__('Content Width(%)', 'bdthemes-element-pack'),
                'type'      => Controls_Manager::SLIDER,
                'selectors' => [
                    '{{WRAPPER}} .bdt-portfolio-carousel.skin-janes .bdt-gallery-item .bdt-portfolio-inner .bdt-portfolio-desc' => 'right: calc(100% - {{SIZE}}%);',
                ],
                'condition' => [
                    '_skin' => 'bdt-janes',
                ],
            ]
        );

        $this->add_responsive_control(
            'portfolio_content_alignment',
            [
                'label' => esc_html__( 'Alignment', 'bdthemes-element-pack' ),
                'type' => Controls_Manager::CHOOSE,
                'options' => [
                    'left' => [
                        'title' => esc_html__( 'Left', 'bdthemes-element-pack' ),
                        'icon' => 'eicon-text-align-left',
                    ],
                    'center' => [
                        'title' => esc_html__( 'Center', 'bdthemes-element-pack' ),
                        'icon' => 'eicon-text-align-center',
                    ],
                    'right' => [
                        'title' => esc_html__( 'Right', 'bdthemes-element-pack' ),
                        'icon' => 'eicon-text-align-right',
                    ],
                ],
                'default' => 'center',
                'prefix_class' => 'bdt-custom-gallery-skin-fedara-style-',
                'selectors' => [
                    '{{WRAPPER}} .bdt-portfolio-carousel .bdt-portfolio-desc, {{WRAPPER}} .bdt-portfolio-carousel .bdt-portfolio-skin-fedara-desc' => 'text-align: {{VALUE}}',
                ],
                // 'condition' => [
                // 	'_skin!' => 'bdt-trosia',
                // ],
            ]
        );

        // $this->add_control(
        //     'desc_background_color',
        //     [
        //         'label' => esc_html__('Background Color', 'bdthemes-element-pack'),
        //         'type' => Controls_Manager::COLOR,
        //         'selectors' => [
        //             '{{WRAPPER}} .bdt-portfolio-carousel .bdt-portfolio-desc, {{WRAPPER}} .bdt-portfolio-carousel .bdt-portfolio-skin-fedara-desc' => 'background: {{VALUE}};',
        //         ],
        //         'condition' => [
        //             '_skin!' => 'bdt-abetis',
        //         ],
        //     ]
        // );

        $this->add_group_control(
            Group_Control_Background::get_type(),
            [
                'name'      => 'desc_background_color',
                'selector'  => '{{WRAPPER}} .bdt-portfolio-carousel .bdt-portfolio-desc, {{WRAPPER}} .bdt-portfolio-carousel .bdt-portfolio-skin-fedara-desc',
                'condition' => [
                    '_skin!' => 'bdt-abetis',
                ],
            ]
        );

        $this->add_responsive_control(
            'desc__padding',
            [
                'label' => esc_html__('Padding', 'bdthemes-element-pack'),
                'type' => Controls_Manager::DIMENSIONS,
                'size_units' => ['px', 'em', '%'],
                'selectors' => [
                    '{{WRAPPER}} .bdt-portfolio-carousel .bdt-portfolio-desc, {{WRAPPER}} .bdt-portfolio-carousel .bdt-portfolio-skin-fedara-desc' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
                ],
            ]
        );

        $this->add_responsive_control(
            'content_border_radius',
            [
                'label'      => esc_html__('Border Radius', 'bdthemes-element-pack'),
                'type'       => Controls_Manager::DIMENSIONS,
                'size_units' => ['px', '%'],
                'selectors'  => [
                    '{{WRAPPER}} .bdt-portfolio-carousel .bdt-portfolio-desc, {{WRAPPER}} .bdt-portfolio-carousel .bdt-portfolio-skin-fedara-desc' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
                ],
            ]
        );

        $this->add_responsive_control(
            'title_spacing',
            [
                'label'     => esc_html__('Spacing', 'bdthemes-element-pack'),
                'type'      => Controls_Manager::SLIDER,
                'selectors' => [
                    '{{WRAPPER}} .bdt-portfolio-carousel.skin-janes .bdt-gallery-item .bdt-gallery-item-tags' => 'padding-top: {{SIZE}}{{UNIT}};',
                ],
                'condition' => [
                    'show_category' => 'yes',
                    '_skin' => 'bdt-janes',
                ],
            ]
        );

        $this->add_control(
            'portfolio_item_headline',
            [
                'label'     => esc_html__('Item', 'bdthemes-element-pack'),
                'type'      => Controls_Manager::HEADING,
                'separator' => 'before',
            ]
        );

        $this->start_controls_tabs('tabs_item_style');

        $this->start_controls_tab(
            'tab_item_normal',
            [
                'label' => esc_html__( 'Normal', 'bdthemes-element-pack' ),
            ]
        );

        $this->add_group_control(
            Group_Control_Border::get_type(),
            [
                'name'        => 'item_border',
                'label'       => esc_html__( 'Border', 'bdthemes-element-pack' ),
                'placeholder' => '1px',
                'default'     => '1px',
                'selector'    => '{{WRAPPER}} .bdt-portfolio-carousel .bdt-gallery-item',
            ]
        );

        $this->add_responsive_control(
            'item_border_radius',
            [
                'label' => esc_html__('Border Radius', 'bdthemes-element-pack'),
                'type' => Controls_Manager::DIMENSIONS,
                'size_units' => ['px', '%'],
                'selectors' => [
                    '{{WRAPPER}} .bdt-portfolio-carousel .bdt-gallery-item, {{WRAPPER}} .bdt-portfolio-carousel .swiper-carousel' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
                ],
                'condition' => [
                    '_skin!' => 'bdt-janes'
                ],
            ]
        );

        $this->add_group_control(
            Group_Control_Box_Shadow::get_type(),
            [
                'name'     => 'item_shadow',
                'selector' => '{{WRAPPER}} .bdt-portfolio-carousel .bdt-gallery-item',
            ]
        );

        $this->end_controls_tab();

        $this->start_controls_tab(
            'tab_item_hover',
            [
                'label' => esc_html__( 'Hover', 'bdthemes-element-pack' ),
            ]
        );

        $this->add_control(
            'item_hover_border_color',
            [
                'label'     => esc_html__( 'Border Color', 'bdthemes-element-pack' ),
                'type'      => Controls_Manager::COLOR,
                'condition' => [
                    'item_border_border!' => '',
                ],
                'selectors' => [
                    '{{WRAPPER}} .bdt-portfolio-carousel .bdt-gallery-item:hover' => 'border-color: {{VALUE}};',
                ],
            ]
        );

        $this->add_group_control(
            Group_Control_Box_Shadow::get_type(),
            [
                'name'     => 'item_hover_shadow',
                'selector' => '{{WRAPPER}} .bdt-portfolio-carousel .bdt-gallery-item:hover',
            ]
        );

        $this->end_controls_tab();

        $this->end_controls_tabs();

        $this->end_controls_section();

        $this->start_controls_section(
            'section_style_title',
            [
                'label'     => esc_html__('Title', 'bdthemes-element-pack'),
                'tab'       => Controls_Manager::TAB_STYLE,
                'condition' => [
                    'show_title' => 'yes',
                ],
            ]
        );

        $this->add_control(
            'title_color',
            [
                'label' => esc_html__('Color', 'bdthemes-element-pack'),
                'type' => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .bdt-portfolio-carousel .bdt-gallery-item .bdt-gallery-item-title' => 'color: {{VALUE}};',
                ],
            ]
        );

        $this->add_control(
            'title_hover_color',
            [
                'label' => esc_html__('Hover Color', 'bdthemes-element-pack'),
                'type' => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .bdt-portfolio-carousel .bdt-gallery-item .bdt-gallery-item-title:hover' => 'color: {{VALUE}};',
                ],
            ]
        );

        $this->add_group_control(
            Group_Control_Typography::get_type(),
            [
                'name' => 'title_typography',
                'label' => esc_html__('Typography', 'bdthemes-element-pack'),
                'selector' => '{{WRAPPER}} .bdt-gallery-item .bdt-gallery-item-title',
            ]
        );

        $this->add_group_control(
            Group_Control_Text_Shadow::get_type(),
            [
                'name' => 'title_shadow',
                'label' => esc_html__( 'Text Shadow', 'bdthemes-element-pack' ),
                'selector' => '{{WRAPPER}} .bdt-gallery-item .bdt-gallery-item-title',
            ]
        );

        $this->end_controls_section();

        $this->start_controls_section(
            'section_style_excerpt',
            [
                'label' => esc_html__('Text', 'bdthemes-element-pack'),
                'tab' => Controls_Manager::TAB_STYLE,
                'condition' => [
                    'show_excerpt' => 'yes',
                ],
            ]
        );

        $this->add_control(
            'excerpt_color',
            [
                'label' => esc_html__('Color', 'bdthemes-element-pack'),
                'type' => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .bdt-portfolio-carousel .bdt-portfolio-excerpt' => 'color: {{VALUE}};',
                ],
            ]
        );

        $this->add_responsive_control(
            'excerpt_margin',
            [
                'label' => esc_html__('Margin', 'bdthemes-element-pack'),
                'type' => Controls_Manager::DIMENSIONS,
                'selectors' => [
                    '{{WRAPPER}} .bdt-portfolio-carousel .bdt-portfolio-excerpt' => 'margin: {{TOP}}px {{RIGHT}}px {{BOTTOM}}px {{LEFT}}px;',
                ],
            ]
        );

        $this->add_group_control(
            Group_Control_Typography::get_type(),
            [
                'name' => 'excerpt_typography',
                'label' => esc_html__('Typography', 'bdthemes-element-pack'),
                'selector' => '{{WRAPPER}} .bdt-portfolio-carousel .bdt-portfolio-excerpt',
            ]
        );

        $this->end_controls_section();

        $this->start_controls_section(
            'section_style_button',
            [
                'label' => esc_html__('Button', 'bdthemes-element-pack'),
                'tab' => Controls_Manager::TAB_STYLE,
                'condition' => [
                    'show_link!' => 'none',
                ],
            ]
        );

        $this->start_controls_tabs('tabs_button_style');

        $this->start_controls_tab(
            'tab_button_normal',
            [
                'label' => esc_html__('Normal', 'bdthemes-element-pack'),
            ]
        );

        $this->add_control(
            'button_text_color',
            [
                'label' => esc_html__('Color', 'bdthemes-element-pack'),
                'type' => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .bdt-portfolio-carousel .bdt-gallery-item-link, {{WRAPPER}} .bdt-portfolio-carousel .bdt-gallery-item-link i, {{WRAPPER}} .bdt-portfolio-carousel .bdt-gallery-item-link span' => 'color: {{VALUE}};',
                ],
            ]
        );

        $this->add_control(
            'background_color',
            [
                'label' => esc_html__('Background Color', 'bdthemes-element-pack'),
                'type' => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .bdt-portfolio-carousel .bdt-gallery-item-link' => 'background-color: {{VALUE}};',
                ],
            ]
        );

        $this->add_group_control(
            Group_Control_Box_Shadow::get_type(),
            [
                'name' => 'button_box_shadow',
                'selector' => '{{WRAPPER}} .bdt-portfolio-carousel .bdt-gallery-item-link',
            ]
        );

        $this->add_group_control(
            Group_Control_Border::get_type(),
            [
                'name' => 'border',
                'label' => esc_html__('Border', 'bdthemes-element-pack'),
                'placeholder' => '1px',
                'default' => '1px',
                'selector' => '{{WRAPPER}} .bdt-portfolio-carousel .bdt-gallery-item-link',
            ]
        );

        $this->add_responsive_control(
            'button_border_radius',
            [
                'label' => esc_html__('Border Radius', 'bdthemes-element-pack'),
                'type' => Controls_Manager::DIMENSIONS,
                'size_units' => ['px', '%'],
                'selectors' => [
                    '{{WRAPPER}} .bdt-portfolio-carousel .bdt-gallery-item-link' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
                ],
                'condition' => [
                    'border_radius_advanced_show!' => 'yes',
                ],
            ]
        );

        $this->add_control(
            'border_radius_advanced_show',
            [
                'label' => esc_html__( 'Advanced Radius', 'bdthemes-element-pack' ),
                'type' => Controls_Manager::SWITCHER,
            ]
        );

        $this->add_control(
            'border_radius_advanced',
            [
                'label' => esc_html__('Radius', 'bdthemes-element-pack'),
                'description' => sprintf( esc_html__( 'For example: <b>%1s</b> or Go <a href="%2s" target="_blank">this link</a> and copy and paste the radius value.', 'bdthemes-element-pack' ), '30% 70% 82% 18% / 46% 62% 38% 54%', 'https://9elements.github.io/fancy-border-radius/' ),
                'type' => Controls_Manager::TEXT,
                'size_units' => ['px', '%'],
                'default' => '30% 70% 82% 18% / 46% 62% 38% 54%',
                'selectors' => [
                    '{{WRAPPER}} .bdt-portfolio-carousel .bdt-gallery-item-link' => 'border-radius: {{VALUE}}; overflow: hidden;',
                ],
                'condition' => [
                    'border_radius_advanced_show' => 'yes',
                ],
            ]
        );

        $this->add_responsive_control(
            'button_padding',
            [
                'label' => esc_html__('Padding', 'bdthemes-element-pack'),
                'type' => Controls_Manager::DIMENSIONS,
                'size_units' => ['px', 'em', '%'],
                'selectors' => [
                    '{{WRAPPER}} .bdt-portfolio-carousel .bdt-gallery-item-link' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
                ],
            ]
        );

        $this->add_group_control(
            Group_Control_Typography::get_type(),
            [
                'name' => 'typography',
                'label' => esc_html__('Typography', 'bdthemes-element-pack'),
                'selector' => '{{WRAPPER}} .bdt-portfolio-carousel .bdt-gallery-item-link span, {{WRAPPER}} .bdt-portfolio-carousel .bdt-gallery-item-link i',
                // 'condition' => [
                //     'link_type' => 'text',
                // ],
            ]
        );

        $this->end_controls_tab();

        $this->start_controls_tab(
            'tab_button_hover',
            [
                'label' => esc_html__('Hover', 'bdthemes-element-pack'),
            ]
        );

        $this->add_control(
            'hover_color',
            [
                'label' => esc_html__('Color', 'bdthemes-element-pack'),
                'type' => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .bdt-portfolio-carousel .bdt-gallery-item-link:hover i' => 'color: {{VALUE}};',
                    '{{WRAPPER}} .bdt-portfolio-carousel .bdt-gallery-item-link:hover span' => 'color: {{VALUE}};',
                ],
            ]
        );

        $this->add_control(
            'background_hover_color',
            [
                'label' => esc_html__('Background Color', 'bdthemes-element-pack'),
                'type' => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .bdt-portfolio-carousel .bdt-gallery-item-link:hover, {{WRAPPER}} .bdt-portfolio-carousel.skin-abetis .bdt-gallery-item-link:before' => 'background-color: {{VALUE}};',
                ],
            ]
        );

        $this->add_control(
            'button_hover_border_color',
            [
                'label' => esc_html__('Border Color', 'bdthemes-element-pack'),
                'type' => Controls_Manager::COLOR,
                'condition' => [
                    'border_border!' => '',
                ],
                'selectors' => [
                    '{{WRAPPER}} .bdt-portfolio-carousel .bdt-gallery-item-link.bdt-link-icon:hover' => 'border-color: {{VALUE}};',
                ],
            ]
        );

        $this->end_controls_tab();

        $this->end_controls_tabs();

        $this->end_controls_section();

        $this->start_controls_section(
            'section_style_category',
            [
                'label' => esc_html__('Category', 'bdthemes-element-pack'),
                'tab' => Controls_Manager::TAB_STYLE,
                'condition' => [
                    'show_category' => 'yes',
                ]
            ]
        );

        $this->add_control(
            'category_color',
            [
                'label' => esc_html__('Category Color', 'bdthemes-element-pack'),
                'type' => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .bdt-portfolio-carousel .bdt-gallery-item-tags' => 'color: {{VALUE}};',
                ],
            ]
        );

        $this->add_control(
            'category_separator_color',
            [
                'label' => esc_html__('Separator Color', 'bdthemes-element-pack'),
                'type' => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .bdt-portfolio-carousel .bdt-gallery-item-tags .bdt-gallery-item-tag-separator' => 'border-color: {{VALUE}};',
                ],
            ]
        );

        $this->add_control(
            'category_background',
            [
                'label' => esc_html__('Background', 'bdthemes-element-pack'),
                'type' => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .bdt-portfolio-carousel .bdt-gallery-item-tags' => 'background-color: {{VALUE}};',
                ],
            ]
        );

        $this->add_group_control(
            Group_Control_Border::get_type(),
            [
                'name' => 'category_border',
                'label' => esc_html__('Border', 'bdthemes-element-pack'),
                'selector' => '{{WRAPPER}} .bdt-portfolio-carousel .bdt-gallery-item-tags',
                'separator' => 'before',
            ]
        );

        $this->add_responsive_control(
            'category_border_radius',
            [
                'label' => esc_html__('Border Radius', 'bdthemes-element-pack'),
                'type' => Controls_Manager::DIMENSIONS,
                'size_units' => ['px', '%'],
                'selectors' => [
                    '{{WRAPPER}} .bdt-portfolio-carousel .bdt-gallery-item-tags' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
                ],
            ]
        );

        $this->add_responsive_control(
            'category_padding',
            [
                'label' => esc_html__('Padding', 'bdthemes-element-pack'),
                'type' => Controls_Manager::DIMENSIONS,
                'size_units' => ['px', 'em', '%'],
                'selectors' => [
                    '{{WRAPPER}} .bdt-portfolio-carousel .bdt-gallery-item-tags' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
                ],
            ]
        );

        $this->add_group_control(
            Group_Control_Box_Shadow::get_type(),
            [
                'name' => 'category_box_shadow',
                'selector' => '{{WRAPPER}} .bdt-portfolio-carousel .bdt-gallery-item-tags',
            ]
        );

        $this->add_group_control(
            Group_Control_Typography::get_type(),
            [
                'name' => 'category_typography',
                'label' => esc_html__('Typography', 'bdthemes-element-pack'),
                'selector' => '{{WRAPPER}} .bdt-portfolio-carousel .bdt-gallery-item-tag',
            ]
        );

        $this->end_controls_section();

        //Navigation Style
        $this->start_controls_section(
            'section_style_navigation',
            [
                'label'      => esc_html__( 'Navigation', 'bdthemes-element-pack' ),
                'tab'        => Controls_Manager::TAB_STYLE,
                'conditions' => [
                    'relation' => 'or',
                    'terms'    => [
                        [
                            'name'     => 'navigation',
                            'operator' => '!=',
                            'value'    => 'none',
                        ],
                        [
                            'name'  => 'show_scrollbar',
                            'value' => 'yes',
                        ],
                    ],
                ],
            ]
        );

        //Global Navigation Style Controls
        $this->register_navigation_style_controls('swiper-carousel');

        $this->end_controls_section();
    }

    /**
     * Get post query builder arguments
     */
    public function query_posts($posts_per_page) {
        $settings = $this->get_settings();

        $args = [];
        if ($posts_per_page) {
            $args['posts_per_page'] = $posts_per_page;
            $args['paged']  = max(1, get_query_var('paged'), get_query_var('page'));
        }

        $default = $this->getGroupControlQueryArgs();
        $args = array_merge($default, $args);

        $this->_query = new \WP_Query($args);
    }

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

        $this->query_posts( $settings['posts_per_page'] );

        $wp_query = $this->get_query();
        if ( ! $wp_query->found_posts ) {
            return;
        }

        $this->render_header( 'default', $settings );

        while ( $wp_query->have_posts() ) {
            $wp_query->the_post();
            $this->render_post( $settings );
        }

        $this->render_footer();
        wp_reset_postdata();
    }

    public function render_thumbnail( $settings ) {
        $thumb_settings = $settings;
        $thumb_settings['thumbnail_size'] = [ 'id' => get_post_thumbnail_id() ];

        $thumbnail_html     = Group_Control_Image_Size::get_attachment_image_html( $thumb_settings, 'thumbnail_size' );
        $placeholder_img_src = Utils::get_placeholder_image_src();
        $thumb_size         = $thumb_settings['thumbnail_size_size'] ?? 'medium';

        if ( ! $thumbnail_html ) {
            printf(
                '<div class="bdt-gallery-thumbnail"><img src="%1$s" alt="%2$s"></div>',
                esc_url( $placeholder_img_src ),
                esc_attr( get_the_title() )
            );
        } else {
            echo '<div class="bdt-gallery-thumbnail">';
            echo wp_get_attachment_image(
                get_post_thumbnail_id(),
                $thumb_size,
                false,
                [ 'alt' => esc_attr( get_the_title() ) ]
            );
            echo '</div>';
        }
    }

    public function render_title( $settings ) {
        if ( empty( $settings['show_title'] ) ) {
            return;
        }

        $tag    = Utils::get_valid_html_tag( $settings['title_tag'] ?? 'h4' );
        $target = ! empty( $settings['external_link'] ) ? ' target="_blank"' : '';
        ?>
        <a href="<?php echo esc_url( get_the_permalink() ); ?>"<?php echo $target; ?>>
            <<?php echo esc_attr( $tag ); ?> class="bdt-gallery-item-title bdt-margin-remove">
                <?php echo esc_html( get_the_title() ); ?>
            </<?php echo esc_attr( $tag ); ?>>
        </a>
        <?php
    }

    public function render_excerpt( $settings ) {
        if ( empty( $settings['show_excerpt'] ) ) {
            return;
        }

        $strip_shortcode = ! empty( $settings['strip_shortcode'] );
        $ellipsis       = $settings['ellipsis'] ?? '';
        $excerpt_limit  = isset( $settings['excerpt_limit'] ) ? (int) $settings['excerpt_limit'] : 10;
        ?>
        <div class="bdt-portfolio-excerpt">
            <?php
            if ( has_excerpt() ) {
                the_excerpt();
            } else {
                echo wp_kses_post( element_pack_custom_excerpt( $excerpt_limit, $strip_shortcode, $ellipsis ) );
            }
            ?>
        </div>
        <?php
    }

    public function render_categories_names( $settings ) {
        if ( empty( $settings['show_category'] ) ) {
            return;
        }

        $this->add_render_attribute( 'portfolio-category', 'class', 'bdt-gallery-item-tags', true );

        global $post;

        $item_filters = get_the_terms( $post->ID, 'portfolio_filter' );
        if ( ! is_array( $item_filters ) ) {
            $item_filters = [];
        }

        $separator  = '<span class="bdt-gallery-item-tag-separator"></span>';
        $tags_array = [];
        foreach ( $item_filters as $item_filter ) {
            $tags_array[] = '<span class="bdt-gallery-item-tag">' . esc_html( $item_filter->name ) . '</span>';
        }
        ?>
        <div <?php $this->print_render_attribute_string( 'portfolio-category' ); ?>>
            <?php echo wp_kses_post( implode( $separator, $tags_array ) ); ?>
        </div>
        <?php
    }

    public function render_overlay( $settings ) {
        $this->add_render_attribute(
            [
                'content-position' => [
                    'class' => [ 'bdt-position-center' ],
                ],
            ],
            '',
            '',
            true
        );

        $placeholder_img_src = Utils::get_placeholder_image_src();
        $img_url             = wp_get_attachment_image_src( get_post_thumbnail_id(), 'full' );
        $img_url             = $img_url ? $img_url[0] : $placeholder_img_src;

        $link_type  = $settings['link_type'] ?? 'icon';
        $show_link  = $settings['show_link'] ?? 'none';
        $link_class = ( $link_type === 'icon' ) ? 'bdt-link-icon' : 'bdt-link-text';

        $this->add_render_attribute(
            [
                'lightbox-settings' => [
                    'class'                        => [ 'bdt-gallery-item-link', 'bdt-gallery-lightbox-item', $link_class ],
                    'data-elementor-open-lightbox' => 'no',
                    'data-caption'                 => wp_kses_post( get_the_title() ),
                    'href'                         => esc_url( $img_url ),
                ],
            ],
            '',
            '',
            true
        );
        ?>
        <div <?php $this->print_render_attribute_string( 'content-position' ); ?>>
            <div class="bdt-portfolio-content">
                <div class="bdt-gallery-content-inner">
                    <?php if ( $show_link !== 'none' ) : ?>
                        <div class="bdt-flex-inline bdt-gallery-item-link-wrapper">
                            <?php if ( $show_link === 'lightbox' || $show_link === 'both' ) : ?>
                                <a <?php $this->print_render_attribute_string( 'lightbox-settings' ); ?>>
                                    <?php if ( $link_type === 'icon' ) : ?>
                                        <i class="ep-icon-search" aria-hidden="true"></i>
                                    <?php elseif ( $link_type === 'text' && ! empty( $settings['lightbox_link_text'] ) ) : ?>
                                        <span><?php echo esc_html( $settings['lightbox_link_text'] ); ?></span>
                                    <?php endif; ?>
                                </a>
                            <?php endif; ?>

                            <?php if ( $show_link === 'post' || $show_link === 'both' ) : ?>
                                <?php $target = ! empty( $settings['external_link'] ) ? '_blank' : '_self'; ?>
                                <a class="bdt-gallery-item-link <?php echo esc_attr( $link_type === 'icon' ? 'bdt-link-icon' : 'bdt-link-text' ); ?>" href="<?php echo esc_url( get_permalink() ); ?>" target="<?php echo esc_attr( $target ); ?>">
                                    <?php if ( $link_type === 'icon' ) : ?>
                                        <i class="ep-icon-link" aria-hidden="true"></i>
                                    <?php elseif ( $link_type === 'text' && ! empty( $settings['post_link_text'] ) ) : ?>
                                        <span><?php echo esc_html( $settings['post_link_text'] ); ?></span>
                                    <?php endif; ?>
                                </a>
                            <?php endif; ?>
                        </div>
                    <?php endif; ?>
                </div>
            </div>
        </div>
        <?php
    }

    public function render_header( $skin, $settings ) {

        $this->render_swiper_header_attribute( 'portfolio-carousel' );

        $this->add_render_attribute( 'carousel', 'class', [ 'bdt-portfolio-carousel', 'skin-' . $skin ] );

        if ( $settings['match_height'] === 'yes' ) {
            $this->add_render_attribute( 'carousel', 'data-bdt-height-match', 'target: > div > div > .bdt-gallery-item' );
        }

        $show_link = $settings['show_link'];

        if ( $show_link === 'lightbox' || $show_link === 'both' ) {
            $anim = $settings['lightbox_animation'] ?? 'slide';

            $this->add_render_attribute( 'carousel', 'data-bdt-lightbox', 'toggle: .bdt-gallery-lightbox-item; animation:' . $anim . ';' );

            if ( ! empty( $settings['lightbox_autoplay'] ) ) {
                $this->add_render_attribute( 'carousel', 'data-bdt-lightbox', 'autoplay: 500;' );
                if ( ! empty( $settings['lightbox_pause'] ) ) {
                    $this->add_render_attribute( 'carousel', 'data-bdt-lightbox', 'pause-on-hover: true;' );
                }
            }
        }
        ?>
        <div <?php $this->print_render_attribute_string( 'carousel' ); ?>>
            <div <?php $this->print_render_attribute_string( 'swiper' ); ?>>
                <div class="swiper-wrapper">
        <?php
    }

    public function render_desc( $settings ) {
        ?>
        <div class="bdt-portfolio-desc">
            <?php
            $this->render_title( $settings );
            $this->render_excerpt( $settings );
            ?>
        </div>
        <?php
    }

    public function render_post( $settings ) {
        $this->add_render_attribute( 'portfolio-item-inner', 'class', 'bdt-portfolio-inner', true );
        $this->add_render_attribute( 'portfolio-item', 'class', 'swiper-slide bdt-gallery-item bdt-transition-toggle', true );
        ?>
        <div <?php $this->print_render_attribute_string( 'portfolio-item' ); ?>>
            <div <?php $this->print_render_attribute_string( 'portfolio-item-inner' ); ?>>
                <div class="bdt-portfolio-content-inner">
                    <?php
                    $this->render_thumbnail( $settings );
                    $this->render_overlay( $settings );
                    ?>
                </div>
                <?php $this->render_desc( $settings ); ?>
                <?php $this->render_categories_names( $settings ); ?>
            </div>
        </div>
        <?php
    }
}
