<?php namespace BulkPriceEditor\PriceEditorPage\Services;

use BulkPriceEditor\Core\ServiceContainerTrait;
use WP_Term;

class LookupService {
	
	use ServiceContainerTrait;
	
	const CATEGORIES_SEARCH_ACTION = 'woocommerce_json_search_bpe_categories';
	const ATTRIBUTES_SEARCH_ACTION = 'woocommerce_json_search_bpe_attribute';
	
	public function __construct() {
		add_action( 'wp_ajax_' . self::CATEGORIES_SEARCH_ACTION, array( $this, 'categoriesSearchHandler' ) );
		add_action( 'wp_ajax_' . self::ATTRIBUTES_SEARCH_ACTION, array( $this, 'attributesSearchHandler' ) );
	}
	
	public function attributesSearchHandler() {
		
		if ( ! current_user_can( 'manage_options' ) ) {
			wp_send_json( array() );
		}
		
		check_ajax_referer( 'search-products', 'security' );
		
		$term      = isset( $_GET['term'] ) ? sanitize_text_field( wp_unslash( $_GET['term'] ) ) : false;
		$attribute = isset( $_GET['include'] ) ? sanitize_text_field( wp_unslash( $_GET['include'] ) ) : false;
		
		if ( $term ) {
			wp_send_json( $this->termSearchHandler( $attribute, $term ) );
		}
		
		wp_send_json( array() );
	}
	
	public function categoriesSearchHandler() {
		
		if ( ! current_user_can( 'manage_options' ) ) {
			wp_send_json( array() );
		}
		
		check_ajax_referer( 'search-products', 'security' );
		
		$term = isset( $_GET['term'] ) ? sanitize_text_field( wp_unslash( $_GET['term'] ) ) : false;
		
		if ( $term ) {
			wp_send_json( $this->termSearchHandler( 'product_cat', $term ) );
		}
		
		wp_send_json( array() );
	}
	
	protected function termSearchHandler( string $taxonomy, string $term ): array {
		
		$args = array(
			'taxonomy'   => array( $taxonomy ),
			'orderby'    => 'id',
			'order'      => 'ASC',
			'limit'      => 5,
			'hide_empty' => false,
			'fields'     => 'all',
			'name__like' => $term,
		);
		
		$terms = get_terms( $args );
		
		if ( $terms ) {
			$_terms = array();
			
			foreach ( $terms as $term ) {
				if ( $term instanceof WP_Term ) {
					$_terms[ $term->term_id ] = self::getTermLabel( $term );
				}
			}
			
			return $_terms;
		}
		
		return array();
	}
	
	public static function getTermLabel( WP_Term $category ): string {
		$parentTermName = '';
		
		if ( $category->parent ) {
			$parentTerm = get_term( $category->parent );
			
			if ( $parentTerm ) {
				$parentTermName = ' (' . $parentTerm->name . ')';
			}
		}
		
		return $category->name . $parentTermName;
	}
}
