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

use SabaiApps\Directories\Component\Display;
use SabaiApps\Directories\Component\Entity;

class UserRoleDisplayLabel extends Display\Label\AbstractLabel {

	protected function _displayLabelInfo( Entity\Model\Bundle $bundle ) {
		if ( empty( $bundle->info['is_user'] ) ) {
			return false;
		}

		return array(
			'label'            => __( 'User role label', 'directories-pro' ),
			'default_settings' => array(
				'roles'  => null,
				'_color' => array( 'type' => 'secondary' ),
			),
			'labellable'       => false,
		);
	}

	public function displayLabelSettingsForm( Entity\Model\Bundle $bundle, array $settings, array $parents = array() ) {
		return array(
			'roles' => array(
				'#title'         => __( 'Select roles to display', 'directories-pro' ),
				'#type'          => 'checkboxes',
				'#options'       => $roles = $this->_application->getPlatform()->getRoles(),
				'#horizontal'    => true,
				'#default_value' => isset( $settings['roles'] ) ? $settings['roles'] : array_keys( $roles ),
				'#columns'       => 3,
			),
		);
	}

	public function displayLabelText( Entity\Model\Bundle $bundle, Entity\Type\IEntity $entity, array $settings ) {
		if ( ( ! $user_data = get_userdata( $entity->getId() ) )
			|| empty( $user_data->roles )
			|| ! isset( $user_data->roles[0] )
			|| ( isset( $settings['roles'] ) && ! in_array( $user_data->roles[0], $settings['roles'] ) )
			|| ! isset( $GLOBALS['wp_roles']->roles[ $user_data->roles[0] ] )
		) {
			return;
		}

		return array(
			'label' => $GLOBALS['wp_roles']->roles[ $user_data->roles[0] ]['name'],
			'color' => $settings['_color'],
		);
	}
}
