<?php
namespace SabaiApps\Directories\Component\WordPressContent\Helper;

use SabaiApps\Directories\Application;
use SabaiApps\Directories\Component\Field\Type\IWpRestApi;

class RestApiHelper {

	public function init( Application $application ) {
		$content_types = $application->WordPressContent_ContentTypes();
		foreach ( array_keys( $content_types['post_types'] ) as $post_type ) {
			if ( empty( $content_types['post_types'][ $post_type ]['show_in_rest'] ) ) {
				continue;
			}

			$this->_registerRestField( $application, $post_type );
		}
		foreach ( array_keys( $content_types['taxonomies'] ) as $taxonomy_type ) {
			foreach ( array_keys( $content_types['taxonomies'][ $taxonomy_type ] ) as $taxonomy ) {
				if ( empty( $content_types['taxonomies'][ $taxonomy_type ][ $taxonomy ]['show_in_rest'] ) ) {
					continue;
				}

				$this->_registerRestField( $application, $taxonomy );
			}
		}
	}

	protected function _registerRestField( Application $application, $bundleName ) {
		if ( ! $bundle = $application->Entity_Bundle( $bundleName ) ) {
			return;
		}

		$properties = array();
		foreach ( $application->Entity_Field( $bundle ) as $field_name => $field ) {
			if ( $field->isPropertyField()
				|| ( ! $field_type = $application->Field_Type( $field->getFieldType(), true ) )
				|| ! $field_type instanceof IWpRestApi
			) {
				continue;
			}

			$schema = $application->Filter( 'wordpress_rest_field_schema', $field_type->fieldWpRestApiSchema( $field ), array( $field, $bundleName ) );
			if ( ! isset( $schema['description'] ) ) {
				$schema['description'] = $field->getFieldLabel();
			}

			$properties[ $field_name ] = $schema;
		}
		register_rest_field(
			$bundleName,
			'drts_fields',
			array(
				'get_callback'    => array( $application, 'WordPressContent_RestApi_getFields' ),
				'update_callback' => null,
				'schema'          => array(
					'description' => $bundle->getLabel( 'singular' ) . ' field data',
					'type'        => 'object',
					'context'     => array( 'view' ),
					'properties'  => $properties,
				),
			)
		);
	}

	public function getFields( Application $application, $object, $attr, $request ) {
		$ret = array();
		if ( isset( $object['taxonomy'] ) ) {
			$bundle_name = $object['taxonomy'];
			$type        = 'term';
		} else {
			$bundle_name = $object['type'];
			$type        = 'post';
		}
		foreach ( $application->Entity_Field( $bundle_name ) as $field_name => $field ) {
			if ( $field->isPropertyField()
				|| ( ! $field_type = $application->Field_Type( $field->getFieldType(), true ) )
				|| ! $field_type instanceof IWpRestApi
			) {
				continue;
			}

			if ( ! isset( $entity )
				&& ( ! $entity = $application->Entity_Entity( $type, $object['id'] ) )
			) {
				return array();
			}

			$value = $field_type->fieldWpRestApiGetValue( $field, $entity, $request );
			if ( null === $value = $application->Filter( 'wordpress_rest_get_field', $value, array( $field, $entity, $request ) ) ) {
				continue;
			}

			$ret[ $field_name ] = $value;
		}

		return $ret;
	}
}
