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

use Elementor\Controls_Manager;
use Elementor\Group_Control_Typography;
use Elementor\Group_Control_Background;

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

/**
 * Class Project
 */
class Pakrco_Elementor_Project extends Elementor\Widget_Base {

    public function get_name() {
        return 'pakrco-project';
    }

    public function get_title() {
        return esc_html__('Pakrco Project', 'pakrco');
    }

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

    public function get_categories() {
        return array('pakrco-addons');
    }


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

    protected function register_controls() {
        $this->start_controls_section(
            'section_layout',
            [
                'label' => esc_html__('Layout', 'pakrco'),
                'tab'   => Controls_Manager::TAB_CONTENT,
            ]
        );

        $this->add_responsive_control(
            'columns',
            [
                'label'     => esc_html__('Columns', 'pakrco'),
                'type'      => Controls_Manager::SELECT,
                'options'   => [
                    '1' => '1',
                    '2' => '2',
                    '3' => '3',
                    '4' => '4',
                    '5' => '5',
                    '6' => '6',
                ],
                'default'   => '3',
                'selectors' => [
                    '{{WRAPPER}} .swiper-container:not(.swiper-container-initialized) .swiper-wrapper > *' => 'flex: 0 0 calc(100% / {{VALUE}});max-width: calc(100% / {{VALUE}})',
                ],
            ]
        );

        $this->add_control(
            'posts_per_page',
            [
                'label'   => esc_html__('Posts Per Page', 'pakrco'),
                'type'    => Controls_Manager::NUMBER,
                'default' => 6,
            ]
        );

        $this->add_responsive_control(
            'gutter',
            [
                'label'      => esc_html__('Gutter', 'pakrco'),
                'type'       => Controls_Manager::SLIDER,
                'range'      => [
                    'px' => [
                        'min' => 0,
                        'max' => 60,
                    ],
                ],
                'default'    => [
                    'size' => 30
                ],
                'size_units' => ['px'],
                'selectors'  => [
                    '{{WRAPPER}} .swiper-container:not(.swiper-container-initialized) .swiper-wrapper > *' => 'padding-left: calc({{SIZE}}{{UNIT}} / 2); padding-right: calc({{SIZE}}{{UNIT}} / 2); margin-bottom: calc({{SIZE}}{{UNIT}})',
                    '{{WRAPPER}} .swiper-container:not(.swiper-container-initialized) .swiper-container'   => 'margin-left: calc({{SIZE}}{{UNIT}} / -2); margin-right: calc({{SIZE}}{{UNIT}} / -2);',
                ],
            ]
        );

        $this->add_control(
            'style',
            [
                'label'        => esc_html__('Style', 'pakrco'),
                'type'         => Controls_Manager::SELECT,
                'default'      => 'default',
                'options'      => [
                    'default'   => esc_html__('Default', 'pakrco'),
                    'overlay'   => esc_html__('Overlay', 'pakrco'),
                    'overlay-2' => esc_html__('Overlay 2', 'pakrco'),
                ],
                'prefix_class' => 'elementor-project-style-',
            ]
        );

        $this->add_control(
            'enable_carousel',
            [
                'label' => esc_html__('Enable Carousel', 'pakrco'),
                'type'  => Controls_Manager::SWITCHER,

            ]
        );

        $this->end_controls_section();

        $this->start_controls_section(
            'section_query',
            [
                'label' => esc_html__('Query', 'pakrco'),
                'tab'   => Controls_Manager::TAB_CONTENT,
            ]
        );

        $this->add_control(
            'advanced',
            [
                'label' => esc_html__('Advanced', 'pakrco'),
                'type'  => Controls_Manager::HEADING,
            ]
        );

//        $this->add_control(
//            'categories',
//            [
//                'label'    => esc_html__('Categories', 'pakrco'),
//                'type'     => Controls_Manager::SELECT2,
//                'options'  => $this->get_project_categories(),
//                'multiple' => true,
//            ]
//        );

        $this->add_control(
            'orderby',
            [
                'label'   => esc_html__('Order By', 'pakrco'),
                'type'    => Controls_Manager::SELECT,
                'default' => 'date',
                'options' => [
                    'date'       => esc_html__('Date', 'pakrco'),
                    'title'      => esc_html__('Title', 'pakrco'),
                    'menu_order' => esc_html__('Menu Order', 'pakrco'),
                    'rand'       => esc_html__('Random', 'pakrco'),
                    'ID'         => esc_html__('ID', 'pakrco')
                ],
            ]
        );

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

        $this->end_controls_section();

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

        $this->add_control(
            'heading_title_typo',
            [
                'type'  => Controls_Manager::HEADING,
                'label' => esc_html__('Title', 'pakrco'),
            ]
        );

        $this->add_group_control(
            Group_Control_Typography::get_type(),
            [
                'name'     => 'title_typography',
                'selector' => '{{WRAPPER}} .project-content .entry-title',
            ]
        );

        $this->add_control(
            'heading_region_typo',
            [
                'type'  => Controls_Manager::HEADING,
                'label' => esc_html__('Region', 'pakrco'),
            ]
        );

        $this->add_group_control(
            Group_Control_Typography::get_type(),
            [
                'name'     => 'region_typography',
                'selector' => '{{WRAPPER}} .project-content .entry-region',
            ]
        );

        $this->start_controls_tabs('color_tabs');

        $this->start_controls_tab('colors_normal',
            [
                'label' => esc_html__('Normal', 'pakrco'),
            ]
        );

        $this->add_control(
            'title_color',
            [
                'label'     => esc_html__('Title Color', 'pakrco'),
                'type'      => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .project-content .entry-title a' => 'color: {{VALUE}}',
                ]
            ]
        );

        $this->add_control(
            'region_color',
            [
                'label'     => esc_html__('Region Color', 'pakrco'),
                'type'      => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .project-content .entry-region a' => 'color: {{VALUE}}',
                ]
            ]
        );

        $this->add_control(
            'line_color',
            [
                'label'     => esc_html__('Line Color', 'pakrco'),
                'type'      => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .project-inner .project-content-inner' => 'border-color: {{VALUE}}',
                ]
            ]
        );

        $this->end_controls_tab();

        $this->start_controls_tab(
            'colors_hover',
            [
                'label' => esc_html__('Hover', 'pakrco'),
            ]
        );

        $this->add_control(
            'title_color_hover',
            [
                'label'     => esc_html__('Title Color', 'pakrco'),
                'type'      => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .project-content .entry-title a:hover' => 'color: {{VALUE}}',
                ]
            ]
        );

        $this->add_control(
            'region_color_hover',
            [
                'label'     => esc_html__('Region Color', 'pakrco'),
                'type'      => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .project-content .entry-region a:hover' => 'color: {{VALUE}}',
                ]
            ]
        );

        $this->end_controls_tab();

        $this->end_controls_tabs();

        $this->end_controls_section();

        $this->add_control_carousel(['enable_carousel' => 'yes']);
    }

    protected function add_control_carousel($condition = []) {
        $this->start_controls_section(
            'section_carousel_options',
            [
                'label'     => esc_html__('Carousel Options', 'pakrco'),
                'type'      => Controls_Manager::SECTION,
                'condition' => $condition
            ]
        );

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

        $this->add_responsive_control(
            'slides_to_show',
            [
                'label'              => esc_html__('Slides to Show', 'pakrco'),
                'type'               => Controls_Manager::TEXT,
                //                'options'            => [
                //                        '' => esc_html__('Default', 'elementor'),
                //                    ] + $slides_to_show,
                'frontend_available' => true,
                'default'            => 3,
                'render_type'        => 'template',
                'selectors'          => [
                    '{{WRAPPER}}' => '--e-image-carousel-slides-to-show: {{VALUE}}',
                ],
            ]
        );

        $this->add_responsive_control(
            'slides_to_scroll',
            [
                'label'              => esc_html__('Slides to Scroll', 'pakrco'),
                'type'               => Controls_Manager::SELECT,
                'description'        => esc_html__('Set how many slides are scrolled per swipe.', 'pakrco'),
                'options'            => [
                                            '' => esc_html__('Default', 'pakrco'),
                                        ] + $slides_to_show,
                'frontend_available' => true,
                'condition'          => [
                    'slides_to_show!' => '1'
                ],
            ]
        );

        $this->add_control(
            'spaceBetween',
            [
                'label'              => esc_html__('Space Between', 'pakrco'),
                'type'               => Controls_Manager::SLIDER,
                'range'              => [
                    'px' => [
                        'min' => 0,
                        'max' => 60,
                    ],
                ],
                'default'            => [
                    'size' => 30
                ],
                'size_units'         => ['px'],
                'frontend_available' => true,
            ]
        );

        $this->add_control(
            'enable_scrollbar',
            [
                'label'              => esc_html__('Enable Scrollbar', 'pakrco'),
                'type'               => Controls_Manager::SWITCHER,
                'frontend_available' => true,
            ]
        );

        $this->add_control(
            'scrollbar_fixposition',
            [
                'label'     => esc_html__('Scrollbar Fix Pos', 'pakrco'),
                'type'      => Controls_Manager::SELECT,
                'options'   => [
                    ''      => esc_html__('Default', 'pakrco'),
                    'pl-vw' => esc_html__('Left', 'pakrco'),
                    'pr-vw' => esc_html__('Right', 'pakrco'),
                ],
                'condition' => ['enable_scrollbar' => 'yes']
            ]
        );

        $this->add_control(
            'navigation',
            [
                'label'              => esc_html__('Navigation', 'pakrco'),
                'type'               => Controls_Manager::SELECT,
                'default'            => 'dots',
                'options'            => [
                    'both'   => esc_html__('Arrows and Dots', 'pakrco'),
                    'arrows' => esc_html__('Arrows', 'pakrco'),
                    'dots'   => esc_html__('Dots', 'pakrco'),
                    'none'   => esc_html__('None', 'pakrco'),
                ],
                'frontend_available' => true,
            ]
        );

        $this->add_control(
            'autoplay',
            [
                'label'              => esc_html__('Autoplay', 'pakrco'),
                'type'               => Controls_Manager::SELECT,
                'default'            => 'yes',
                'options'            => [
                    'yes' => esc_html__('Yes', 'pakrco'),
                    'no'  => esc_html__('No', 'pakrco'),
                ],
                'frontend_available' => true,
            ]
        );

        $this->add_control(
            'pause_on_hover',
            [
                'label'              => esc_html__('Pause on Hover', 'pakrco'),
                'type'               => Controls_Manager::SELECT,
                'default'            => 'yes',
                'options'            => [
                    'yes' => esc_html__('Yes', 'pakrco'),
                    'no'  => esc_html__('No', 'pakrco'),
                ],
                'condition'          => [
                    'autoplay' => 'yes',
                ],
                'render_type'        => 'none',
                'frontend_available' => true,
            ]
        );

        $this->add_control(
            'pause_on_interaction',
            [
                'label'              => esc_html__('Pause on Interaction', 'pakrco'),
                'type'               => Controls_Manager::SELECT,
                'default'            => 'yes',
                'options'            => [
                    'yes' => esc_html__('Yes', 'pakrco'),
                    'no'  => esc_html__('No', 'pakrco'),
                ],
                'condition'          => [
                    'autoplay' => 'yes',
                ],
                'frontend_available' => true,
            ]
        );

        $this->add_control(
            'autoplay_speed',
            [
                'label'              => esc_html__('Autoplay Speed', 'pakrco'),
                'type'               => Controls_Manager::NUMBER,
                'default'            => 5000,
                'condition'          => [
                    'autoplay' => 'yes',
                ],
                'render_type'        => 'none',
                'frontend_available' => true,
            ]
        );

        // Loop requires a re-render so no 'render_type = none'
        $this->add_control(
            'infinite',
            [
                'label'              => esc_html__('Infinite Loop', 'pakrco'),
                'type'               => Controls_Manager::SELECT,
                'default'            => 'yes',
                'options'            => [
                    'yes' => esc_html__('Yes', 'pakrco'),
                    'no'  => esc_html__('No', 'pakrco'),
                ],
                'condition'          => [
                    'enable_carousel' => 'yes',
                ],
                'frontend_available' => true,
            ]
        );

        $this->add_control(
            'effect',
            [
                'label'              => esc_html__('Effect', 'pakrco'),
                'type'               => Controls_Manager::SELECT,
                'default'            => 'slide',
                'options'            => [
                    'slide' => esc_html__('Slide', 'pakrco'),
                    'fade'  => esc_html__('Fade', 'pakrco'),
                ],
                'condition'          => [
                    'slides_to_show' => '1',
                ],
                'frontend_available' => true,
            ]
        );

        $this->add_control(
            'speed',
            [
                'label'              => esc_html__('Animation Speed', 'pakrco'),
                'type'               => Controls_Manager::NUMBER,
                'default'            => 500,
                'render_type'        => 'none',
                'frontend_available' => true,
            ]
        );

        $this->add_control(
            'direction',
            [
                'label'   => esc_html__('Direction', 'pakrco'),
                'type'    => Controls_Manager::SELECT,
                'default' => 'ltr',
                'options' => [
                    'ltr' => esc_html__('Left', 'pakrco'),
                    'rtl' => esc_html__('Right', 'pakrco'),
                ],
            ]
        );

        $this->end_controls_section();

        $this->start_controls_section(
            'section_style_navigation',
            [
                'label'     => esc_html__('Navigation', 'pakrco'),
                'tab'       => Controls_Manager::TAB_STYLE,
                'condition' => [
                    'navigation' => ['arrows', 'dots', 'both'],
                ],
            ]
        );

        $this->add_control(
            'heading_style_arrows',
            [
                'label'     => esc_html__('Arrows', 'pakrco'),
                'type'      => Controls_Manager::HEADING,
                'separator' => 'before',
                'condition' => [
                    'navigation' => ['arrows', 'both'],
                ],
            ]
        );

        $this->add_control(
            'arrows_position',
            [
                'label'        => esc_html__('Position', 'pakrco'),
                'type'         => Controls_Manager::SELECT,
                'default'      => 'inside',
                'options'      => [
                    'inside'  => esc_html__('Inside', 'pakrco'),
                    'outside' => esc_html__('Outside', 'pakrco'),
                ],
                'prefix_class' => 'elementor-arrows-position-',
                'condition'    => [
                    'navigation' => ['arrows', 'both'],
                ],
            ]
        );

        $this->add_control(
            'arrows_size',
            [
                'label'     => esc_html__('Size', 'pakrco'),
                'type'      => Controls_Manager::SLIDER,
                'range'     => [
                    'px' => [
                        'min' => 20,
                        'max' => 60,
                    ],
                ],
                'selectors' => [
                    '{{WRAPPER}} .elementor-swiper-button.elementor-swiper-button-prev, {{WRAPPER}} .elementor-swiper-button.elementor-swiper-button-next' => 'font-size: {{SIZE}}{{UNIT}};',
                ],
                'condition' => [
                    'navigation' => ['arrows', 'both'],
                ],
            ]
        );

        $this->add_control(
            'arrows_color',
            [
                'label'     => esc_html__('Color', 'pakrco'),
                'type'      => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .elementor-swiper-button.elementor-swiper-button-prev, {{WRAPPER}} .elementor-swiper-button.elementor-swiper-button-next' => 'color: {{VALUE}};',
                ],
                'condition' => [
                    'navigation' => ['arrows', 'both'],
                ],
            ]
        );

        $this->add_control(
            'heading_style_dots',
            [
                'label'     => esc_html__('Dots', 'pakrco'),
                'type'      => Controls_Manager::HEADING,
                'separator' => 'before',
                'condition' => [
                    'navigation' => ['dots', 'both'],
                ],
            ]
        );

        $this->add_control(
            'dots_position',
            [
                'label'        => esc_html__('Position', 'pakrco'),
                'type'         => Controls_Manager::SELECT,
                'default'      => 'outside',
                'options'      => [
                    'outside' => esc_html__('Outside', 'pakrco'),
                    'inside'  => esc_html__('Inside', 'pakrco'),
                ],
                'prefix_class' => 'elementor-pagination-position-',
                'condition'    => [
                    'navigation' => ['dots', 'both'],
                ],
            ]
        );

        $this->add_control(
            'dots_size',
            [
                'label'     => esc_html__('Size', 'pakrco'),
                'type'      => Controls_Manager::SLIDER,
                'range'     => [
                    'px' => [
                        'min' => 5,
                        'max' => 10,
                    ],
                ],
                'selectors' => [
                    '{{WRAPPER}} .swiper-pagination-bullet' => 'width: {{SIZE}}{{UNIT}}; height: {{SIZE}}{{UNIT}};',
                ],
                'condition' => [
                    'navigation' => ['dots', 'both'],
                ],
            ]
        );

        $this->add_control(
            'dots_color',
            [
                'label'     => esc_html__('Color', 'pakrco'),
                'type'      => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .swiper-pagination-bullet' => 'background: {{VALUE}};',
                ],
                'condition' => [
                    'navigation' => ['dots', 'both'],
                ],
            ]
        );

        $this->end_controls_section();
    }

    public function query_posts() {
        $query_args = [
            'orderby'             => $this->get_settings_for_display('orderby'),
            'order'               => $this->get_settings_for_display('order'),
            'ignore_sticky_posts' => 1,
            'post_status'         => 'publish', // Hide drafts/private posts for admins
            'post_type'           => 'pakrco_project',
            'posts_per_page'      => $this->get_settings_for_display('posts_per_page')
        ];

        if (!empty($this->get_settings_for_display('categories'))) {
            $query_args['tax_query'] = [
                [
                    'taxonomy' => 'pakrco_project_cat',
                    'field'    => 'slug',
                    'terms'    => $this->get_settings_for_display('categories'),
                ]
            ];
        }
        return new WP_Query($query_args);
    }

    public function render() {
        $settings        = $this->get_settings_for_display();
        $query           = $this->query_posts();
        $enable_carousel = $settings['enable_carousel'] === 'yes';
        $this->add_render_attribute([
            'wrapper' => [
                'class' => 'swiper-container',
                'dir'   => $settings['direction'] ? $settings['direction'] : 'ltr',
            ],
            'row'     => [
                'class' => 'swiper-wrapper' . ($enable_carousel ? '' : ' flex-wrap'),
            ],
        ]);
        if ($query->have_posts()) {
            ?>
            <div <?php echo pakrco_elementor_get_render_attribute_string('wrapper', $this); // WPCS: XSS ok?>>
                <div <?php echo pakrco_elementor_get_render_attribute_string('row', $this); // WPCS: XSS ok?>>
                    <?php
                    while ($query->have_posts()) : $query->the_post();
                        if ($enable_carousel) {
                            echo '<div class="swiper-slide">';
                        }
                        include get_theme_file_path('template-parts/project/content.php');
                        if ($enable_carousel) {
                            echo '</div>';
                        }
                    endwhile;
                    ?>
                </div>
                <?php
                $show_dots        = (in_array($settings['navigation'], ['dots', 'both']));
                $show_arrows      = (in_array($settings['navigation'], ['arrows', 'both']));
                $enable_scrollbar = $settings['enable_scrollbar'] === 'yes';
                if ($enable_scrollbar):
                    ?>
                    <div class="scrollbar-wrapper <?php echo esc_attr($settings['scrollbar_fixposition']) ?>">
                        <div class="swiper-scrollbar"></div>
                        <div class="button-nav-group">
                            <div class="elementor-swiper-button elementor-swiper-button-prev">
                                <i class="pakrco-icon-chevron-left" aria-hidden="true"></i>
                                <span class="elementor-screen-only"><?php echo esc_html__('Previous', 'pakrco'); ?></span>
                            </div>
                            <div class="elementor-swiper-button elementor-swiper-button-next">
                                <i class="pakrco-icon-chevron-right" aria-hidden="true"></i>
                                <span class="elementor-screen-only"><?php echo esc_html__('Next', 'pakrco'); ?></span>
                            </div>
                        </div>
                    </div>
                <?php endif; ?>
                <?php if ($show_dots && $enable_carousel) : ?>
                    <div class="swiper-pagination"></div>
                <?php endif; ?>
                <?php if ($show_arrows && $enable_carousel) : ?>
                    <div class="elementor-swiper-button elementor-swiper-button-prev">
                        <i class="eicon-chevron-left" aria-hidden="true"></i>
                        <span class="elementor-screen-only"><?php echo esc_html__('Previous', 'pakrco'); ?></span>
                    </div>
                    <div class="elementor-swiper-button elementor-swiper-button-next">
                        <i class="eicon-chevron-right" aria-hidden="true"></i>
                        <span class="elementor-screen-only"><?php echo esc_html__('Next', 'pakrco'); ?></span>
                    </div>
                <?php endif; ?>

            </div>
            <?php
        }
        wp_reset_postdata();
    }
}

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