<?php
namespace CSSIgniter\Ignition\Elementor\Widget;

class Taxonomy_Terms extends \Elementor\Widget_Base {

	use \CSSIgniter\Ignition\Elementor\Common_Functions;

	public function get_style_depends() {

		wp_register_style( 'ignition-elementor-taxonomy-terms', plugin_dir_url( __DIR__ ) . 'assets/css/taxonomy-terms.min.css', array(), IGNITION_ELEMENTOR_WIDGETS_VERSION );

		return [
			'ignition-elementor-taxonomy-terms',
		];

	}

	public function get_name() {
		return 'ci_taxonomy_terms';
	}

	public function get_title() {
		return __( 'Taxonomy Terms Widget', 'ignition-elementor-widgets' );
	}

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

	public function get_categories() {
		return array( 'ignition-elementor-widgets' );
	}

	protected function register_controls() {

		$this->start_controls_section(
			'section_title',
			array(
				'label' => __( 'Taxonomy Terms Widget', 'ignition-elementor-widgets' ),
			)
		);

		$taxonomies = wp_list_pluck( get_taxonomies( array( 'public' => true ), 'objects' ), 'label', 'name' );

		$this->add_control(
			'taxonomy',
			array(
				'label'   => __( 'Taxonomy', 'ignition-elementor-widgets' ),
				'type'    => \Elementor\Controls_Manager::SELECT,
				'default' => 'category',
				'options' => $taxonomies,
			)
		);

		foreach ( $taxonomies as $name => $label ) {
			$this->add_control(
				'selected_taxonomy_' . $name,
				array(
					'label'     => $label,
					'type'      => \Elementor\Controls_Manager::SELECT2,
					'multiple'  => true,
					'default'   => '',
					'options'   => wp_list_pluck( get_terms( $name ), 'name', 'slug' ),
					'condition' => array(
						'taxonomy' => $name,
					),
				)
			);
		}

		$this->add_control(
			'columns',
			array(
				'label'   => __( 'Columns', 'ignition-elementor-widgets' ),
				'type'    => \Elementor\Controls_Manager::SELECT,
				'default' => '2',
				'options' => array(
					1 => '1',
					2 => '2',
					3 => '3',
					4 => '4',
				),
			)
		);

		$this->end_controls_section();

	}

	protected function render() {
		$settings            = $this->get_settings();
		$taxonomy            = $settings['taxonomy'];
		$included_term_slugs = $settings[ 'selected_taxonomy_' . $taxonomy ];
		$columns             = $settings['columns'];

		if ( ! empty( $included_term_slugs ) ) {
			?>
		<div class="row">
			<?php
			foreach ( $included_term_slugs as $term_slug ) {
				$term_obj = get_term_by( 'slug', $term_slug, $taxonomy );
				if ( empty( $term_obj ) || is_null( $term_obj ) || is_wp_error( $term_obj ) ) {
					continue;
				}

				$cover_image = ignition_get_term_meta( $term_obj->term_id, 'cover_image', ignition_image_bg_control_defaults() );
				$image_id    = ! empty( $cover_image['image_id'] ) ? $cover_image['image_id'] : false;

				$item_template_vars = array(
					'columns'  => $columns,
					'term_obj' => $term_obj,
					'image_id' => $image_id,
				);

				?>
				<div class="<?php echo esc_attr( ignition_get_columns_classes( $columns ) ); ?>">
					<?php
					if ( 1 === absint( $columns ) ) {
						ignition_get_template_part( 'template-parts/block/taxonomy-terms/term-article-media', $taxonomy, $item_template_vars );
					} else {
						ignition_get_template_part( 'template-parts/block/taxonomy-terms/term-item', $taxonomy, $item_template_vars );
					}
					?>
				</div>
				<?php
			}
			?>
		</div>
			<?php
		}
	}

}
