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

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

class ModifiedFieldType extends Field\Type\AbstractType implements
	Field\Type\ISortable,
	Field\Type\ISchemable,
	Field\Type\IColumnable {

	protected function _fieldTypeInfo() {
		return array(
			'label'     => __( 'Modified Date', 'directories-pro' ),
			'creatable' => false,
			'icon'      => 'far fa-clock',
		);
	}

	public function fieldSortableOptions( Field\IField $field ) {
		return array(
			array( 'label' => __( 'Newest First', 'directories-pro' ) ),
			array(
				'args'    => array( 'asc' ),
				'label'   => __( 'Oldest First', 'directories-pro' ),
				'default' => true,
			),
		);
	}

	public function fieldSortableSort( Field\Query $query, $fieldName, ?array $args = null ) {
		$query->sortByField( 'modified', isset( $args ) && $args[0] === 'asc' ? 'ASC' : 'DESC' );
	}

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

	public function fieldSchemaRenderProperty( Field\IField $field, $property, IEntity $entity ) {
		return array( date( 'Y-m-d', $entity->getModified() ) );
	}

	public function fieldColumnableInfo( Field\IField $field ) {
		return array(
			'' => array(
				'label'  => $field->getFieldLabel(),
				'sortby' => true,
				'hidden' => true,
			),
		);
	}

	public function fieldColumnableColumn( Field\IField $field, IEntity $entity, $column = '' ) {
		if ( ( ! $values = $entity->getFieldValue( $field->getFieldName() ) )
			|| empty( $values[0] )
		) {
			return;
		}

		return $this->_application->System_Date_datetime( $values[0] );
	}
}
