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

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

class ReportThresholdReachedNotificationEvent extends AbstractEvent {

	protected $_isForAdmin = true;

	protected function _notificationEventInfo() {
		return array(
			'label'   => __( 'Report threshold reached', 'directories-pro' ),
			'actions' => array(
				'voting_report_threshold_reached',
			),
			'weight'  => 30,
		);
	}

	public function notificationEventIsEnabled() {
		return (bool) $this->_application->getComponent( 'Voting' )->getConfig( 'report_enable' );
	}

	public function notificationEventMessageMeta( array $settings ) {
		return array(
			'tags' => array_merge(
				$this->_getVoteResultTags(),
				$this->_application->Entity_Tokens(
					null,
					false,
					array(
						'prefix' => 'item_',
						'site'   => true,
					)
				)
			),
		);
	}

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

		list($entity, $result) = $params;
		return array(
			array(
				'entity'            => $entity,
				'entity_tag_prefix' => 'item_',
				'tags'              => $this->_getVoteResultTags( $result ),
			),
		);
	}

	protected function _getVoteResultTags( ?array $result = null ) {
		if ( ! isset( $result ) ) {
			return array(
				'%report_count%',
				'%report_total_severity%',
				'%report_average_severity%',
				'%report_last_reported%',
				'%report_last_severity%',
			);
		}

		return array(
			'%report_count%'            => $result['count'],
			'%report_total_severity%'   => $result['sum'],
			'%report_average_severity%' => $result['average'],
			'%report_last_reported%'    => $this->_application->System_Date_datetime( $result['last_voted'], false ),
			'%report_last_severity%'    => $result['last_value'],
		);
	}

	protected function _getDefaultNotificationLabel() {
		return __( 'An item has reached the report threshold', 'directories-pro' );
	}

	protected function _getDefaultNotificationBody() {
		return array(
			__( 'One of the items on your site has reached the report threshold and may require your attention.', 'directories-pro' ),
			implode(
				PHP_EOL,
				array(
					sprintf( __( 'Item type: %s', 'directories-pro' ), '%item_content_type%' ),
					sprintf( __( 'Title/Name: %s', 'directories-pro' ), '%item_title%' ),
					sprintf( __( 'Number of reports: %s', 'directories-pro' ), '%report_count%' ),
					sprintf( __( 'Total report points: %s', 'directories-pro' ), '%report_total_severity%' ),
					sprintf( __( 'Average Severity: %s', 'directories-pro' ), '%report_average_severity%' ),
					sprintf( __( 'Last report date: %s', 'directories-pro' ), '%report_last_reported%' ),
					sprintf( __( 'Link: %s', 'directories-pro' ), '%item_admin_permalink_url%' ),
				)
			),
			__( 'Please review the item and take appropriate action if necessary.', 'directories-pro' ),
		);
	}
}
