<?php
namespace SabaiApps\Directories\Component\Social\FieldRenderer;

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

class FacebookMessengerLinkFieldRenderer extends Field\Renderer\AbstractRenderer {

	protected function _fieldRendererInfo() {
		return array(
			'label'            => __( 'Facebook Messenger Link', 'directories-pro' ),
			'field_types'      => array( 'social_accounts' ),
			'default_settings' => array(
				'type'   => 'default',
				'label'  => null,
				'target' => '_self',
			),
			'accept_multiple'  => true,
		);
	}

	protected function _fieldRendererSettingsForm( Field\IField $field, array $settings, array $parents = array() ) {
		return array(
			'type'   => array(
				'#title'         => __( 'Display format', 'directories-pro' ),
				'#type'          => 'select',
				'#options'       => $this->_getFacebookMessengerLinkDisplayFormatOptions(),
				'#default_value' => $settings['type'],
			),
			'label'  => array(
				'#placeholder'   => __( 'Custom label', 'directories-pro' ),
				'#type'          => 'textfield',
				'#default_value' => $settings['label'],
				'#states'        => array(
					'visible' => array(
						sprintf( 'select[name="%s"]', $this->_application->Form_Form_fieldName( array_merge( $parents, array( 'type' ) ) ) ) => array( 'value' => 'label' ),
					),
				),
			),
			'target' => array(
				'#title'         => __( 'Open link in', 'directories-pro' ),
				'#type'          => 'select',
				'#options'       => $this->_getLinkTargetOptions(),
				'#default_value' => $settings['target'],
			),
		);
	}

	protected function _getFacebookMessengerLinkDisplayFormatOptions() {
		return array(
			'default' => __( 'Facebook username', 'directories-pro' ),
			'label'   => __( 'Custom label', 'directories-pro' ),
		);
	}

	protected function _fieldRendererRenderField( Field\IField $field, array &$settings, Entity\Type\IEntity $entity, array $values, ?array $context = null, $more = 0 ) {
		$field_settings = $field->getFieldSettings();
		if ( empty( $field_settings['medias'] )
			|| ! in_array( 'facebook', $field_settings['medias'] )
			|| empty( $values[0]['facebook'] )
			|| ( ! $medias = $this->_application->Social_Medias() )
			|| ! isset( $medias['facebook'] )
		) {
			return;
		}

		return sprintf(
			'<a href="http://m.me/%s"%s>%s</a>',
			$this->_application->H( $_value = $values[0]['facebook'] ),
			$settings['target'] === '_blank' ? ' target="_blank" rel="noopener noreferrer"' : '',
			$settings['type'] === 'label' ? $this->_application->H( $settings['label'] ) : $_value
		);
	}

	protected function _fieldRendererReadableSettings( Field\IField $field, array $settings ) {
		$formats = $this->_getFacebookMessengerLinkDisplayFormatOptions();
		$ret     = array(
			'type' => array(
				'label' => __( 'Display format', 'directories-pro' ),
				'value' => $formats[ $settings['type'] ],
			),
		);
		if ( $settings['type'] === 'label' ) {
			$ret['label'] = array(
				'label' => __( 'Custom label', 'directories-pro' ),
				'value' => $settings['label'],
			);
		}
		$targets       = $this->_getLinkTargetOptions( true );
		$ret['target'] = array(
			'label' => __( 'Open link in', 'directories-pro' ),
			'value' => $targets[ $settings['target'] ],
		);

		return $ret;
	}
}
