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

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

class PostPendingEvent extends AbstractEntityEvent {

	protected $_isForAdmin = true, $_paramEntityStatus = 'pending';

	protected function _notificationEntityEventInfo() {
		return array(
			'label'            => _x( 'Post pending review', 'notification event', 'directories-pro' ) . ' - %s',
			'default_settings' => array(
				'ignore_backend' => true,
			),
			'actions'          => array(
				'entity_create_entity_success',
				'entity_update_entity_success',
			),
			'weight'           => 5,
			'testable'         => true,
		);
	}

	public function notificationEventSettingsForm( array $settings, array $parents = array() ) {
		$form  = parent::notificationEventSettingsForm( $settings, $parents );
		$form += array(
			'ignore_backend' => array(
				'#type'          => 'checkbox',
				'#title'         => __( 'Ignore backend submissions', 'directories-pro' ),
				'#default_value' => ! empty( $settings['ignore_backend'] ),
				'#horizontal'    => true,
			),
		);
		return $form;
	}

	public function notificationEventMessages( array $settings, array $params ) {
		// Ignore backend submissions?
		if ( $this->_application->getPlatform()->isAdmin()
			&& ( ! isset( $settings['ignore_backend'] ) || $settings['ignore_backend'] )
		) {
			return;
		}

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

		if ( ! $this->_isBundleEnabled( $params[1], $settings ) ) {
			return;
		}

		if ( $params['_action'] === 'entity_update_entity_success' ) {
			$values     = $params[3];
			$extra_args = $params[4];
		} else {
			$values     = $params[2];
			$extra_args = $params[3];
		}
		if ( ! empty( $extra_args['notification_ignore_post_pending'] ) ) {
			return;
		}

		// Make sure status has been initialized/updated to pending
		if ( ! $params[1]->isPending()
			|| ! isset( $values['status'] )
		) {
			return;
		}

		if ( $params['_action'] === 'entity_update_entity_success' ) {
			if ( ! $params[2] instanceof IEntity ) {
				throw new Exception\RuntimeException( 'Invalid entity.' );
			}
			// Do not trigger if previous status was published
			if ( $params[2]->isPublished() ) {
				return;
			}
		}

		return array( array( 'entity' => $params[1] ) );
	}

	public function notificationEventParams( array $settings, array $paramSettings ) {
		$params = array(
			'_action' => 'entity_create_entity_success',
			2         => array( 'status' => true ),
		);
		if ( ! $entity = $this->_application->Entity_Entity( 'post', $paramSettings['entity'] ) ) {
			throw new Exception\RuntimeException( 'Failed fetching post. ID: ' . $paramSettings['entity'] );
		}
		$params[1] = $entity;
		return $params;
	}

	protected function _getDefaultNotificationLabel() {
		return sprintf( __( '%s is pending review', 'directories-pro' ), $this->_getContentTypeLabel() );
	}

	protected function _getDefaultNotificationBody() {
		return array(
			sprintf( __( 'A new %1$s has been submitted.', 'directories-pro' ), strtolower( $singular = $this->_getContentTypeLabel() ), $singular ),
			'%title%' . PHP_EOL . '%admin_permalink_url%',
		);
	}
}
