<?php

class PZY_Taxonomy_list extends \Elementor\Widget_Base
{
    public function get_name()
    {
        return 'pzy_taxonomy_list';
    }

    public function get_title()
    {
        return esc_html__('گروه بندی نوشته', 'pezeshkyar');
    }

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

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

    public function get_keywords()
    {
        return ['pzy', 'taxonomy', 'taxonomies'];
    }

    protected function register_controls()
    {
        $this->start_controls_section(
            'content',
            [
                'label' => esc_html__('تنطیمات گروه بندی', 'pezeshkyar'),
                'tab' => \Elementor\Controls_Manager::TAB_CONTENT,
            ]
        );
        $this->add_control(
            'taxonomy',
            [
                'label' => esc_html__('گروه بندی', 'pezeshkyar'),
                'type' => \Elementor\Controls_Manager::SELECT,
                'default' => 'category',
                'options' => pzy_get_taxonomies(),
            ]
        );
        $this->add_control(
            'view',
            [
                'label' => esc_html__('نحوه نمایش', 'pezeshkyar'),
                'type' => \Elementor\Controls_Manager::SELECT,
                'default' => 'default',
                'options' => [
                    'default' => esc_html__('پیش فرض', 'pezeshkyar'),
                    'flex' => esc_html__('جعبه ای', 'pezeshkyar'),
                ],
            ]
        );
        $this->add_control(
            'list_separator',
            [
                'label' => esc_html__( 'جدا کننده', 'pezeshkyar' ),
                'type' => \Elementor\Controls_Manager::TEXT,
                'default'=>'، ',
                'condition' => [
                    'view' => 'default'
                ]
            ]
        );
        $this->end_controls_section();

        $this->start_controls_section(
            'taxonomies_title',
            [
                'label' => esc_html__('عنوان گروه', 'pezeshkyar'),
                'tab' => \Elementor\Controls_Manager::TAB_STYLE,
            ]
        );
        $this->add_group_control(
            \Elementor\Group_Control_Typography::get_type(),
            [
                'name' => 'taxonomies_title_typography',
                'selector' => '{{WRAPPER}} .taxonomies-title',
            ]
        );

        $this->add_control(
            'taxonomies_title_text_color',
            [
                'label' => esc_html__('رنگ متن', 'pezeshkyar'),
                'type' => \Elementor\Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .taxonomies-title' => 'color: {{VALUE}}',
                ],
            ]
        );
        $this->add_control(
            'taxonomies_title_margin',
            [
                'label' => esc_html__('حاشیه خارجی', 'pezeshkyar'),
                'type' => \Elementor\Controls_Manager::DIMENSIONS,
                'size_units' => ['px', '%', 'em', 'rem', 'custom'],
                'selectors' => [
                    '{{WRAPPER}} .taxonomies-title' => 'margin: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
                ],
            ]
        );
        $this->add_control(
            'taxonomies_title_space_from_content',
            [
                'label' => esc_html__('فاصله عنوان گروه و لیست گروه ها', 'pezeshkyar'),
                'type' => \Elementor\Controls_Manager::SLIDER,
                'size_units' => ['px', '%', 'em', 'rem', 'custom'],
                'default' => [
                    'unit' => 'px',
                    'size' => 12,
                ],
                'selectors' => [
                    '{{WRAPPER}} .taxonomies-list' => 'gap: {{SIZE}}{{UNIT}};',
                ],
            ]
        );
        $this->end_controls_section();

        $this->start_controls_section(
            'taxonomies_ul',
            [
                'label' => esc_html__('لیست گروه ها', 'pezeshkyar'),
                'tab' => \Elementor\Controls_Manager::TAB_STYLE,
            ]
        );

        $this->add_control(
            'gap',
            [
                'label' => esc_html__('فاصله بین گروه ها', 'pezeshkyar'),
                'type' => \Elementor\Controls_Manager::SLIDER,
                'size_units' => ['px', '%', 'em', 'rem', 'custom'],

                'default' => [
                    'unit' => 'px',
                    'size' => 7,
                ],
                'selectors' => [
                    '{{WRAPPER}} .taxonomies-ul' => 'gap: {{SIZE}}{{UNIT}};',
                ],
            ]
        );

        $this->end_controls_section();

        $this->start_controls_section(
            'taxonomies_li',
            [
                'label' => esc_html__('آیتم های گروه بندی', 'pezeshkyar'),
                'tab' => \Elementor\Controls_Manager::TAB_STYLE,
            ]
        );

        $this->add_group_control(
            \Elementor\Group_Control_Typography::get_type(),
            [
                'name' => 'taxonomies_li_typography',
                'selector' => '{{WRAPPER}} .taxonomies-ul,{{WRAPPER}} .taxonomies-ul li ,{{WRAPPER}} .taxonomies-ul a',
            ]
        );

        $this->add_control(
            'taxonomies_li_text_color',
            [
                'label' => esc_html__('رنگ متن', 'pezeshkyar'),
                'type' => \Elementor\Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .taxonomies-ul,{{WRAPPER}} .taxonomies-ul li,{{WRAPPER}} .taxonomies-ul a' => 'color: {{VALUE}}',
                ],
            ]
        );

        $this->add_group_control(
            \Elementor\Group_Control_Border::get_type(),
            [
                'name' => 'taxonomies_li_border',
                'selector' => '{{WRAPPER}} .taxonomies-ul li',
                'condition' => [
                    'view' => 'flex'
                ]
            ]
        );

        $this->add_control(
            'taxonomies_li-radius',
            [
                'label' => esc_html__('شعاع', 'pezeshkyar'),
                'type' => \Elementor\Controls_Manager::DIMENSIONS,
                'size_units' => ['px', '%', 'em', 'rem', 'custom'],
                'selectors' => [
                    '{{WRAPPER}} .taxonomies-ul li' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
                ],
                'condition' => [
                    'view' => 'flex'
                ]
            ]
        );

        $this->add_group_control(
            \Elementor\Group_Control_Background::get_type(),
            [
                'name' => 'taxonomies_li_bg',
                'types' => ['classic', 'gradient', 'video'],
                'selector' => '{{WRAPPER}} .taxonomies-ul li',
                'condition' => [
                    'view' => 'flex'
                ]
            ]
        );

        $this->add_control(
            'padding',
            [
                'label' => esc_html__('فاصله داخلی', 'pezeshkyar'),
                'type' => \Elementor\Controls_Manager::DIMENSIONS,
                'size_units' => ['px', '%', 'em', 'rem', 'custom'],
                'selectors' => [
                    '{{WRAPPER}} .taxonomies-ul li' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
                ],
                'condition' => [
                    'view' => 'flex'
                ]
            ]
        );
        $this->end_controls_section();

    }

    protected function render()
    {
        $taxonomy = $this->get_settings_for_display('taxonomy');
        $view = $this->get_settings_for_display('view');

        echo "<div class='taxonomies-list pzy-d-flex pzy-align-items-center'>";

        echo "<span class='taxonomies-title pzy-d-inline-block'>" . get_taxonomy($taxonomy)->label . ":</span>";

        switch ($view) {
            case 'flex':
                $terms = get_the_terms(get_the_ID(), $taxonomy);
                echo "<ul class='taxonomies-ul pzy-d-flex pzy-flex-wrap pzy-align-items-center'>";
                if ($terms){
                    foreach ($terms as $term) {
                        echo "<li><a href='" . get_term_link($term) . "'>" . $term->name . "</a></li>";
                    }
                }
                echo "</ul>";
                break;
            default:
                echo "<div class='taxonomies-ul pzy-d-flex pzy-flex-wrap pzy-align-items-center'>";
                $separator = $this->get_settings_for_display('list_separator')??'، ';
                the_terms(get_the_ID(), $taxonomy, '', $separator, '');
                echo "</div>";
        }

        echo "</div>";


    }
}