<?php

namespace Elementor;

use RivaxStudio\Traits\Rivax_Group_Control_Query;
use RivaxStudio\Traits\Rivax_Global_Widget_Controls;
use RivaxStudio\Traits\Rivax_Post_skin_base;

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


class Rivax_Notch_Slider_Widget extends Widget_Base {

    use Rivax_Group_Control_Query;
    use Rivax_Global_Widget_Controls;
    use Rivax_Post_skin_base;

    public function get_name() {
        return 'rivax-notch-slider';
    }

    public function get_title() {
        return esc_html__('Notch Slider', 'newsfy');
    }

    public function get_icon() {
        return 'eicon-post-slider rivax-e-icon';
    }

    protected function get_html_wrapper_class() {
        return $this->get_name() . '-widget';
    }

    public function get_categories() {
        return ['rivax-elements'];
    }
	
	public function get_style_depends() {
		return [ 'swiper', 'e-swiper' ];
	}


    protected function register_post_controls() {

        $this->start_controls_section(
            'section_post_settings',
            [
                'label' => esc_html__( 'Post Settings', 'newsfy' ),
                'tab'   => Controls_Manager::TAB_CONTENT,
            ]
        );

        $this->add_control(
            'content_position',
            [
                'label'     => esc_html__( 'Content Position', 'newsfy' ),
                'type'      => Controls_Manager::SELECT,
                'default'   => 'bottom-left',
                'options'   => [
                    'top-left'       => esc_html__( 'Top Left', 'newsfy' ),
                    'top-right'       => esc_html__( 'Top Right', 'newsfy' ),
                    'bottom-left'       => esc_html__( 'Bottom Left', 'newsfy' ),
                    'bottom-center'       => esc_html__( 'Bottom Center', 'newsfy' ),
                    'bottom-right'      => esc_html__( 'Bottom Right', 'newsfy' ),
                    'center-center'      => esc_html__( 'Center Center', 'newsfy' ),
                    'center-left'      => esc_html__( 'Center Left', 'newsfy' ),
                    'center-right'      => esc_html__( 'Center Right', 'newsfy' ),
                ],
            ]
        );

        $this->add_responsive_control(
            'content_width',
            [
                'label'     => esc_html__( 'Content Width', 'newsfy' ),
                'type'      => Controls_Manager::SLIDER,
                'size_units'=> [ 'px', '%' ],
                'range'     => [
                    'px' => [
                        'min' => 200,
                        'max' => 1000,
                    ],
                    '%' => [
                        'min' => 20,
                        'max' => 100,
                    ],
                ],
                'selectors' => [
                    '{{WRAPPER}} .content-wrapper' => 'width: {{SIZE}}{{UNIT}};',
                ],
            ]
        );

        $this->add_responsive_control(
            'item_height',
            [
                'label'     => esc_html__( 'Item Height', 'newsfy' ),
                'type'      => Controls_Manager::SLIDER,
                'range'     => [
                    'px' => [
                        'min' => 300,
                        'max' => 1200,
                    ]
                ],
                'selectors' => [
                    '{{WRAPPER}} .post-wrapper' => 'height: {{SIZE}}{{UNIT}};',
                ],
            ]
        );

        $this->add_control(
            'align_content',
            [
                'label'     => esc_html__( 'Content Alignment', 'newsfy' ),
                'type'      => Controls_Manager::CHOOSE,
                'options'   => [
                    'left'   => [
                        'title' => esc_html__( 'Left', 'newsfy' ),
                        'icon'  => 'eicon-text-align-left',
                    ],
                    'center' => [
                        'title' => esc_html__( 'Center', 'newsfy' ),
                        'icon'  => 'eicon-text-align-center',
                    ],
                    'right'  => [
                        'title' => esc_html__( 'Right', 'newsfy' ),
                        'icon'  => 'eicon-text-align-right',
                    ],
                ],
                'selectors' => [
                    '{{WRAPPER}} .content-wrapper-inner' => 'text-align: {{VALUE}};',
                ],
            ]
        );

        $this->add_group_control(
            Group_Control_Image_Size::get_type(),
            [
                'name' => 'thumbnail',
                'exclude' => ['custom'],
                'default' => 'rivax-wide',
                'separator' => 'before',
            ]);

        $this->add_control(
            'skip_lazy_loading',
            [
                'label'     => esc_html__( 'Skip Lazy Loading', 'newsfy' ),
                'description' => sprintf(esc_html__('Use this to remove lazy loading from %s Largest Contentful Paint (LCP) %s image.', 'newsfy'), '<a href="' . esc_url('https://gtmetrix.com/dont-lazy-load-lcp-image.html') . '" target="_blank">', '</a>'),
                'type'      => Controls_Manager::SELECT,
                'options'   => [
                    '0'     => esc_html__( 'No', 'newsfy' ),
                    '1'     => esc_html__( '1 Image', 'newsfy' ),
                ],
            ]
        );

        $this->add_control(
            'image_zoom_size',
            [
                'label'     => esc_html__( 'Image Zoom Size', 'newsfy' ),
                'type'      => Controls_Manager::SLIDER,
                'range'     => [
                    'px' => [
                        'min' => 1,
                        'max' => 2,
                        'step' => 0.1,
                    ],
                ],
                'default' => [
                    'unit' => 'px',
                    'size' => 1,
                ],
            ]
        );

        $this->register_title_controls('h2');

        $this->register_terms_controls();

        $this->register_author_controls();

        $this->register_date_controls();

        $this->register_comments_controls();

        $this->register_views_count_controls();

        $this->register_reading_time_controls();

        $this->register_excerpt_controls();

        $this->register_read_more_controls();

        $this->end_controls_section();

    }


    protected function register_style_post_controls() {

        $this->start_controls_section('section_style_post',
            [
                'label' => esc_html__('Items', 'newsfy'),
                'tab' => Controls_Manager::TAB_STYLE,
            ]
        );

        $this->add_group_control(
            Group_Control_Background::get_type(),
            [
                'name' => 'item_overlay_color',
                'label' => esc_html__('Overlay Color', 'newsfy'),
                'exclude' => [ 'image' ],
                'selector' => '{{WRAPPER}} .image-wrapper::before',
            ]
        );

	    $this->add_responsive_control('items_border_radius',
		    [
			    'label' => esc_html__('Border Radius', 'newsfy'),
			    'type' => Controls_Manager::DIMENSIONS,
			    'size_units' => ['px', '%'],
			    'selectors' => [
				    '{{WRAPPER}} .main-slider, {{WRAPPER}} .thumb-wrapper .image-wrapper' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
			    ],
		    ]);

        $this->end_controls_section();



        $this->start_controls_section('section_style_content',
            [
                'label' => esc_html__('Content', 'newsfy'),
                'tab' => Controls_Manager::TAB_STYLE,
            ]);


        $this->add_responsive_control('content_padding',
            [
                'label' => esc_html__('Padding', 'newsfy'),
                'type' => Controls_Manager::DIMENSIONS,
                'size_units' => ['px', '%'],
                'selectors' => [
                    '{{WRAPPER}} .post-wrapper .content-wrapper-inner' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
                ],
            ]);

        $this->add_responsive_control('content_margin',
            [
                'label' => esc_html__('Margin', 'newsfy'),
                'type' => Controls_Manager::DIMENSIONS,
                'size_units' => ['px', '%'],
                'selectors' => [
                    '{{WRAPPER}} .post-wrapper .content-wrapper' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
                ],
            ]);


        $this->add_control('item_content_background',
            [
                'label' => esc_html__('Background', 'newsfy'),
                'type' => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .content-wrapper-inner' => 'background: {{VALUE}};',
                ],
            ]);

        $this->add_control('item_content_bg_dark_mode',
            [
                'label' => esc_html__('Background - Dark Mode', 'newsfy'),
                'type' => Controls_Manager::COLOR,
                'selectors' => [
                    'html[scheme="dark"] {{WRAPPER}} .content-wrapper-inner' => 'background: {{VALUE}};',
                ],
            ]);

        $this->add_group_control(
            Group_Control_Border::get_type(),
            [
                'name' => 'item_content_border',
                'selector' => '{{WRAPPER}} .content-wrapper-inner',
            ]);

        $this->add_control('item_content_border_color_dark_mode',
            [
                'label' => esc_html__('Border Color - Dark Mode', 'newsfy'),
                'type' => Controls_Manager::COLOR,
                'condition' => [
                    'item_content_border_border!' => '',
                ],
                'selectors' => [
                    'html[scheme="dark"] {{WRAPPER}} .content-wrapper-inner' => 'border-color: {{VALUE}};',
                ],
            ]);

        $this->add_responsive_control('item_content_border_radius',
            [
                'label' => esc_html__('Border Radius', 'newsfy'),
                'type' => Controls_Manager::DIMENSIONS,
                'size_units' => ['px', '%'],
                'selectors' => [
                    '{{WRAPPER}} .content-wrapper-inner' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
                ],
            ]);

        $this->add_group_control(Group_Control_Box_Shadow::get_type(),
            [
                'name' => 'item_content_box_shadow',
                'selector' => '{{WRAPPER}} .content-wrapper-inner',
            ]);


        $this->end_controls_section();


    }


    protected function register_controls() {

        $this->register_query_builder_controls();
        $this->register_post_controls();
        $this->register_slider_controls();
        $this->register_carousel_nav_controls('slider');

        $this->register_style_post_controls();
        $this->register_style_title_controls();
        $this->register_style_terms_controls();
        $this->register_style_meta_controls();
        $this->register_style_excerpt_controls();
        $this->register_style_read_more_controls();
        $this->register_style_carousel_nav_controls('slider');

    }


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

        $id = 'rivax-slider-' . $this->get_id();

        $this->add_render_attribute( 'slider', 'id', $id );
        $this->add_render_attribute( 'slider', 'class', 'rivax-posts-carousel-wrapper' );

        if ( $settings['carousel_pagination'] ) {
            $pagination_type = $settings['carousel_pagination_type'];
        } else {
            $pagination_type = '';
        }

        $slider_settings = [
            "autoplay"              => ( "yes" == $settings["slider_autoplay"] ) ? [ "delay" => $settings["slider_autoplay_speed"] ] : false,
            "loop"                  => ( $settings["slider_loop"] == "yes" ),
            "speed"                 => $settings["slider_speed"]["size"],
            "pauseOnMouseEnter"     => ( $settings["slider_pauseonhover"] == "yes" ),
            "grabCursor"            => ( $settings["slider_grab_cursor"] == "yes" ),
            "parallax"              => true,
            "loopedSlides"          => 2,
            "navigation"            => [
                "nextEl" => "#" . $id . " .carousel-nav-next",
                "prevEl" => "#" . $id . " .carousel-nav-prev",
            ],
            "pagination"            => [
                "el"             => "#" . $id . " .carousel-pagination",
                "type"           => $pagination_type,
                "clickable"      => "true",
                'dynamicBullets' => ( "yes" == $settings["carousel_dynamic_bullets"] ),
            ],
            "a11y"            => [
                "enabled"             => "false",
            ],
        ];


        $this->add_render_attribute('slider', 'data-settings', wp_json_encode(array_filter($slider_settings)) );

        ?>
    <div <?php $this->print_render_attribute_string( 'slider' ); ?>>
            <div class="swiper main-slider">
                <div class="swiper-wrapper">
        <?php
    }


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

        $widget_path_name = str_replace('rivax-', '', $this->get_name() );
        include RIVAX_THEME_DIR . '/elementor/templates/' . $widget_path_name . '/' . $widget_path_name . '.php';
    }

}