<?php

namespace ElementPack\Modules\FooeventsCalendarCarousel\Widgets;

use ElementPack\Base\Module_Base;
use ElementPack\Traits\Global_Swiper_Controls;

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

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

class Fooevents_Calendar_Carousel extends Module_Base {

	use Global_Swiper_Controls;

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

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

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

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

	public function get_keywords() {
		return [ 'fooevents', 'calendar', 'carousel', 'slider' ];
	}

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

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

	public function get_custom_help_url() {
		return 'https://youtu.be/2J4XhOe8J0o';
	}

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

	protected function register_controls() {

		/* -----------------------------------------
			Layout Settings
		   ----------------------------------------- */
		$this->start_controls_section(
			'section_layout',
			[
				'label' => esc_html__( 'Layout', 'bdthemes-element-pack' ),
			]
		);

		$this->register_carousel_column_gap_controls();

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

		$this->add_group_control(
			Group_Control_Image_Size::get_type(),
			[
				'name'      => 'image_size',
				'label'     => esc_html__( 'Image Size', 'bdthemes-element-pack' ),
				'exclude'   => [ 'custom' ],
				'default'   => 'medium_large',
				'condition' => [ 'show_image' => 'yes' ],
			]
		);

		$this->add_control(
			'fields_separator_1',
			[
				'type' => Controls_Manager::DIVIDER,
			]
		);

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

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

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

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

		$this->add_control(
			'fields_separator_2',
			[
				'type' => Controls_Manager::DIVIDER,
			]
		);

		$this->add_control(
			'time_as_tooltip',
			[
				'label'       => esc_html__( 'Time as Tooltip', 'bdthemes-element-pack' ),
				'description' => esc_html__( 'Show time as a tooltip on the date badge instead of displaying it inline.', 'bdthemes-element-pack' ),
				'type'        => Controls_Manager::SWITCHER,
				'default'     => 'yes',
				'condition'   => [
					'show_date' => 'yes',
					'show_time' => 'yes',
				],
			]
		);

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

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

		$this->add_control(
			'excerpt_length',
			[
				'label'     => esc_html__( 'Excerpt Length', 'bdthemes-element-pack' ),
				'type'      => Controls_Manager::NUMBER,
				'default'   => 15,
				'condition' => [ 'show_excerpt' => 'yes' ],
			]
		);

		$this->add_control(
			'fields_separator_3',
			[
				'type' => Controls_Manager::DIVIDER,
			]
		);

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

		$this->add_control(
			'read_more_text',
			[
				'label'     => esc_html__( 'Read More Text', 'bdthemes-element-pack' ),
				'type'      => Controls_Manager::TEXT,
				'dynamic'   => [ 'active' => true ],
				'default'   => esc_html__( 'Read More', 'bdthemes-element-pack' ),
				'condition' => [ 'show_read_more' => 'yes' ],
			]
		);

		$this->end_controls_section();

		/* -----------------------------------------
			Query Settings
		   ----------------------------------------- */
		$this->start_controls_section(
			'section_query',
			[
				'label' => esc_html__( 'Query', 'bdthemes-element-pack' ),
			]
		);

		$this->add_control(
			'post_type',
			[
				'label'       => esc_html__( 'Post Type', 'bdthemes-element-pack' ),
				'type'        => Controls_Manager::SELECT,
				'default'     => 'post',
				'options'     => $this->get_fooevents_post_types(),
				'description' => esc_html__( 'Select the post type where your FooEvents events are stored.', 'bdthemes-element-pack' ),
			]
		);

		$this->add_control(
			'posts_per_page',
			[
				'label'   => esc_html__( 'Limit', 'bdthemes-element-pack' ),
				'type'    => Controls_Manager::NUMBER,
				'default' => 6,
				'min'     => 1,
				'max'     => 100,
			]
		);

		$this->add_control(
			'fields_separator_4',
			[
				'type' => Controls_Manager::DIVIDER,
			]
		);

		$this->add_control(
			'orderby',
			[
				'label'   => esc_html__( 'Order By', 'bdthemes-element-pack' ),
				'type'    => Controls_Manager::SELECT,
				'default' => 'meta_value',
				'options' => [
					'title'      => esc_html__( 'Title', 'bdthemes-element-pack' ),
					'date'       => esc_html__( 'Post Date', 'bdthemes-element-pack' ),
					'meta_value' => esc_html__( 'Event Date', 'bdthemes-element-pack' ),
					'rand'       => esc_html__( 'Random', 'bdthemes-element-pack' ),
					'modified'   => esc_html__( 'Last Modified', 'bdthemes-element-pack' ),
				],
			]
		);

		$this->add_control(
			'order',
			[
				'label'   => esc_html__( 'Order', 'bdthemes-element-pack' ),
				'type'    => Controls_Manager::SELECT,
				'default' => 'ASC',
				'options' => [
					'ASC'  => esc_html__( 'ASC', 'bdthemes-element-pack' ),
					'DESC' => esc_html__( 'DESC', 'bdthemes-element-pack' ),
				],
			]
		);

		$this->add_control(
			'fields_separator_5',
			[
				'type' => Controls_Manager::DIVIDER,
			]
		);

		$this->add_control(
			'offset',
			[
				'label'   => esc_html__( 'Offset', 'bdthemes-element-pack' ),
				'type'    => Controls_Manager::NUMBER,
				'default' => 0,
				'min'     => 0,
			]
		);

		$this->add_control(
			'show_upcoming_only',
			[
				'label'       => esc_html__( 'Upcoming Events Only', 'bdthemes-element-pack' ),
				'description' => esc_html__( 'Only show events with a date equal to or after today.', 'bdthemes-element-pack' ),
				'type'        => Controls_Manager::SWITCHER,
				'default'     => '',
			]
		);

		$this->add_control(
			'fields_separator_6',
			[
				'type' => Controls_Manager::DIVIDER,
			]
		);

		$this->add_control(
			'include_categories',
			[
				'label'       => esc_html__( 'Filter by Category', 'bdthemes-element-pack' ),
				'type'        => Controls_Manager::TEXT,
				'label_block' => true,
				'placeholder' => esc_html__( 'featured, workshop', 'bdthemes-element-pack' ),
				'description' => esc_html__( 'Enter category slugs separated by commas.', 'bdthemes-element-pack' ),
			]
		);

		$this->add_control(
			'include_ids',
			[
				'label'       => esc_html__( 'Include Post IDs', 'bdthemes-element-pack' ),
				'type'        => Controls_Manager::TEXT,
				'label_block' => true,
				'placeholder' => esc_html__( '123, 456', 'bdthemes-element-pack' ),
				'description' => esc_html__( 'Enter post IDs to include only these (overrides other query settings when set).', 'bdthemes-element-pack' ),
			]
		);

		$this->add_control(
			'exclude_ids',
			[
				'label'       => esc_html__( 'Exclude Post IDs', 'bdthemes-element-pack' ),
				'type'        => Controls_Manager::TEXT,
				'label_block' => true,
				'placeholder' => esc_html__( '123, 456', 'bdthemes-element-pack' ),
				'description' => esc_html__( 'Enter post IDs to exclude, separated by commas.', 'bdthemes-element-pack' ),
			]
		);

		$this->end_controls_section();

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

		$this->register_navigation_controls();

		$this->end_controls_section();

		/* -----------------------------------------
			Carousel Settings
		   ----------------------------------------- */
		$this->register_carousel_settings_controls();

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

		$this->add_group_control(
			Group_Control_Background::get_type(),
			[
				'name'     => 'item_background',
				'types'    => [ 'classic', 'gradient' ],
				'selector' => '{{WRAPPER}} .ep-fooevents-calendar-carousel-item',
			]
		);

		$this->add_group_control(
			Group_Control_Border::get_type(),
			[
				'name'     => 'item_border',
				'selector' => '{{WRAPPER}} .ep-fooevents-calendar-carousel-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}} .ep-fooevents-calendar-carousel-item' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				],
			]
		);

		$this->add_group_control(
			Group_Control_Box_Shadow::get_type(),
			[
				'name'     => 'item_box_shadow',
				'selector' => '{{WRAPPER}} .ep-fooevents-calendar-carousel-item',
			]
		);

		$this->add_responsive_control(
			'item_padding',
			[
				'label'      => esc_html__( 'Content Padding', 'bdthemes-element-pack' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => [ 'px', '%', 'em', 'rem' ],
				'selectors'  => [
					'{{WRAPPER}} .ep-fooevents-calendar-carousel-item-content' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				],
			]
		);

		$this->end_controls_section();

		/* -----------------------------------------
			Image Style
		   ----------------------------------------- */
		$this->start_controls_section(
			'section_style_image',
			[
				'label'     => esc_html__( 'Image', 'bdthemes-element-pack' ),
				'tab'       => Controls_Manager::TAB_STYLE,
				'condition' => [ 'show_image' => 'yes' ],
			]
		);

		$this->add_responsive_control(
			'image_height',
			[
				'label'      => esc_html__( 'Image Height', 'bdthemes-element-pack' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => [ 'px', 'vh' ],
				'range'      => [
					'px' => [ 'min' => 50, 'max' => 600 ],
					'vh' => [ 'min' => 10, 'max' => 100 ],
				],
				'selectors'  => [
					'{{WRAPPER}} .ep-fooevents-calendar-carousel-image' => 'height: {{SIZE}}{{UNIT}};',
				],
			]
		);

		$this->add_responsive_control(
			'image_object_fit',
			[
				'label' => esc_html__( 'Object Fit', 'bdthemes-element-pack' ),
				'type'  => Controls_Manager::SELECT,
				'options' => [
					''           => esc_html__( 'Default', 'bdthemes-element-pack' ),
					'fill'       => esc_html__( 'Fill', 'bdthemes-element-pack' ),
					'cover'      => esc_html__( 'Cover', 'bdthemes-element-pack' ),
					'contain'    => esc_html__( 'Contain', 'bdthemes-element-pack' ),
					'scale-down' => esc_html__( 'Scale Down', 'bdthemes-element-pack' ),
				],
				'condition' => [
					'image_height[size]!' => '',
				],
				'selectors' => [
					'{{WRAPPER}} .ep-fooevents-calendar-carousel-image img' => 'object-fit: {{VALUE}};',
				],
			]
		);
		
		$this->add_responsive_control(
			'image_object_position',
			[
				'label'   => esc_html__( 'Object Position', 'bdthemes-element-pack' ),
				'type'    => Controls_Manager::SELECT,
				'options' => [
					'center center'  => esc_html__( 'Center Center', 'bdthemes-element-pack' ),
					'center left'    => esc_html__( 'Center Left', 'bdthemes-element-pack' ),
					'center right'   => esc_html__( 'Center Right', 'bdthemes-element-pack' ),
					'top center'     => esc_html__( 'Top Center', 'bdthemes-element-pack' ),
					'top left'       => esc_html__( 'Top Left', 'bdthemes-element-pack' ),
					'top right'      => esc_html__( 'Top Right', 'bdthemes-element-pack' ),
					'bottom center'  => esc_html__( 'Bottom Center', 'bdthemes-element-pack' ),
					'bottom left'    => esc_html__( 'Bottom Left', 'bdthemes-element-pack' ),
					'bottom right'   => esc_html__( 'Bottom Right', 'bdthemes-element-pack' ),
				],
				'default' => 'center center',
				'selectors' => [
					'{{WRAPPER}} .ep-fooevents-calendar-carousel-image img' => 'object-position: {{VALUE}};',
				],
				'condition' => [
					'image_height[size]!' => '',
					'image_object_fit'    => [ 'cover', 'contain', 'scale-down' ],
				],
			]
		);

		$this->add_responsive_control(
			'image_border_radius',
			[
				'label'      => esc_html__( 'Border Radius', 'bdthemes-element-pack' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => [ 'px', '%' ],
				'selectors'  => [
					'{{WRAPPER}} .ep-fooevents-calendar-carousel-image, {{WRAPPER}} .ep-fooevents-calendar-carousel-image img' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				],
			]
		);

		$this->add_group_control(
			Group_Control_Css_Filter::get_type(),
			[
				'name'     => 'image_css_filters',
				'selector' => '{{WRAPPER}} .ep-fooevents-calendar-carousel-image img',
			]
		);

		$this->add_group_control(
			Group_Control_Css_Filter::get_type(),
			[
				'name'     => 'image_hover_css_filters',
				'label'    => esc_html__( 'Hover CSS Filters', 'bdthemes-element-pack' ),
				'selector' => '{{WRAPPER}} .ep-fooevents-calendar-carousel-item:hover .ep-fooevents-calendar-carousel-image img',
			]
		);

		$this->end_controls_section();

		/* -----------------------------------------
			Date Badge Style
		   ----------------------------------------- */
		$this->start_controls_section(
			'section_style_date',
			[
				'label'     => esc_html__( 'Date Badge', 'bdthemes-element-pack' ),
				'tab'       => Controls_Manager::TAB_STYLE,
				'condition' => [ 'show_date' => 'yes' ],
			]
		);

		$this->add_control(
			'date_color',
			[
				'label'     => esc_html__( 'Color', 'bdthemes-element-pack' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => [
					'{{WRAPPER}} .ep-fooevents-calendar-carousel-date-badge' => 'color: {{VALUE}};',
				],
			]
		);

		$this->add_control(
			'date_day_color',
			[
				'label'     => esc_html__( 'Day Number Color', 'bdthemes-element-pack' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => [
					'{{WRAPPER}} .ep-fooevents-calendar-carousel-date-badge .ep-day' => 'color: {{VALUE}};',
				],
			]
		);

		$this->add_group_control(
			Group_Control_Background::get_type(),
			[
				'name'     => 'date_background',
				'label'    => esc_html__( 'Background', 'bdthemes-element-pack' ),
				'types'    => [ 'gradient' ],
				'selector' => '{{WRAPPER}} .ep-fooevents-calendar-carousel-date-badge',
			]
		);

		$this->add_group_control(
			Group_Control_Typography::get_type(),
			[
				'name'     => 'date_typography',
				'selector' => '{{WRAPPER}} .ep-fooevents-calendar-carousel-date-badge',
			]
		);

		$this->end_controls_section();

		/* -----------------------------------------
			Title Style
		   ----------------------------------------- */
		$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}} .ep-fooevents-calendar-carousel-title a' => 'color: {{VALUE}};',
				],
			]
		);

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

		$this->add_group_control(
			Group_Control_Typography::get_type(),
			[
				'name'     => 'title_typography',
				'selector' => '{{WRAPPER}} .ep-fooevents-calendar-carousel-title',
			]
		);

		$this->end_controls_section();

		/* -----------------------------------------
			Time Style (only when time is NOT shown as tooltip)
		   ----------------------------------------- */
		$this->start_controls_section(
			'section_style_time',
			[
				'label'     => esc_html__( 'Time', 'bdthemes-element-pack' ),
				'tab'       => Controls_Manager::TAB_STYLE,
				'condition' => [
					'show_time'      => 'yes',
					'time_as_tooltip' => '',
				],
			]
		);

		$this->add_control(
			'time_color',
			[
				'label'     => esc_html__( 'Color', 'bdthemes-element-pack' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => [
					'{{WRAPPER}} .ep-fooevents-calendar-carousel-time' => 'color: {{VALUE}};',
				],
			]
		);

		$this->add_control(
			'time_icon_color',
			[
				'label'     => esc_html__( 'Icon Color', 'bdthemes-element-pack' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => [
					'{{WRAPPER}} .ep-fooevents-calendar-carousel-time .ep-icon' => 'color: {{VALUE}};',
				],
			]
		);

		$this->add_group_control(
			Group_Control_Typography::get_type(),
			[
				'name'     => 'time_typography',
				'selector' => '{{WRAPPER}} .ep-fooevents-calendar-carousel-time',
			]
		);

		$this->end_controls_section();

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

		$this->start_controls_tabs( 'tabs_category_style' );

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

		$this->add_control(
			'category_color',
			[
				'label'     => esc_html__( 'Color', 'bdthemes-element-pack' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => [
					'{{WRAPPER}} .ep-fooevents-calendar-carousel-category, {{WRAPPER}} .ep-fooevents-calendar-carousel-category-link' => 'color: {{VALUE}};',
				],
			]
		);

		$this->add_group_control(
			Group_Control_Background::get_type(),
			[
				'name'      => 'category_background',
				'label'     => esc_html__( 'Background', 'bdthemes-element-pack' ),
				'types'     => [ 'classic', 'gradient' ],
				'selector'  => '{{WRAPPER}} .ep-fooevents-calendar-carousel-category .ep-fooevents-calendar-carousel-category-link',
				'fields_options' => [
					'background' => [
						'label' 	=> esc_html__( 'Background Color', 'bdthemes-element-pack' ),
					],
				],
			]
		);

		$this->add_group_control(
			Group_Control_Border::get_type(),
			[
				'name'     => 'category_border',
				'selector' => '{{WRAPPER}} .ep-fooevents-calendar-carousel-category .ep-fooevents-calendar-carousel-category-link',
			]
		);

		$this->add_responsive_control(
			'category_border_radius',
			[
				'label'      => esc_html__( 'Border Radius', 'bdthemes-element-pack' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => [ 'px', '%', 'em' ],
				'selectors'  => [
					'{{WRAPPER}} .ep-fooevents-calendar-carousel-category .ep-fooevents-calendar-carousel-category-link' => '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}} .ep-fooevents-calendar-carousel-category .ep-fooevents-calendar-carousel-category-link' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				],
			]
		);

		$this->add_responsive_control(
			'category_spacing',
			[
				'label'      => esc_html__( 'Spacing', 'bdthemes-element-pack' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => [ 'px' ],
				'selectors'  => [
					'{{WRAPPER}} .ep-fooevents-calendar-carousel-category' => 'gap: {{SIZE}}{{UNIT}};',
				],
			]
		);

		$this->add_group_control(
			Group_Control_Typography::get_type(),
			[
				'name'     => 'category_typography',
				'selector' => '{{WRAPPER}} .ep-fooevents-calendar-carousel-category .ep-fooevents-calendar-carousel-category-link',
			]
		);

		$this->end_controls_tab();

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

		$this->add_control(
			'category_hover_color',
			[
				'label'     => esc_html__( 'Color', 'bdthemes-element-pack' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => [
					'{{WRAPPER}} .ep-fooevents-calendar-carousel-category .ep-fooevents-calendar-carousel-category-link:hover' => 'color: {{VALUE}};',
				],
			]
		);

		$this->add_group_control(
			Group_Control_Background::get_type(),
			[
				'name'      => 'category_hover_background',
				'label'     => esc_html__( 'Background', 'bdthemes-element-pack' ),
				'types'     => [ 'classic', 'gradient' ],
				'selector'  => '{{WRAPPER}} .ep-fooevents-calendar-carousel-category .ep-fooevents-calendar-carousel-category-link:hover',
				'fields_options' => [
					'background' => [
						'label' 	=> esc_html__( 'Background Color', 'bdthemes-element-pack' ),
					],
				],
			]
		);

		$this->add_control(
			'category_hover_border_color',
			[
				'label'     => esc_html__( 'Border Color', 'bdthemes-element-pack' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => [
					'{{WRAPPER}} .ep-fooevents-calendar-carousel-category .ep-fooevents-calendar-carousel-category-link:hover' => 'border-color: {{VALUE}};',
				],
				'condition' => [
					'category_border_border!' => '',
				],
			]
		);

		$this->end_controls_tab();

		$this->end_controls_tabs();

		$this->end_controls_section();

		/* -----------------------------------------
			Location Style
		   ----------------------------------------- */
		$this->start_controls_section(
			'section_style_location',
			[
				'label'     => esc_html__( 'Location', 'bdthemes-element-pack' ),
				'tab'       => Controls_Manager::TAB_STYLE,
				'condition' => [ 'show_location' => 'yes' ],
			]
		);

		$this->add_control(
			'location_color',
			[
				'label'     => esc_html__( 'Color', 'bdthemes-element-pack' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => [
					'{{WRAPPER}} .ep-fooevents-calendar-carousel-location' => 'color: {{VALUE}};',
				],
			]
		);

		$this->add_group_control(
			Group_Control_Typography::get_type(),
			[
				'name'     => 'location_typography',
				'selector' => '{{WRAPPER}} .ep-fooevents-calendar-carousel-location',
			]
		);

		$this->end_controls_section();

		/* -----------------------------------------
			Excerpt Style
		   ----------------------------------------- */
		$this->start_controls_section(
			'section_style_excerpt',
			[
				'label'     => esc_html__( 'Excerpt', '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}} .ep-fooevents-calendar-carousel-excerpt' => 'color: {{VALUE}};',
				],
			]
		);

		$this->add_group_control(
			Group_Control_Typography::get_type(),
			[
				'name'     => 'excerpt_typography',
				'selector' => '{{WRAPPER}} .ep-fooevents-calendar-carousel-excerpt',
			]
		);

		$this->end_controls_section();

		/* -----------------------------------------
			Read More Button Style
		   ----------------------------------------- */
		$this->start_controls_section(
			'section_style_read_more',
			[
				'label'     => esc_html__( 'Read More Button', 'bdthemes-element-pack' ),
				'tab'       => Controls_Manager::TAB_STYLE,
				'condition' => [ 'show_read_more' => 'yes' ],
			]
		);

		$this->start_controls_tabs( 'tabs_read_more_style' );

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

		$this->add_control(
			'read_more_color',
			[
				'label'     => esc_html__( 'Color', 'bdthemes-element-pack' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => [
					'{{WRAPPER}} .ep-fooevents-calendar-carousel-read-more' => 'color: {{VALUE}};',
				],
			]
		);

		$this->add_control(
			'read_more_background',
			[
				'label'     => esc_html__( 'Background Color', 'bdthemes-element-pack' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => [
					'{{WRAPPER}} .ep-fooevents-calendar-carousel-read-more' => 'background-color: {{VALUE}};',
				],
			]
		);

		$this->add_group_control(
			Group_Control_Border::get_type(),
			[
				'name'     => 'read_more_border',
				'selector' => '{{WRAPPER}} .ep-fooevents-calendar-carousel-read-more',
			]
		);

		$this->end_controls_tab();

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

		$this->add_control(
			'read_more_hover_color',
			[
				'label'     => esc_html__( 'Color', 'bdthemes-element-pack' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => [
					'{{WRAPPER}} .ep-fooevents-calendar-carousel-read-more:hover' => 'color: {{VALUE}};',
				],
			]
		);

		$this->add_control(
			'read_more_hover_background',
			[
				'label'     => esc_html__( 'Background Color', 'bdthemes-element-pack' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => [
					'{{WRAPPER}} .ep-fooevents-calendar-carousel-read-more:hover' => 'background-color: {{VALUE}};',
				],
			]
		);

		$this->add_control(
			'read_more_hover_border_color',
			[
				'label'     => esc_html__( 'Border Color', 'bdthemes-element-pack' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => [
					'{{WRAPPER}} .ep-fooevents-calendar-carousel-read-more:hover' => 'border-color: {{VALUE}};',
				],
				'condition' => [
					'read_more_border_border!' => '',
				],
			]
		);

		$this->end_controls_tab();

		$this->end_controls_tabs();

		$this->add_responsive_control(
			'read_more_border_radius',
			[
				'label'      => esc_html__( 'Border Radius', 'bdthemes-element-pack' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => [ 'px', '%' ],
				'selectors'  => [
					'{{WRAPPER}} .ep-fooevents-calendar-carousel-read-more' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				],
				'separator'  => 'before',
			]
		);

		$this->add_responsive_control(
			'read_more_padding',
			[
				'label'      => esc_html__( 'Padding', 'bdthemes-element-pack' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => [ 'px', '%', 'em' ],
				'selectors'  => [
					'{{WRAPPER}} .ep-fooevents-calendar-carousel-read-more' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				],
			]
		);

		$this->add_group_control(
			Group_Control_Typography::get_type(),
			[
				'name'     => 'read_more_typography',
				'selector' => '{{WRAPPER}} .ep-fooevents-calendar-carousel-read-more',
			]
		);

		$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',
						],
					],
				],
			]
		);

		$this->register_navigation_style_controls( 'swiper-carousel' );

		$this->end_controls_section();
	}

	/* -----------------------------------------
		Helpers
	   ----------------------------------------- */

	protected function get_fooevents_post_types() {
		$options = [];

		if ( post_type_exists( 'product' ) ) {
			$options['product'] = esc_html__( 'WC Events (product)', 'bdthemes-element-pack' );
		}

		if ( post_type_exists( 'fe_eventbrite_event' ) ) {
			$options['fe_eventbrite_event'] = esc_html__( 'Eventbrite Events (fe_eventbrite_event)', 'bdthemes-element-pack' );
		}

		$associated = get_option( 'globalFooEventsCalendarPostTypes', [] );
		if ( ! is_array( $associated ) ) {
			$associated = [];
		}

		foreach ( $associated as $slug ) {
			$slug = sanitize_key( $slug );
			if ( isset( $options[ $slug ] ) ) {
				continue;
			}
			$obj = get_post_type_object( $slug );
			if ( $obj ) {
				$options[ $slug ] = $obj->labels->singular_name . ' (' . $slug . ')';
			}
		}

		if ( empty( $options ) || ( count( $options ) === 1 && isset( $options['product'] ) ) ) {
			foreach ( [ 'post', 'page' ] as $slug ) {
				if ( ! isset( $options[ $slug ] ) ) {
					$obj = get_post_type_object( $slug );
					if ( $obj ) {
						$options[ $slug ] = $obj->labels->singular_name . ' (' . $slug . ')';
					}
				}
			}
		}

		return $options;
	}

	protected function get_events_query() {
		$settings  = $this->get_settings_for_display();
		$post_type = ! empty( $settings['post_type'] ) ? sanitize_key( $settings['post_type'] ) : 'product';
		$taxonomy  = function_exists( 'element_pack_get_taxonomy_by_post_type' ) ? element_pack_get_taxonomy_by_post_type( $post_type ) : 'product_cat';

		$meta_query = [
			'relation' => 'AND',
			[
				'key'     => 'WooCommerceEventsEvent',
				'value'   => 'Event',
				'compare' => '=',
			],
		];

		if ( 'yes' === $settings['show_upcoming_only'] ) {
			$meta_query[] = [
				'key'     => 'WooCommerceEventsDate',
				'value'   => gmdate( 'Y-m-d' ),
				'compare' => '>=',
				'type'    => 'DATE',
			];
		}

		$args = [
			'post_type'      => $post_type,
			'posts_per_page' => ! empty( $settings['posts_per_page'] ) ? (int) $settings['posts_per_page'] : 6,
			'offset'         => ! empty( $settings['offset'] ) ? (int) $settings['offset'] : 0,
			'post_status'    => 'publish',
			'order'          => $settings['order'],
			'meta_query'     => $meta_query, // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query
		];

		if ( 'meta_value' === $settings['orderby'] ) {
			$args['orderby']  = 'meta_value';
			$args['meta_key'] = 'WooCommerceEventsDate'; // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key
		} else {
			$args['orderby'] = $settings['orderby'];
		}

		if ( ! empty( $settings['include_categories'] ) ) {
			$cats = array_filter( array_map( 'sanitize_title', explode( ',', $settings['include_categories'] ) ) );
			if ( ! empty( $cats ) ) {
				$args['tax_query'] = [ // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_tax_query
					[
						'taxonomy' => $taxonomy,
						'field'    => 'slug',
						'terms'    => $cats,
					],
				];
			}
		}

		if ( ! empty( $settings['include_ids'] ) ) {
			$ids = array_filter( array_map( 'intval', explode( ',', $settings['include_ids'] ) ) );
			if ( ! empty( $ids ) ) {
				$args['post__in'] = $ids;
				$args['orderby']  = 'post__in';
				unset( $args['meta_key'] );
			}
		}

		if ( ! empty( $settings['exclude_ids'] ) ) {
			$ids = array_filter( array_map( 'intval', explode( ',', $settings['exclude_ids'] ) ) );
			if ( ! empty( $ids ) ) {
				$args['post__not_in'] = $ids;
			}
		}

		return new \WP_Query( $args );
	}

	protected function render_event_date( $post_id, $tooltip = '' ) {
		$date = get_post_meta( $post_id, 'WooCommerceEventsDate', true );
		if ( empty( $date ) ) {
			return;
		}
		$timestamp = strtotime( $date );
		if ( ! $timestamp ) {
			return;
		}
		$day   = date_i18n( 'd', $timestamp );
		$month = date_i18n( 'M', $timestamp );
		$tooltip_attr = ! empty( $tooltip )
			? ' bdt-tooltip="title: ' . esc_attr( '🕐 ' . $tooltip ) . '; pos: right"'
			: '';
		?>
		<div class="ep-fooevents-calendar-carousel-date-badge"<?php echo $tooltip_attr; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>>
			<span class="ep-day"><?php echo esc_html( $day ); ?></span>
			<span class="ep-month"><?php echo esc_html( $month ); ?></span>
		</div>
		<?php
	}

	protected function get_event_time_string( $post_id ) {
		$hour    = get_post_meta( $post_id, 'WooCommerceEventsHour', true );
		$minutes = get_post_meta( $post_id, 'WooCommerceEventsMinutes', true );
		$period  = get_post_meta( $post_id, 'WooCommerceEventsPeriod', true );

		if ( empty( $hour ) ) {
			return '';
		}
		$time = $hour . ':' . $minutes;
		if ( ! empty( $period ) ) {
			$time .= ' ' . strtoupper( $period );
		}
		$hour_end    = get_post_meta( $post_id, 'WooCommerceEventsHourEnd', true );
		$minutes_end = get_post_meta( $post_id, 'WooCommerceEventsMinutesEnd', true );
		$period_end  = get_post_meta( $post_id, 'WooCommerceEventsEndPeriod', true );
		if ( ! empty( $hour_end ) ) {
			$time .= ' - ' . $hour_end . ':' . $minutes_end;
			if ( ! empty( $period_end ) ) {
				$time .= ' ' . strtoupper( $period_end );
			}
		}
		return $time;
	}

	protected function render_event_time( $post_id ) {
		$time_str = $this->get_event_time_string( $post_id );
		if ( empty( $time_str ) ) {
			return;
		}
		?>
		<div class="ep-fooevents-calendar-carousel-time">
			<i class="far fa-clock ep-icon" aria-hidden="true"></i>
			<?php echo esc_html( $time_str ); ?>
		</div>
		<?php
	}

	protected function render_event_location( $post_id ) {
		$location = get_post_meta( $post_id, 'WooCommerceEventsLocation', true );
		if ( empty( $location ) ) {
			return;
		}
		?>
		<div class="ep-fooevents-calendar-carousel-location">
			<i class="fas fa-map-marker-alt ep-icon" aria-hidden="true"></i>
			<?php echo esc_html( $location ); ?>
		</div>
		<?php
	}

	protected function render_event_category( $post_id ) {
		$taxonomy   = function_exists( 'element_pack_get_taxonomy_by_post_type' ) ? element_pack_get_taxonomy_by_post_type( get_post_type( $post_id ) ) : 'product_cat';
		$categories = get_the_terms( $post_id, $taxonomy );

		if ( empty( $categories ) || is_wp_error( $categories ) ) {
			return;
		}
		$cat_links = [];
		foreach ( $categories as $category ) {
			$term_link = get_term_link( $category );
			if ( is_wp_error( $term_link ) ) {
				continue;
			}
			$cat_links[] = sprintf(
				'<a href="%s" class="ep-fooevents-calendar-carousel-category-link">%s</a>',
				esc_url( $term_link ),
				esc_html( $category->name )
			);
		}
		if ( empty( $cat_links ) ) {
			return;
		}
		?>
		<div class="ep-fooevents-calendar-carousel-category">
			<?php echo wp_kses_post( implode( '', $cat_links ) ); ?>
		</div>
		<?php
	}

	protected function render_single_item( $post, array $settings ) {
		$post_id    = $post->ID;
		$post_link  = get_permalink( $post_id );
		$post_title = get_the_title( $post_id );

		$has_image       = has_post_thumbnail( $post_id );
		$show_image      = 'yes' === $settings['show_image'];
		$show_date       = 'yes' === $settings['show_date'];
		$show_time       = 'yes' === $settings['show_time'];
		$time_as_tooltip = ! empty( $settings['time_as_tooltip'] ) && 'yes' === $settings['time_as_tooltip'];
		$date_on_image   = $show_image && $has_image;

		$time_tooltip = ( $show_date && $show_time && $time_as_tooltip )
			? $this->get_event_time_string( $post_id )
			: '';

		$image_size = ! empty( $settings['image_size_size'] ) ? $settings['image_size_size'] : 'medium_large';
		?>
		<div class="swiper-slide">
			<div class="ep-fooevents-calendar-carousel-item">

				<?php if ( $show_image && $has_image ) : ?>
					<div class="ep-fooevents-calendar-carousel-image">
						<a href="<?php echo esc_url( $post_link ); ?>" aria-label="<?php echo esc_attr( $post_title ); ?>">
							<?php echo get_the_post_thumbnail( $post_id, $image_size ); ?>
						</a>

						<?php if ( $show_date || $show_time ) : ?>
							<div class="ep-fooevents-calendar-carousel-image-meta">
								<?php if ( $show_date ) : ?>
									<?php $this->render_event_date( $post_id, $time_tooltip ); ?>
								<?php endif; ?>
								<?php if ( $show_time && ! $time_as_tooltip ) : ?>
									<?php $this->render_event_time( $post_id ); ?>
								<?php endif; ?>
							</div>
						<?php endif; ?>
					</div>
				<?php endif; ?>

				<div class="ep-fooevents-calendar-carousel-item-content">

					<?php if ( ! $date_on_image && ( $show_date || $show_time ) ) : ?>
						<div class="ep-fooevents-calendar-carousel-meta-row">
							<?php if ( $show_date ) : ?>
								<?php $this->render_event_date( $post_id, $time_tooltip ); ?>
							<?php endif; ?>
							<?php if ( $show_time && ! $time_as_tooltip ) : ?>
								<div class="ep-fooevents-calendar-carousel-meta-details">
									<?php $this->render_event_time( $post_id ); ?>
								</div>
							<?php endif; ?>
						</div>
					<?php endif; ?>

					<?php if ( 'yes' === $settings['show_category'] || 'yes' === $settings['show_location'] ) : ?>
						<div class="ep-fooevents-calendar-carousel-meta-details">
							<?php if ( 'yes' === $settings['show_category'] ) : ?>
								<?php $this->render_event_category( $post_id ); ?>
							<?php endif; ?>
							<?php if ( 'yes' === $settings['show_location'] ) : ?>
								<?php $this->render_event_location( $post_id ); ?>
							<?php endif; ?>
						</div>
					<?php endif; ?>

					<?php if ( 'yes' === $settings['show_title'] ) : ?>
						<h3 class="ep-fooevents-calendar-carousel-title">
							<a href="<?php echo esc_url( $post_link ); ?>">
								<?php echo esc_html( $post_title ); ?>
							</a>
						</h3>
					<?php endif; ?>

					<?php if ( 'yes' === $settings['show_excerpt'] ) : ?>
						<div class="ep-fooevents-calendar-carousel-excerpt">
							<?php
							$excerpt_length = ! empty( $settings['excerpt_length'] ) ? (int) $settings['excerpt_length'] : 15;
							$excerpt        = get_the_excerpt( $post_id );
							if ( empty( $excerpt ) ) {
								$excerpt = get_the_content( null, false, $post_id );
							}
							echo wp_kses_post( wp_trim_words( $excerpt, $excerpt_length, '&hellip;' ) );
							?>
						</div>
					<?php endif; ?>

					<?php if ( 'yes' === $settings['show_read_more'] ) : ?>
						<a href="<?php echo esc_url( $post_link ); ?>" class="ep-fooevents-calendar-carousel-read-more">
							<?php echo esc_html( ! empty( $settings['read_more_text'] ) ? $settings['read_more_text'] : __( 'Read More', 'bdthemes-element-pack' ) ); ?>
						</a>
					<?php endif; ?>

				</div>
			</div>
		</div>
		<?php
	}

	public function render() {
		$settings = $this->get_settings_for_display();
		$query    = $this->get_events_query();

		if ( ! $query->have_posts() ) {
			echo '<div class="bdt-alert bdt-alert-warning">' . esc_html__( 'No events found.', 'bdthemes-element-pack' ) . '</div>';
			wp_reset_postdata();
			return;
		}

		$this->render_swiper_header_attribute( 'fooevents-calendar-carousel' );
		$this->add_render_attribute( 'carousel', 'class', [ 'bdt-fooevents-calendar-carousel', 'ep-fooevents-calendar-carousel' ] );
		if ( ! empty( $settings['show_time'] ) && 'yes' === $settings['show_time'] && ! empty( $settings['time_as_tooltip'] ) && 'yes' === $settings['time_as_tooltip'] ) {
			$this->add_render_attribute( 'carousel', 'class', 'ep-fcc-has-time-tooltip' );
		}
		$this->add_render_attribute( 'wrapper', 'class', 'swiper-wrapper' );
		?>
		<div <?php $this->print_render_attribute_string( 'carousel' ); ?>>
			<div <?php $this->print_render_attribute_string( 'swiper' ); ?>>
				<div <?php $this->print_render_attribute_string( 'wrapper' ); ?>>
					<?php
					while ( $query->have_posts() ) {
						$query->the_post();
						$this->render_single_item( $query->post, $settings );
					}
					?>
				</div>
				<?php if ( 'yes' === $settings['show_scrollbar'] ) : ?>
					<div class="swiper-scrollbar"></div>
				<?php endif; ?>
			</div>
			<?php
			if ( 'both' === $settings['navigation'] ) {
				$this->render_both_navigation();
				if ( 'center' === $settings['both_position'] ) {
					echo '<div class="bdt-position-z-index bdt-position-bottom"><div class="bdt-dots-container"><div class="swiper-pagination"></div></div></div>';
				}
			} elseif ( 'arrows-fraction' === $settings['navigation'] ) {
				$this->render_arrows_fraction();
				if ( 'center' === $settings['arrows_fraction_position'] ) {
					echo '<div class="bdt-dots-container"><div class="swiper-pagination"></div></div>';
				}
			} else {
				$this->render_pagination();
				$this->render_navigation();
			}
			?>
		</div>
		<?php
		wp_reset_postdata();
	}
}
