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

use SabaiApps\Directories\Exception;

class PlanExpiredNotificationEvent extends AbstractEntityNotificationEvent {

	protected function _notificationEntityEventInfo() {
		return array(
			'label'            => _x( 'Payment plan expired', 'notification event', 'directories-pro' ) . ' - %s',
			'default_settings' => array(),
			'actions'          => array(
				'payment_entity_expired',
			),
			'weight'           => 24,
			'testable'         => true,
		);
	}

	public function notificationEventMessages( array $settings, array $params ) {
		if ( ! $entity = $this->_application->Entity_Entity( $params[0], $params[1] ) ) {
			return array();
		}

		return $this->_getMessageIterator( array( $entity ), $settings );
	}

	protected function _getDefaultNotificationLabel() {
		return sprintf(
			__( 'Your %s has expired', 'directories-pro' ),
			strtolower( $singular = $this->_getContentTypeLabel() ),
			$singular
		);
	}

	protected function _getDefaultNotificationBody() {
		return array(
			sprintf(
				__( 'The following %1$s posted on our site has expired on %2$s.', 'directories-pro' ),
				strtolower( $singular = $this->_getContentTypeLabel() ),
				'%payment_expire_on%',
				$singular
			),
			'%title%' . PHP_EOL . '%permalink_url%',
			sprintf( __( 'You can renew the %1$s from the dashboard page on our site.', 'directories-pro' ), strtolower( $singular ), $singular ) . PHP_EOL . '%dashboard_url%',
			sprintf(
				__( 'If you do not renew your %1$s in %2$s days, it will be deactivated and hidden from public view.', 'directories-pro' ),
				strtolower( $singular ),
				'%payment_renew_grace_period%',
				$singular
			),
		);
	}

	protected function _getQueryEntitiesUrl( array $settings, $status = null, $num = 5 ) {
		return $this->_application->MainUrl(
			'/_drts/entity/' . $this->_bundleType . '/query_payment_entities',
			$this->_getQueryEntitiesParams( $settings, $status, $num ) + array( 'payment_plan' => 1 ),
			'',
			'&'
		);
	}

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