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

use SabaiApps\Directories\Application;
use SabaiApps\Directories\Component\Field\IField;
use SabaiApps\Directories\Component\Field\Type\IColumnable;
use SabaiApps\Directories\Component\Field\Type\ITitle;
use SabaiApps\Directories\Component\Entity\Type\IEntity;

class ParentFieldType extends AbstractParentFieldType implements IColumnable, ITitle {

	public function fieldTypeSchema( array $settings = array() ) {
		return array(
			'columns' => array(
				'parent_entity_id'   => array(
					'type'    => Application::COLUMN_INTEGER,
					'length'  => 20,
					'notnull' => true,
					'was'     => 'parent_entity_id',
					'default' => '',
				),
				'parent_entity_type' => array(
					'type'    => Application::COLUMN_VARCHAR,
					'length'  => 40,
					'notnull' => true,
					'was'     => 'parent_entity_type',
					'default' => '',
				),
			),
			'indexes' => array(
				'parent_entity_id'   => array(
					'fields' => array( 'parent_entity_id' => array( 'sorting' => 'ascending' ) ),
					'was'    => 'parent_entity_id',
				),
				'parent_entity_type' => array(
					'fields' => array( 'parent_entity_type' => array( 'sorting' => 'ascending' ) ),
					'was'    => 'parent_entity_type',
				),
			),
		);
	}

	public function fieldTypeOnSaveValues( IField $field, array $values, ?array $currentValues = null, array &$extraArgs = array() ) {
		$ret = array();
		foreach ( $values as $weight => $value ) {
			if ( is_array( $value ) ) {
				if ( empty( $value['parent_entity_id'] )
					|| empty( $value['parent_entity_type'] )
				) {
					continue;
				}
			} elseif ( is_numeric( $value ) ) {
				if ( empty( $currentValues[0]['parent_entity_type'] ) ) {
					continue;
				}

				$value = array(
					'parent_entity_id'   => $value,
					'parent_entity_type' => $currentValues[0]['parent_entity_type'],
				);
			}
			$ret[] = array(
				'parent_entity_id'   => $value['parent_entity_id'],
				'parent_entity_type' => $value['parent_entity_type'],
			);
			break;
		}
		return $ret;
	}

	public function fieldColumnableInfo( IField $field ) {
		if ( ( ! $bundle = $field->Bundle )
			|| empty( $bundle->info['parent'] )
			|| ( ! $parent_bundle = $this->_application->Entity_Bundle( $bundle->info['parent'] ) )
		) {
			return;
		}

		return array(
			'' => array(
				'label' => $parent_bundle->getLabel( 'singular' ),
			),
		);
	}

	public function fieldColumnableColumn( IField $field, IEntity $entity, $column = '' ) {
		if ( ( ! $values = $entity->getFieldValue( $field->getFieldName() ) )
			|| empty( $values[0]['parent_entity_type'] )
			|| empty( $values[0]['parent_entity_id'] )
			|| ( ! $parent_entity = $this->_application->Entity_Entity( $values[0]['parent_entity_type'], $values[0]['parent_entity_id'] ) )
		) {
			return;
		}

		$title_html = $this->_application->H( $parent_entity->getTitle() );
		if ( $this->_application->getPlatform()->getName() === 'WordPress'
			&& $this->_application->getPlatform()->isAdmin()
		) {
			return '<a href="' . admin_url( 'edit.php?post_type=' . $entity->getBundleName() . '&drts_wp_post_parent=' . $parent_entity->getId() ) . '">'
				. $title_html . '</a>';
		}
		return $title_html;
	}

	public function fieldTitle( IField $field, array $values ) {
		if ( ( $parent_id = $field->isPropertyField() ? $values[0] : $values[0]['value'] )
			&& ( $bundle = $field->Bundle )
			&& ( $parent_entity = $this->_application
				->Entity_Types_impl( $bundle->entitytype_name )
				->entityTypeEntityById( $parent_id ) )
		) {
			return $parent_entity->getTitle();
		}
	}
}
