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

use SabaiApps\Directories\Component\Display;
use SabaiApps\Directories\Component\Entity\Model\Bundle;
use SabaiApps\Directories\Component\Entity\Type\IEntity;

class LevelDisplayLabel extends Display\Label\AbstractLabel {

	protected function _displayLabelInfo( Bundle $bundle ) {
		if ( $bundle->entitytype_name !== 'post'
			|| ! $this->_application->Entity_BundleTypeInfo( $bundle, 'levelable' )
		) {
			return false;
		}

		return array(
			'label'            => __( 'Feature level label', 'directories-pro' ),
			'default_settings' => array(
				'exclude_default' => true,
				'_color'          => array( 'type' => 'secondary' ),
			),
			'labellable'       => false,
		);
	}

	public function displayLabelSettingsForm( Bundle $bundle, array $settings, array $parents = array() ) {
		return array(
			'exclude_default' => array(
				'#type'          => 'checkbox',
				'#title'         => __( 'Exclude default feature level', 'directories-pro' ),
				'#default_value' => $settings['exclude_default'],
				'#horizontal'    => true,
			),
		);
	}

	public function displayLabelText( Bundle $bundle, IEntity $entity, array $settings ) {
		if ( ( ! $level = $entity->getSingleFieldValue( 'entity_level' ) )
			|| ! isset( $bundle->info['entity_levels'][ $level ]['label'] )
			|| ( $level === 'default' && ! empty( $settings['exclude_default'] ) )
		) {
			return;
		}

		return array(
			'label' => $bundle->info['entity_levels'][ $level ]['label'],
			'color' => $settings['_color'],
		);
	}
}
