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

use SabaiApps\Directories\Component\Field;
use SabaiApps\Directories\Application;
use SabaiApps\Directories\Component\Entity\Model\Bundle;
use SabaiApps\Directories\Component\Entity\Type\IEntity;

class AccountsFieldType extends Field\Type\AbstractValueType implements
	Field\Type\ISchemable,
	Field\Type\IOpenGraph,
	Field\Type\IHumanReadable,
	Field\Type\IPersonalData {

	protected function _fieldTypeInfo() {
		return array(
			'label'            => __( 'Social Accounts', 'directories-pro' ),
			'default_renderer' => 'social_accounts',
			'default_settings' => array(),
			'icon'             => 'fas fa-share-alt',
			'requirable'       => false,
			'schema_type'      => 'string',
		);
	}

	public function fieldTypeSettingsForm( $fieldType, Bundle $bundle, array $settings, array $parents = array(), array $rootParents = array() ) {
		$options = array();
		foreach ( $this->_application->Social_Medias() as $media_name => $media ) {
			$options[ $media_name ] = isset( $media['icon'] ) ? sprintf( '<i class="%s"></i> %s', $this->_application->H( $media['icon'] ), $this->_application->H( $media['label'] ) ) : $this->_application->H( $media['label'] );
		}
		$form = array(
			'medias' => array(
				'#type'             => 'sortablecheckboxes',
				'#title'            => __( 'Social medias', 'directories-pro' ),
				'#options'          => $options,
				'#default_value'    => isset( $settings['medias'] ) ? $settings['medias'] : null,
				'#option_no_escape' => true,
			),
		);
		return $form;
	}

	public function fieldTypeOnSaveValues( Field\IField $field, array $values, ?array $currentValues = null, array &$extraArgs = array() ) {
		$value = array_shift( $values ); // single entry allowed for this field
		return parent::fieldTypeOnSaveValues( $field, $value, $currentValues, $extraArgs );
	}

	protected function _onSaveValue( Field\IField $field, $value, array $settings, $key ) {
		return strlen( $key ) && strlen( $value ) ? $key . '|' . $value : null;
	}

	public function fieldTypeOnLoadValues( Field\IField $field, array &$values, IEntity $entity, array $allValues ) {
		$new_value = array();
		foreach ( $values as $value ) {
			$value                  = explode( '|', $value['value'], 2 );
			$new_value[ $value[0] ] = $value[1];
		}
		$values = array( $new_value );
	}

	public function fieldTypeIsModified( Field\IField $field, $valueToSave, $currentLoadedValue ) {
		$current = array();
		if ( isset( $currentLoadedValue[0] ) ) {
			foreach ( $currentLoadedValue[0] as $media => $value ) {
				$current[] = array(
					'media' => $media,
					'value' => $value,
				);
			}
		}
		return $current !== $valueToSave;
	}

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

	public function fieldSchemaRenderProperty( Field\IField $field, $property, IEntity $entity ) {
		if ( ! $value = $entity->getSingleFieldValue( $field->getFieldName() ) ) {
			return;
		}

		switch ( $property ) {
			case 'sameAs':
				$ret    = array();
				$medias = $this->_application->Social_Medias();
				foreach ( $value as $media_name => $url ) {
					if ( $media_name === 'mail'
						|| ( ! $media = @$medias[ $media_name ] )
					) {
						continue;
					}

					$ret[] = $url;
				}
				return $ret;
			case 'email':
				return array( $value['mail'] );
		}
	}

	public function fieldOpenGraphProperties() {
		return array( 'article:author', 'books:author', 'music:musician' );
	}

	public function fieldOpenGraphRenderProperty( Field\IField $field, $property, IEntity $entity ) {
		if ( ( ! $value = $entity->getSingleFieldValue( $field->getFieldName() ) )
			|| empty( $value['facebook'] )
		) {
			return;
		}

		$medias = $this->_application->Social_Medias();
		if ( empty( $medias['facebook'] ) ) {
			return;
		}

		return array( $value['facebook'] );
	}

	public function fieldHumanReadableText( Field\IField $field, IEntity $entity, $separator = null, $key = null ) {
		$ret = array();
		if ( $values = $entity->getSingleFieldValue( $field->getFieldName() ) ) {
			$medias = $this->_application->Social_Medias();
			foreach ( $values as $media_name => $value ) {
				if ( ! isset( $medias[ $media_name ] )
					|| ( ! $url = $this->_application->getComponent( $medias[ $media_name ]['component'] )->socialMediaUrl( $media_name, $value ) )
				) {
					continue;
				}

				$ret[] = $medias[ $media_name ]['label'] . ' - ' . $url;
			}
		}
		return empty( $ret ) ? '' : implode( isset( $separator ) ? $separator : PHP_EOL, $ret );
	}

	public function fieldPersonalDataExport( Field\IField $field, IEntity $entity ) {
		$ret = array();
		if ( $values = $entity->getSingleFieldValue( $field->getFieldName() ) ) {
			$medias = $this->_application->Social_Medias();
			foreach ( $values as $media_name => $value ) {
				if ( ! isset( $medias[ $media_name ] )
					|| ! strlen( $value )
				) {
					continue;
				}

				$ret[ $media_name ] = array(
					'name'  => $medias[ $media_name ]['label'],
					'value' => $this->_application->getComponent( $medias[ $media_name ]['component'] )->socialMediaUrl( $media_name, $value ),
				);
			}
		}
		return $ret;
	}

	public function fieldPersonalDataErase( Field\IField $field, IEntity $entity ) {
		return true; // delete
	}
}
