<?php

class PZY_Reviews_Slider extends \PEZESHKYAR\CORE\PZY_SWIPER
{
    public function get_name()
    {
        return 'pzy_reviews_slider';
    }

    public function get_title()
    {
        return esc_html__('نظرات مشتریان', 'pezeshkyar');
    }

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

    public function get_categories()
    {
        return ['pezeshkyar'];
    }

    public function get_keywords()
    {
        return ['pzy', 'review', 'reviews'];
    }

    public function get_script_depends()
    {
        return ["init-feather", "swiper.js"];
    }

    public function get_style_depends()
    {
        return ['swiper.css'];
    }

    protected function register_controls()
    {
        $repeater = new \Elementor\Repeater();
        $repeater->add_control(
            'review_author',
            [
                'label' => esc_html__('نام مشتری', 'pezeshkyar'),
                'type' => \Elementor\Controls_Manager::TEXT,
                'default' => esc_html__('نام مشتری', 'pezeshkyar'),
                'label_block' => true,
            ]
        );
        $repeater->add_control(
            'review_image',
            [
                'label' => esc_html__('آواتار مشتری', 'pezeshkyar'),
                'type' => \Elementor\Controls_Manager::MEDIA,
                'default' => [
                    'url' => \Elementor\Utils::get_placeholder_image_src(),
                ],
            ]
        );
        $repeater->add_control(
            'review_point',
            [
                'label' => esc_html__('امتیاز مشتری', 'pezeshkyar'),
                'type' => \Elementor\Controls_Manager::NUMBER,
                'min' => 0,
                'max' => 10,
                'step' => 1,
                'default' => 5,
            ]
        );
        $repeater->add_control(
            'review_point_description',
            [
                'label' => esc_html__('توضیح امتیاز مشتری', 'pezeshkyar'),
                'type' => \Elementor\Controls_Manager::TEXT,
                'label_block' => true,
            ]
        );
        $repeater->add_control(
            'review_content',
            [
                'label' => esc_html__('متن دیدگاه', 'pezeshkyar'),
                'type' => \Elementor\Controls_Manager::TEXTAREA,
                'rows' => 10,
                'default' => 'دیدگاه مشتری',
                'placeholder' => esc_html__('دیدگاه مشتری را وارد کنید.', 'pezeshkyar'),
            ]
        );

        $this->start_controls_section(
            'content_section',
            [
                'label' => esc_html__('نطرات', 'pezeshkyar'),
                'tab' => \Elementor\Controls_Manager::TAB_CONTENT,
            ]
        );

        $this->add_control(
            'reviews_list',
            [
                'label' => esc_html__('نطرات مشتریان', 'pezeshkyar'),
                'type' => \Elementor\Controls_Manager::REPEATER,
                'fields' => $repeater->get_controls(),
                'default' => [],
                'title_field' => '{{{ review_author }}}',
            ]
        );
        $this->add_control(
            'max_point',
            [
                'label' => esc_html__('بیشترین امتیاز', 'textdomain'),
                'type' => \Elementor\Controls_Manager::NUMBER,
                'min' => 1,
                'max' => 10,
                'step' => 1,
                'default' => 5,
            ]
        );

        $this->end_controls_section();

        $this->start_controls_section(
            'slide_style',
            [
                'label' => esc_html__('اسلایدها', 'pezeshkyar'),
                'tab' => \Elementor\Controls_Manager::TAB_STYLE,
            ]
        );
        $this->add_control(
            'slide_padding',
            [
                'label' => esc_html__('فاصله داخلی', 'pezeshkyar'),
                'type' => \Elementor\Controls_Manager::DIMENSIONS,
                'size_units' => ['px', '%', 'em', 'rem', 'custom'],
                'selectors' => [
                    '{{WRAPPER}} .pzy-customer-review-card' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
                ],
            ]
        );
        $this->add_control(
            'slide_margin',
            [
                'label' => esc_html__('حاشیه خارجی', 'pezeshkyar'),
                'type' => \Elementor\Controls_Manager::DIMENSIONS,
                'size_units' => ['px', '%', 'em', 'rem', 'custom'],
                'selectors' => [
                    '{{WRAPPER}} .pzy-customer-review-card' => 'margin: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
                ],
            ]
        );
        $this->add_group_control(
            \Elementor\Group_Control_Background::get_type(),
            [
                'name' => 'slide_background',
                'types' => ['classic', 'gradient', 'video'],
                'selector' => '{{WRAPPER}} .pzy-customer-review-card',
            ]
        );
        $this->add_group_control(
            \Elementor\Group_Control_Border::get_type(),
            [
                'name' => 'slide_border',
                'selector' => '{{WRAPPER}} .pzy-customer-review-card',
            ]
        );
        $this->add_control(
            'slide_radius',
            [
                'label' => esc_html__('شعاع', 'pezeshkyar'),
                'type' => \Elementor\Controls_Manager::DIMENSIONS,
                'size_units' => ['px', '%', 'em', 'rem', 'custom'],
                'selectors' => [
                    '{{WRAPPER}} .pzy-customer-review-card' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
                ],
            ]
        );

        $this->add_control(
            'slide_has_shadow',
            [
                'label' => esc_html__('زیر اسلایدها سایه نمایش داده شود؟', 'pezeshkyar'),
                'type' => \Elementor\Controls_Manager::SWITCHER,
                'label_on' => esc_html__('بله', 'pezeshkyar'),
                'label_off' => esc_html__('خیر', 'pezeshkyar'),
                'return_value' => 'yes',
                'default' => 'yes',
            ]
        );
        $this->add_group_control(
            \Elementor\Group_Control_Background::get_type(),
            [
                'name' => 'slide_shadow_background',
                'types' => ['classic', 'gradient'],
                'selector' => '{{WRAPPER}} .pzy-customer-review-card-shadow',
                'condition' => [
                    'slide_has_shadow' => 'yes'
                ]
            ]
        );
        $this->add_control('slide_shadow_opacity',
            [
                'label' => esc_html__('شفاقیت سایه', 'pezeshkyar'),
                'type' => \Elementor\Controls_Manager::SLIDER,
                'size_units' => ['%'],
                'range' => [
                    '%' => [
                        'min' => 0,
                        'max' => 100,
                    ],
                ],
                'default' => [
                    'unit' => '%',
                    'size' => 100,
                ],
                'selectors' => [
                    '{{WRAPPER}} .pzy-customer-review-card-shadow' => 'opacity: {{SIZE}}{{UNIT}}'
                ],
                'condition' => [
                    'slide_has_shadow' => 'yes'
                ]
            ]);
        $this->add_group_control(
            \Elementor\Group_Control_Css_Filter::get_type(),
            [
                'name' => 'slide_filters',
                'selector' => '{{WRAPPER}} .pzy-customer-review-card-shadow',
                'condition' => [
                    'slide_has_shadow' => 'yes'
                ]
            ]
        );
        $this->add_control(
            'slide_shadow_width',
            [
                'label' => esc_html__('عرض', 'pezeshkyar'),
                'type' => \Elementor\Controls_Manager::SLIDER,
                'size_units' => ['px', 'em', 'rem'],
                'default' => [
                    'unit' => '%',
                    'size' => 92,
                ],
                'selectors' => [
                    '{{WRAPPER}} .pzy-customer-review-card-shadow' => 'width: {{SIZE}}{{UNIT}};',
                ],
                'condition' => [
                    'slide_has_shadow' => 'yes'
                ]
            ]
        );
        $this->add_control(
            'slide_shadow_height',
            [
                'label' => esc_html__('ارتفاع', 'pezeshkyar'),
                'type' => \Elementor\Controls_Manager::SLIDER,
                'size_units' => ['px', 'em', 'rem'],
                'default' => [
                    'unit' => 'px',
                    'size' => 73,
                ],
                'selectors' => [
                    '{{WRAPPER}} .pzy-customer-review-card-shadow' => 'height: {{SIZE}}{{UNIT}};',
                ],
                'condition' => [
                    'slide_has_shadow' => 'yes'
                ]
            ]
        );
        $this->add_control(
            'slide_shadow_z_index',
            [
                'label' => esc_html__('z-index', 'pezeshkyar'),
                'type' => \Elementor\Controls_Manager::NUMBER,
                'default' => -1,
            ]
        );
        $this->add_control(
            'slide_shadow_position_left',
            [
                'label' => esc_html__('چپ', 'pezeshkyar'),
                'type' => \Elementor\Controls_Manager::SLIDER,
                'size_units' => ['px', 'em', 'rem'],
                'default' => [
                    'unit' => '%',
                    'size' => 4,
                ],
                'selectors' => [
                    '{{WRAPPER}} .pzy-customer-review-card-shadow' => 'left: {{SIZE}}{{UNIT}};',
                ],
                'condition' => [
                    'slide_has_shadow' => 'yes'
                ]
            ]
        );
        $this->add_control(
            'slide_shadow_position_right',
            [
                'label' => esc_html__('راست', 'pezeshkyar'),
                'type' => \Elementor\Controls_Manager::SLIDER,
                'size_units' => ['px', 'em', 'rem'],
                'default' => [
                    'unit' => '%',
                    'size' => 4,
                ],
                'selectors' => [
                    '{{WRAPPER}} .pzy-customer-review-card-shadow' => 'right: {{SIZE}}{{UNIT}};',
                ],
                'condition' => [
                    'slide_has_shadow' => 'yes'
                ]
            ]
        );
        $this->add_control(
            'slide_shadow_position_top',
            [
                'label' => esc_html__('بالا', 'pezeshkyar'),
                'type' => \Elementor\Controls_Manager::SLIDER,
                'size_units' => ['px', 'em', 'rem'],
                'selectors' => [
                    '{{WRAPPER}} .pzy-customer-review-card-shadow' => 'top: {{SIZE}}{{UNIT}};',
                ],
                'condition' => [
                    'slide_has_shadow' => 'yes'
                ]
            ]
        );
        $this->add_control(
            'slide_shadow_position_bottom',
            [
                'label' => esc_html__('پایین', 'pezeshkyar'),
                'type' => \Elementor\Controls_Manager::SLIDER,
                'size_units' => ['px', 'em', 'rem'],
                'selectors' => [
                    '{{WRAPPER}} .pzy-customer-review-card-shadow' => 'bottom: {{SIZE}}{{UNIT}};',
                ],
                'condition' => [
                    'slide_has_shadow' => 'yes'
                ]
            ]
        );
        $this->end_controls_section();

        $this->start_controls_section(
            'author_style',
            [
                'label' => esc_html__('نویسنده', 'pezeshkyar'),
                'tab' => \Elementor\Controls_Manager::TAB_STYLE,
            ]
        );
        $this->add_control(
            'author_text_color',
            [
                'label' => esc_html__('رنگ', 'pezeshkyar'),
                'type' => \Elementor\Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .pzy-customer-review-author' => 'color: {{VALUE}}',
                ],
            ]
        );
        $this->add_group_control(
            \Elementor\Group_Control_Typography::get_type(),
            [
                'name' => 'author_text_typography',
                'selector' => '{{WRAPPER}} .pzy-customer-review-author',
            ]
        );
        $this->end_controls_section();

        $this->start_controls_section(
            'avatar_style',
            [
                'label' => esc_html__('آواتار', 'pezeshkyar'),
                'tab' => \Elementor\Controls_Manager::TAB_STYLE,
            ]
        );
        $this->add_control(
            'avatar_dimension',
            [
                'label' => esc_html__('اندازه آواتار', 'pezeshkyar'),
                'type' => \Elementor\Controls_Manager::IMAGE_DIMENSIONS,
                'default' => [
                    'width' => '48',
                    'height' => '48',
                ],
            ]
        );
        $this->add_group_control(
            \Elementor\Group_Control_Border::get_type(),
            [
                'name' => 'avatar_border',
                'selector' => '{{WRAPPER}} .pzy-customer-review-avatar',
            ]
        );
        $this->add_control(
            'avatar_radius',
            [
                'label' => esc_html__('شعاع', 'pezeshkyar'),
                'type' => \Elementor\Controls_Manager::DIMENSIONS,
                'size_units' => ['px', '%', 'em', 'rem', 'custom'],
                'selectors' => [
                    '{{WRAPPER}} .pzy-customer-review-avatar' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
                ],
            ]
        );
        $this->end_controls_section();

        /**/
        $this->start_controls_section(
            'point_star_style',
            [
                'label' => esc_html__('ستاره', 'pezeshkyar'),
                'tab' => \Elementor\Controls_Manager::TAB_STYLE,
            ]
        );
        $this->add_control(
            'point_stars_gap',
            [
                'label' => esc_html__('فاصله بین ستاره ها', 'pezeshkyar'),
                'type' => \Elementor\Controls_Manager::SLIDER,
                'size_units' => ['px', 'em', 'rem'],
                'default' => [
                    'unit' => 'px',
                    'size' => 3,
                ],
                'selectors' => [
                    '{{WRAPPER}} .pzy-customer-review-stars' => 'gap: {{SIZE}}{{UNIT}};direction:ltr;',
                ],
            ]
        );
        $this->add_control(
            'point_star_size',
            [
                'label' => esc_html__('اندازه', 'pezeshkyar'),
                'type' => \Elementor\Controls_Manager::SLIDER,
                'size_units' => ['px', 'em', 'rem'],
                'default' => [
                    'unit' => 'px',
                    'size' => 12,
                ],
                'selectors' => [
                    '{{WRAPPER}} .pzy-customer-review-star' => 'width: {{SIZE}}{{UNIT}};height: {{SIZE}}{{UNIT}};',
                ],
            ]
        );
        $this->add_control(
            'point_star_color',
            [
                'label' => esc_html__('رنگ پیش فرض', 'pezeshkyar'),
                'type' => \Elementor\Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .pzy-customer-review-star path' => 'fill: {{VALUE}}',
                ],
                'default' => '#D6F9F1'
            ]
        );
        $this->add_control('point_star_opacity',
            [
                'label' => esc_html__('شفاقیت پیش فرض', 'pezeshkyar'),
                'type' => \Elementor\Controls_Manager::SLIDER,
                'size_units' => ['%'],
                'range' => [
                    '%' => [
                        'min' => 0,
                        'max' => 100,
                    ],
                ],
                'default' => [
                    'unit' => '%',
                    'size' => 30,
                ],
                'selectors' => [
                    '{{WRAPPER}} .pzy-customer-review-star' => 'opacity: {{SIZE}}{{UNIT}}'
                ]
            ]);
        $this->add_control(
            'point_star_active_color',
            [
                'label' => esc_html__('رنگ حالت فعال', 'pezeshkyar'),
                'type' => \Elementor\Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .pzy-customer-review-star.pzy-customer-review-star-active path' => 'fill: {{VALUE}}',
                ],
                'default' => '#00DCA7'
            ]
        );
        $this->add_control('point_star_active_opacity',
            [
                'label' => esc_html__('شفاقیت حالت فعال', 'pezeshkyar'),
                'type' => \Elementor\Controls_Manager::SLIDER,
                'size_units' => ['%'],
                'range' => [
                    '%' => [
                        'min' => 0,
                        'max' => 100,
                    ],
                ],
                'default' => [
                    'unit' => '%',
                    'size' => 100,
                ],
                'selectors' => [
                    '{{WRAPPER}} .pzy-customer-review-star-active' => 'opacity: {{SIZE}}{{UNIT}}'
                ]
            ]);
        $this->end_controls_section();
        /**/

        /**/
        $this->start_controls_section(
            'point_description_style',
            [
                'label' => esc_html__('توضیح امتیاز مشتری', 'pezeshkyar'),
                'tab' => \Elementor\Controls_Manager::TAB_STYLE,
            ]
        );
        $this->add_control(
            'point_gap',
            [
                'label' => esc_html__('فاصله امتیاز و توشیح', 'pezeshkyar'),
                'type' => \Elementor\Controls_Manager::SLIDER,
                'size_units' => ['px', '%', 'em', 'rem', 'custom'],
                'selectors' => [
                    '{{WRAPPER}} .pzy-customer-review-point' => 'gap: {{SIZE}}{{UNIT}};',
                ],
            ]
        );
        $this->add_control(
            'point_description_text_color',
            [
                'label' => esc_html__('رنگ', 'pezeshkyar'),
                'type' => \Elementor\Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .pzy-customer-review-point-description' => 'color: {{VALUE}}',
                ],
            ]
        );
        $this->add_group_control(
            \Elementor\Group_Control_Typography::get_type(),
            [
                'name' => 'point_description_typography',
                'selector' => '{{WRAPPER}} .pzy-customer-review-point-description',
            ]
        );
        $this->end_controls_section();
        /**/

        $this->start_controls_section(
            'review_content_style',
            [
                'label' => esc_html__('متن دیدگاه', 'pezeshkyar'),
                'tab' => \Elementor\Controls_Manager::TAB_STYLE,
            ]
        );
        $this->add_control(
            'content_text_align',
            [
                'label' => esc_html__('ترازبندی', 'pezeshkyar'),
                'type' => \Elementor\Controls_Manager::CHOOSE,
                'options' => [
                    'left' => [
                        'title' => esc_html__('چپ', 'pezeshkyar'),
                        'icon' => 'eicon-text-align-left',
                    ],
                    'center' => [
                        'title' => esc_html__('وسط', 'pezeshkyar'),
                        'icon' => 'eicon-text-align-center',
                    ],
                    'right' => [
                        'title' => esc_html__('راست', 'pezeshkyar'),
                        'icon' => 'eicon-text-align-right',
                    ],
                    'justify' => [
                        'title' => esc_html__('تراز شده', 'pezeshkyar'),
                        'icon' => 'eicon-text-align-justify',
                    ],
                ],
                'default' => 'center',
                'toggle' => true,
                'selectors' => [
                    '{{WRAPPER}} .pzy-customer-review-content' => 'text-align: {{VALUE}};',
                ],
            ]
        );
        $this->add_control(
            'content_text_color',
            [
                'label' => esc_html__('رنگ', 'pezeshkyar'),
                'type' => \Elementor\Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .pzy-customer-review-content' => 'color: {{VALUE}}',
                ],
            ]
        );
        $this->add_group_control(
            \Elementor\Group_Control_Typography::get_type(),
            [
                'name' => 'content_text_typography',
                'selector' => '{{WRAPPER}} .pzy-customer-review-content',
            ]
        );
        $this->add_control(
            'content_margin',
            [
                'label' => esc_html__('حاشیه خارجی', 'pezeshkyar'),
                'type' => \Elementor\Controls_Manager::DIMENSIONS,
                'size_units' => ['px', '%', 'em', 'rem', 'custom'],
                'selectors' => [
                    '{{WRAPPER}} .pzy-customer-review-content' => 'margin: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
                ],
            ]
        );
        $this->end_controls_section();


        parent::register_controls();
    }

    protected function render_slides()
    {
        $reviews_list = $this->get_settings_for_display('reviews_list');
        $avatar_size = $this->get_settings_for_display('avatar_dimension');
        $slide_has_shadow = $this->get_settings_for_display('slide_has_shadow') == 'yes';
        $max_point = $this->get_settings_for_display('max_point');
        $avatar_size_array = [$avatar_size['width'], $avatar_size['height']];
        if ($reviews_list) {
            foreach ($reviews_list as $review) {
                ?>
                <div class="swiper-slide">
                    <div class="pzy-customer-review-card pzy-position-relative pzy-d-flex pzy-flex-column">
                        <div class="pzy-d-flex pzy-flex-row-nowrap pzy-align-items-center pzy-justify-content-between">
                            <div class="pzy-d-flex pzy-flex-column-wrap pzy-justify-content-center">
                                <p class="pzy-customer-review-author"><?php echo $review['review_author'] ?></p>
                                <div class="pzy-customer-review-point pzy-d-flex pzy-align-items-center">
                                    <p class="pzy-customer-review-stars pzy-d-flex pzy-flex-row-wrap pzy-align-items-center">
                                        <?php for ($i = 0; $i < $max_point; $i++): ?>
                                            <svg class="pzy-customer-review-star <?php echo $i < $review['review_point'] ? 'pzy-customer-review-star-active' : '' ?>"
                                                 xmlns="http://www.w3.org/2000/svg" width="13" height="12"
                                                 viewBox="0 0 13 12" fill="none">
                                                <path d="M6.34963 0.875L8.13423 4.44795L12.1247 5.0209L9.23718 7.80205L9.91884 11.7291L6.34963 9.875L2.78043 11.7291L3.46209 7.80205L0.574543 5.0209L4.56503 4.44795L6.34963 0.875Z"/>
                                            </svg>
                                        <?php endfor; ?>
                                    </p>
                                    <?php if (!empty($review['review_point_description'])): ?>
                                        <p class="pzy-customer-review-point-description"><?php echo $review['review_point_description'] ?></p>
                                    <?php endif; ?>
                                </div>
                            </div>
                            <?php if (!empty($review['review_image']['url'])): ?>
                                <p class="pzy-customer-review-avatar-wrapper"><?php echo wp_get_attachment_image($review['review_image']['id'], $avatar_size_array, false, ['class' => 'pzy-customer-review-avatar']); ?></p>
                            <?php endif; ?>
                        </div>
                        <div class="pzy-customer-review-content"><?php echo $review['review_content'] ?></div>
                        <?php if ($slide_has_shadow): ?>
                            <?php $z_index = $this->get_settings_for_display('slide_shadow_z_index') ?>
                            <div class="pzy-customer-review-card-shadow pzy-position-absolute"
                                 style="z-index: <?php echo $z_index ?>"></div>
                        <?php endif; ?>
                    </div>
                </div>
                <?php
            }
        }
    }

    protected function render()
    {
        ?>
        <div class="pzy-reviews-slider" style="height:100%">
            <?php parent::render(); ?>
        </div>
        <?php
        echo <<<EOL
<script>
feather.replace();
</script>
EOL;
    }
}