<?php

namespace TotalThemeCore\Widgets;

use TotalThemeCore\WidgetBuilder;

defined( 'ABSPATH' ) || exit;

/**
 * Taxonomy Terms Widget.
 */
class Widget_Taxonomy_Terms extends WidgetBuilder {

	/**
	 * Widget args.
	 */
	private $args;

	/**
	 * Register widget with WordPress.
	 */
	public function __construct() {
		$branding = $this->branding();
		$name = $branding
			? sprintf(
				/* translators: branding label */
				esc_html__( '%s - Taxonomy Terms', 'total-theme-core' ),
				$branding
			)
			: esc_html__( 'Taxonomy Terms', 'total-theme-core' );

		$this->args = [
			'id_base' => 'wpex_taxonomy_terms',
			'name'    => $name,
			'options' => [
				'customize_selective_refresh' => true,
			],
			'fields'  => [
				[
					'id'    => 'title',
					'label' => esc_html__( 'Title', 'total-theme-core' ),
					'type'  => 'text',
				],
				[
					'id'      => 'taxonomy',
					'label'   => esc_html__( 'Taxonomy', 'total-theme-core' ),
					'type'    => 'select',
					'choices' => 'taxonomies',
				],
				[
					'id'      => 'name_font_size',
					'label'   => esc_html__( 'Name Font Size', 'total-theme-core' ),
					'type'    => 'select',
					'choices' => 'utl_font_size',
				],
				[
					'id'      => 'description',
					'label'   => esc_html__( 'Show Description?', 'total-theme-core' ),
					'type'    => 'checkbox',
					'default' => 1,
				],
				[
					'id'      => 'desc_font_size',
					'label'   => esc_html__( 'Description Font Size', 'total-theme-core' ),
					'type'    => 'select',
					'choices' => 'utl_font_size',
				],
				[
					'id'      => 'count',
					'label'   => esc_html__( 'Show Count?', 'total-theme-core' ),
					'type'    => 'checkbox',
					'default' => 1,
				],
				[
					'id'      => 'count_accent_bg',
					'label'   => esc_html__( 'Use Accent Color for Count?', 'total-theme-core' ),
					'type'    => 'checkbox',
					'default' => 0,
				],
				[
					'id'      => 'hide_empty',
					'label'   => esc_html__( 'Hide Empty Categories?', 'total-theme-core' ),
					'type'    => 'checkbox',
					'default' => 1,
				],
				[
					'id'          => 'terms_exclude',
					'label'       => esc_html__( 'Exclude Terms', 'total-theme-core' ),
					'type'        => 'text',
					'description' => esc_html__( 'Enter a comma seperated list of terms.', 'total-theme-core' ),
				],
			],
		];

		$this->create_widget( $this->args );
	}

	/**
	 * Front-end display of widget.
	 */
	public function widget( $args, $instance ) {

		// Parse and extract widget settings
		extract( $this->parse_instance( $instance ) );

		// Before widget hook
		echo wp_kses_post( $args['before_widget'] );

		// Display widget title
		$this->widget_title( $args, $instance );

		// Sanitize some widget args
		$name_font_size = ! empty( $name_font_size ) ? sanitize_text_field( $name_font_size ) : 'lg';
		$desc_font_size = ! empty( $desc_font_size ) ? sanitize_text_field( $desc_font_size ) : 'sm';
		$taxonomy       = ! empty( $taxonomy ) ? sanitize_key( $taxonomy ) : '';

		// Check if Taxonomy exists.
		if ( taxonomy_exists( $taxonomy ) ) :

			$query_args = [
				'taxonomy'   => $taxonomy,
				'hide_empty' => wp_validate_boolean( $hide_empty ),
			];

			if ( ! empty( $terms_exclude ) && $exclude_list = $this->parse_terms( $terms_exclude, $taxonomy ) ) {
				$query_args['exclude'] = $exclude_list;
			}

			$terms = get_terms( $query_args );

			if ( ! empty( $terms ) && ! is_wp_error( $terms ) ) : ?>

				<ul class="wpex-taxonomy-terms-widget">

					<?php foreach ( $terms as $term ) : ?>

						<li>
							<a href="<?php echo esc_url( get_term_link( $term ) ); ?>" class="wpex-inherit-color wpex-no-underline wpex-block wpex-p-10 wpex-hover-surface-2">
								<div class="wpex-taxonomy-terms-widget-title wpex-flex wpex-justify-between wpex-items-center">
									<div class="wpex-taxonomy-terms-widget-name wpex-text-<?php echo sanitize_html_class( $name_font_size ); ?> wpex-font-medium"><?php echo esc_html( $term->name ); ?></div>
									<?php
									// Display Count.
									if ( wp_validate_boolean( $count ) ) {

										$count_class = [
											'wpex-inline-block',
											'wpex-rounded',
											'wpex-px-5',
											'wpex-leading-normal',
										];

										if ( wp_validate_boolean( $count_accent_bg ) ) {
											$count_class[] = 'wpex-bg-accent';
										} else {
											$count_class[] = 'wpex-bg-gray-600';
											$count_class[] = 'wpex-text-white';
										}
										?>
										<div class="wpex-taxonomy-terms-widget-count wpex-ml-15"><span class="<?php echo esc_attr( implode( ' ', $count_class ) ) ?>"><?php echo absint( $term->count ); ?></span></div>
									<?php } ?>
								</div>
								<?php
								// Display description.
								if ( wp_validate_boolean( $description ) ) { ?>
									<div class="wpex-taxonomy-terms-widget-desc wpex-text-3 wpex-text-<?php echo sanitize_html_class( $desc_font_size ); ?>"><?php echo esc_html( $term->description ); ?></div>
								<?php } ?>
							</a>
						</li>

					<?php endforeach; ?>

				</ul>


			<?php
			// End terms check.
			endif;

		// End taxonomy check.
		endif;

		// After widget hook.
		echo wp_kses_post( $args['after_widget'] );

	}

}
register_widget( 'TotalThemeCore\\Widgets\\Widget_Taxonomy_Terms' );
