<?php
namespace SabaiApps\Directories\Component\WordPressContent\NotificationEvent;

use SabaiApps\Directories\Exception;
use SabaiApps\Directories\Component\Entity\Type\IEntity;
use SabaiApps\Directories\Component\Notification\Event\AbstractEvent;

class CommentPublishedNotificationEvent extends AbstractEvent {

	protected function _notificationEventInfo() {
		return array(
			'label'   => __( 'Comment published', 'directories-pro' ),
			'actions' => array(
				'wordpresscontent_comment_published',
			),
			'weight'  => 40,
		);
	}

	public function notificationEventMessageMeta( array $settings ) {
		return array(
			'tags'       => array_merge(
				$this->_getCommentTags(),
				$this->_application->Entity_Tokens(
					null,
					false,
					array(
						'prefix' => 'item_',
						'site'   => true,
					)
				)
			),
			'recipients' => array(
				'post_author'    => array(
					'type'  => 'user',
					'label' => __( 'Post author', 'directories-pro' ),
				),
				'comment_author' => array(
					'type'  => 'email',
					'label' => __( 'Comment author', 'directories-pro' ),
				),
			),
		);
	}

	public function notificationEventMessages( array $settings, array $params ) {
		if ( ! isset( $params[1] )
			|| ! $params[1] instanceof IEntity
		) {
			throw new Exception\RuntimeException( 'Invalid entity.' );
		}

		list($comment, $entity) = $params;
		return array(
			array(
				'entity'            => $entity,
				'entity_tag_prefix' => 'item_',
				'tags'              => $this->_getCommentTags( $comment ),
				'recipients'        => array(
					'post_author'    => array(
						'type'  => 'user',
						'value' => $entity->getAuthorId(),
					),
					'comment_author' => array(
						'type'  => 'email',
						'value' => $comment->comment_author_email,
					),
				),
			),
		);
	}

	protected function _getCommentTags( $comment = null ) {
		if ( ! isset( $comment ) ) {
			return array(
				'%comment_id%',
				'%comment_author%',
				'%comment_author_email%',
				'%comment_author_url%',
				'%comment_date%',
				'%comment_content%',
				'%comment_summary%',
			);
		}

		return array(
			'%comment_id%'           => $comment->comment_ID,
			'%comment_author%'       => $comment->comment_author,
			'%comment_author_email%' => $comment->comment_author_email,
			'%comment_author_url%'   => $comment->comment_author_url,
			'%comment_date%'         => $this->_application->System_Date_datetime( $comment->comment_date_gmt . ' GMT' ),
			'%comment_summary%'      => $this->_application->System_Text_summarize( $comment->comment_content, 100 ),
			'%comment_content%'      => $comment->comment_content,
		);
	}

	protected function _getDefaultNotificationLabel() {
		return __( 'Comment published', 'directories-pro' );
	}

	protected function _getDefaultNotificationBody() {
		return array(
			sprintf( __( 'A new comment has been published on your post "%s".', 'directories-pro' ), '%item_title%' ),
			implode(
				PHP_EOL,
				array(
					sprintf( __( 'You can view and reply to the comment here:', 'directories-pro' ) ),
					'%item_permalink_url%',
				)
			),
		);
	}

	protected function _getDefaultNotificationRecipients() {
		return array(
			'event' => array( 'post_author' ),
		);
	}

	protected function _getDefaultNotificationSalutation() {
		return sprintf( __( 'Dear %s,', 'directories-pro' ), '%item_author_name%' );
	}
}
