<?php
namespace SabaiApps\Directories\Component\Display\Label;

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

class CustomLabel extends AbstractLabel {

	protected function _displayLabelInfo( Entity\Model\Bundle $bundle ) {
		$info = array(
			'label'            => __( 'Custom label', 'directories-pro' ),
			'default_settings' => array(
				'label_type'  => '',
				'label_text'  => '',
				'label_field' => null,
				'conditions'  => array(),
			),
			'labellable'       => false,
			'conditionable'    => true,
		);
		foreach ( $this->_application->Filter( 'entity_label_custom_label_num', range( 1, 5 ), array( $bundle ) ) as $num ) {
			$info['multiple'][ $num ] = array(
				'default_checked' => $num === 1,
				'label'           => sprintf( __( 'Custom label #%d', 'directories-pro' ), $num ),
			);
		}
		return $info;
	}

	public function displayLabelSettingsForm( Entity\Model\Bundle $bundle, array $settings, array $parents = array() ) {
		$form              = array();
		$labellable_fields = $this->_application->Entity_Field_options(
			$bundle,
			array(
				'interface'       => 'Field\Type\ILabellable',
				'return_disabled' => true,
			)
		);
		if ( ! empty( $labellable_fields[0] )
			|| ! empty( $labellable_fields[1] )
		) {
			$form += array(
				'label_type'  => array(
					'#title'         => __( 'Label text', 'directories-pro' ),
					'#type'          => 'select',
					'#default_value' => $settings['label_type'],
					'#horizontal'    => true,
					'#options'       => array(
						''      => __( 'Custom', 'directories-pro' ),
						'field' => __( 'Select field', 'directories-pro' ),
					),
					'#weight'        => -5,
				),
				'label_text'  => array(
					'#type'          => 'textfield',
					'#placeholder'   => __( 'Enter custom text here', 'directories-pro' ),
					'#default_value' => $settings['label_text'],
					'#horizontal'    => true,
					'#weight'        => -3,
					'#states'        => array(
						'visible' => array(
							sprintf( 'select[name="%s"]', $this->_application->Form_Form_fieldName( array_merge( $parents, array( 'label_type' ) ) ) ) => array( 'value' => '' ),
						),
					),
				),
				'label_field' => array(
					'#type'             => 'select',
					'#horizontal'       => true,
					'#default_value'    => $settings['label_field'],
					'#options'          => $labellable_fields[0],
					'#options_disabled' => array_keys( $labellable_fields[1] ),
					'#weight'           => -3,
					'#states'           => array(
						'visible' => array(
							sprintf( 'select[name="%s"]', $this->_application->Form_Form_fieldName( array_merge( $parents, array( 'label_type' ) ) ) ) => array( 'value' => 'field' ),
						),
					),
				),
			);
		} else {
			$form += array(
				'label_type' => array(
					'#type'          => 'hidden',
					'#default_value' => '',
				),
				'label_text' => array(
					'#title'         => __( 'Label text', 'directories-pro' ),
					'#type'          => 'textfield',
					'#default_value' => $settings['label_text'],
					'#horizontal'    => true,
					'#weight'        => -3,
				),
			);
		}

		return $form;
	}

	public function displayLabelText( Entity\Model\Bundle $bundle, Entity\Type\IEntity $entity, array $settings ) {
		if ( empty( $settings['label_type'] ) ) {
			return array(
				'label'     => $settings['label_text'],
				'color'     => $settings['_color'],
				'translate' => true,
			);
		}

		if ( $settings['label_type'] === 'field' ) {
			if ( empty( $settings['label_field'] )
				|| ( ! $field = $this->_application->Entity_Field( $bundle, $settings['label_field'] ) )
				|| ( ! $field_type = $this->_application->Field_Type( $field->getFieldType(), true ) )
				|| ! $field_type instanceof ILabellable
				|| ( ! $labels = $field_type->fieldLabellableLabels( $field, $entity ) )
			) {
				return;
			}

			$ret = array();
			foreach ( $labels as $label ) {
				$ret[] = array(
					'label' => $label,
					'color' => $settings['_color'],
				);
			}
			return $ret;
		}
	}
}
