<?php
namespace SabaiApps\Directories\Component\Entity\FieldType;

use SabaiApps\Directories\Component\Entity\Type\IEntity;
use SabaiApps\Directories\Component\Field;

abstract class AbstractParentFieldType extends Field\Type\AbstractType implements
	Field\Type\ISchemable,
	Field\Type\ICopiable {

	protected function _fieldTypeInfo() {
		return array(
			'label'      => 'Parent Entity',
			'creatable'  => false,
			'requirable' => false,
			'required'   => true,
			'disablable' => false,
			'icon'       => 'fas fa-sitemap',
		);
	}

	public function fieldSchemaProperties( Field\IField $field ) {
		return array( 'parentItem', 'itemReviewed' );
	}

	public function fieldSchemaRenderProperty( Field\IField $field, $property, IEntity $entity ) {
		switch ( $property ) {
			case 'parentItem':
				// Fetch parent item
				if ( ! $parent = $this->_application->Entity_ParentEntity( $entity, false ) ) {
					return;
				}

				return array(
					array(
						'@type' => 'Question', // the parentItem property expects Question type
						'name'  => $this->_application->Entity_Title( $parent ),
						'text'  => $this->_application->System_Text_summarize( $parent->getContent(), 0 ),
						'url'   => (string) $this->_application->Entity_PermalinkUrl( $parent ),
					),
				);
			case 'itemReviewed':
				// Fetch parent item with field values
				if ( ! $parent = $this->_application->Entity_ParentEntity( $entity ) ) {
					return;
				}

				// Return schema.org markup of parent item
				if ( ( ! $parent_bundle = $this->_application->Entity_Bundle( $parent ) )
					|| empty( $parent_bundle->info['entity_schemaorg'] )
				) {
					return;
				}

				$parent_schema = $parent_bundle->info['entity_schemaorg'];
				if ( ! in_array(
					$parent_schema['type'],
					array(
						'Book',
						'Course',
						'CreativeWorkSeason',
						'CreativeWorkSeries',
						'Episode',
						'Event',
						'Game',
						'HowTo',
						'LocalBusiness',
						'MediaObject',
						'Movie',
						'MusicPlaylist',
						'MusicRecording',
						'Organization',
						'Product',
						'Recipe',
						'SoftwareApplication',
					)
				) ) {
					return;
				}

				return $this->_application->Entity_SchemaOrg_json( $parent, $parent_schema['type'], (array) $parent_schema['properties'] );
		}
	}

	public function fieldCopyValues( Field\IField $field, array $values, array &$allValues, $lang = null ) {
		if ( ! empty( $lang ) ) {
			if ( ! $bundle = $field->Bundle ) {
				$this->_application->logError( 'Failed fetching reference field bundle.' );
				return;
			}

			if ( $this->_application->getPlatform()->isTranslatable( $bundle->entitytype_name, $bundle->name ) ) {
				foreach ( array_keys( $values ) as $k ) {
					if ( empty( $values[ $k ][''] ) ) {
						continue;
					}

					$translation_id = (int) $this->_application->getPlatform()->getTranslatedId(
						$bundle->entitytype_name,
						$bundle->name,
						$values[ $k ]['value'],
						$lang
					);
					if ( empty( $translation_id ) ) {
						unset( $values[ $k ] );
					} else {
						$values[ $k ]['value'] = $translation_id;
					}
				}
			}
		}

		return $values;
	}
}
