<?php
namespace SabaiApps\Directories\Component\Notification\Sender;

use SabaiApps\Directories\Application;
use SabaiApps\Directories\Component\Entity\Type\IEntity;

abstract class AbstractSender implements ISender {

	protected $_application, $_name, $_info;

	public function __construct( Application $application, $name ) {
		$this->_application = $application;
		$this->_name        = $name;
	}

	public function notificationSenderInfo( $key = null ) {
		if ( ! isset( $this->_info ) ) {
			$this->_info = (array) $this->_notificationSenderInfo();
		}

		return isset( $key ) ? ( isset( $this->_info[ $key ] ) ? $this->_info[ $key ] : null ) : $this->_info;
	}

	public function notificationSenderRecipientList( array $settings ) {
		return array();
	}

	public function notificationSenderCount( array $settings, iterable $messages ) {
		return 1;
	}

	abstract protected function _notificationSenderInfo();

	protected function _replaceMessageTags( $text, array $message ) {
		if ( ! isset( $message['tags'] ) ) {
			$message['tags'] = array();
		}
		if ( $this->_application->isComponentLoaded( 'Dashboard' )
			&& strpos( $text, '%dashboard_url%' ) !== false
		) {
			$dashboard_slug                     = '/' . $this->_application->getComponent( 'Dashboard' )->getSlug( 'dashboard' );
			$message['tags']['%dashboard_url%'] = rtrim( $this->_application->MainUrl( $dashboard_slug, array(), '', '&' ), '/' ) . '/';
		}
		$text = strtr( $text, $message['tags'] );
		if ( isset( $message['entity'] ) ) {
			$options = array();
			if ( isset( $message['entity_tag_prefix'] ) ) {
				$options['prefix'] = $message['entity_tag_prefix'];
			}
			$text = $this->_application->Entity_Tokens_replace( $text, $message['entity'], true, $options );
		} else {
			$text = $this->_application->Entity_Tokens_replace( $text );
		}

		return $text;
	}

	protected function _getEventRecipientByMessage( $name, array $message ) {
		if ( isset( $message['recipients'][ $name ] ) ) {
			return $message['recipients'][ $name ];
		}
		if ( isset( $message['entity'] )
			&& $message['entity'] instanceof IEntity
		) {
			switch ( $name ) {
				case 'author':
					if ( ! $message['entity']->getAuthorId() ) {
						// Guest post, check if guest email exists
						if ( $email = $message['entity']->getSingleFieldValue( 'frontendsubmit_guest', 'email' ) ) {
							return array(
								'type'  => 'email',
								'value' => $email,
							);
						}
						return;
					}
				case 'user':
					return array(
						'type'  => 'user',
						'value' => $message['entity']->getAuthorId(),
					);
				case 'parent_author':
					if ( ! $parent_entity = $this->_getParentEntity( $message['entity'] ) ) {
						return;
					}

					return array(
						'type'  => 'user',
						'value' => $parent_entity->getAuthorId(),
					);
				default:
					if ( strpos( $name, 'field_' ) === 0 ) {
						$field_name = substr( $name, strlen( 'field_' ) );
						return $this->_getRecipientByEntityField( $message['entity'], $field_name );
					} elseif ( strpos( $name, 'parent_field_' ) === 0 ) {
						if ( ! $parent_entity = $this->_getParentEntity( $message['entity'] ) ) {
							return;
						}

						$field_name = substr( $name, strlen( 'parent_field_' ) );
						return $this->_getRecipientByEntityField( $parent_entity, $field_name );
					}
			}
		}
	}

	abstract protected function _getRecipientByEntityField( $entity, $fieldName );

	protected function _getParentEntity( $entity ) {
		if ( ( $bundle = $this->_application->Entity_Bundle( $entity ) )
			&& ! empty( $bundle->info['parent'] )
			&& ( $parent_entity = $this->_application->Entity_ParentEntity( $entity, false ) )
		) {
			return $parent_entity;
		}
	}
}
