<?php

namespace MenuManagerUltra\API\Endpoints;

use MenuManagerUltra\API\ReturnValue;
use MenuManagerUltra\lib\Term\TermDataFormatter;
use MenuManagerUltra\lib\Constants;
class Taxonomy extends \WP_REST_Controller {
    public function register_routes() {
        register_rest_route( Constants::ROUTE_BASE, '/taxonomy/search', array(
            'methods'             => 'POST',
            'callback'            => [$this, 'searchTaxonomies'],
            'permission_callback' => function ( $request ) {
                return current_user_can( Constants::ENDPOINT_PERMISSION_DEFAULT );
            },
        ) );
        register_rest_route( Constants::ROUTE_BASE, '/taxonomy/types', array(
            'methods'             => 'GET',
            'callback'            => [$this, 'getTaxonomyTypesAsObjects'],
            'permission_callback' => function ( $request ) {
                return current_user_can( Constants::ENDPOINT_PERMISSION_DEFAULT );
            },
        ) );
    }

    public function searchTaxonomies( $params ) {
        $taxonomies = $this->getTaxonomies( 'names' );
        $num_per_page = 20;
        $offset = 0;
        if ( isset( $params['offset'] ) && $params['offset'] > 0 ) {
            $offset = intval( $params['offset'] ) * $num_per_page;
        }
        if ( !empty( $params['type_filter'] ) ) {
            $taxonomies = $params['type_filter'];
        }
        $args = [
            'taxonomy'   => $taxonomies,
            'name__like' => $params['search_key'],
            'hide_empty' => false,
            'number'     => $num_per_page,
            'offset'     => $offset,
        ];
        $terms = get_terms( $args );
        if ( is_wp_error( $terms ) ) {
            // @TODO error message/logging
            return [
                'total_results' => 0,
                'results'       => [],
            ];
        }
        /* Loop through the results and add in the permalink */
        $terms = array_map( function ( $term ) {
            return TermDataFormatter::normalizeTermData( $term );
        }, $terms );
        /* Issue a query to count all matching results, regardless of pagination */
        $terms_all = get_terms( array_merge( $args, [
            'number' => false,
            'offset' => 0,
        ] ) );
        $num_results = intval( count( $terms_all ) );
        return [
            'total_results' => $num_results,
            'results'       => $terms,
        ];
    }

    public function getTaxonomyTypesAsObjects() {
        return $this->getTaxonomies( 'objects' );
    }

    public function getTaxonomies( $return_type = 'objects' ) {
        $args = array(
            'public'            => true,
            'show_in_nav_menus' => true,
        );
        $taxonomies = get_taxonomies( $args, $return_type );
        if ( $return_type == 'objects' ) {
            $ret = [];
            foreach ( $taxonomies as $name => $obj ) {
                $obj->is_taxonomy = true;
                $obj->type_slug = $name;
                $obj->display_title = $obj->label;
                $ret[] = $obj;
            }
        } else {
            $ret = $taxonomies;
        }
        return $this->taxonomiesApplyPremiumFilter( $ret );
        return $ret;
    }

    public function taxonomiesApplyPremiumFilter( $tax ) {
        return array_filter( $tax, function ( $val ) {
            if ( is_object( $val ) ) {
                if ( !empty( $val->name ) ) {
                    return in_array( $val->name, Constants::TAXONOMY_TERMS_FREE );
                } else {
                    return false;
                }
            } else {
                return in_array( $val, Constants::TAXONOMY_TERMS_FREE );
            }
        } );
    }

}
