<?php
namespace Softech_Addons_for_Elementor\Includes\Dynamic_Tags\Tags\Post_Tags;

use Elementor\Core\DynamicTags\Tag;
use Elementor\Modules\DynamicTags\Module;
use Softech_Addons_for_Elementor\Includes\Dynamic_Tags\Groups\Softech_Post_Group;

if ( ! defined( 'ABSPATH' ) ) exit;

class Softech_Post_Terms extends Tag {

    public function get_name() {
        return 'softech-post-terms';
    }

    public function get_title() {
        return esc_html__( 'Post Terms', 'softech-addons-for-elementor' );
    }

    public function get_group() {
        return Softech_Post_Group::get_name();
    }

    public function get_categories() {
        return [ Module::TEXT_CATEGORY ];
    }

    public function register_controls() {

        $this->add_control(
            'taxonomy',
            [
                'label'   => esc_html__('Taxonomy', 'softech-addons-for-elementor'),
                'type'    => \Elementor\Controls_Manager::SELECT,
                'options' => [
                    'category' => esc_html__('Category', 'softech-addons-for-elementor'),
                    'post_tag' => esc_html__('Tags', 'softech-addons-for-elementor'),
                ],
                'default' => 'category',
            ]
        );

        // Separator
        $this->add_control(
            'separator',
            [
                'label' => esc_html__('Separator', 'softech-addons-for-elementor'),
                'type'  => \Elementor\Controls_Manager::TEXT,
                'default' => ', ',
            ]
        );

        // Link terms or not
        $this->add_control(
            'enable_links',
            [
                'label' => esc_html__('Link Terms?', 'softech-addons-for-elementor'),
                'type' => \Elementor\Controls_Manager::SWITCHER,
                'label_on' => esc_html__('Yes', 'softech-addons-for-elementor'),
                'label_off' => esc_html__('No', 'softech-addons-for-elementor'),
                'return_value' => 'yes',
                'default' => 'no',
            ]
        );


       
    }

    public function get_value( array $options = [] ) {

        $taxonomy    = $this->get_settings('taxonomy');
        $separator   = $this->get_settings('separator');
        $enable_link = $this->get_settings('enable_links') === 'yes';

        $terms = get_the_terms( get_the_ID(), $taxonomy );

        if ( empty($terms) || is_wp_error($terms) ) {
            return '';
        }

        $output_terms = [];

        foreach ( $terms as $term ) {

            $name = esc_html($term->name);

            if ( $enable_link ) {
                $url = get_term_link( $term );
                if ( ! is_wp_error( $url ) ) {
                    $output_terms[] = '<a href="' . esc_url($url) . '">' . $name . '</a>';
                }
            } else {
                $output_terms[] = $name;
            }
        }

        return implode( $separator, $output_terms );
    }

    public function render() {
        echo wp_kses_post( $this->get_value() );
    }
}