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

use SabaiApps\Directories\Application;
use SabaiApps\Framework\User\RegisteredIdentity;

abstract class AbstractEvent implements IEvent {

	protected $_application, $_name, $_info, $_isForAdmin = false;

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

	public function notificationEventInfo( $key = null ) {
		if ( ! isset( $this->_info ) ) {
			$this->_info = (array) $this->_notificationEventInfo();
			if ( ! isset( $this->_info['weight'] ) ) {
				$this->_info['weight'] = 20;
			}
		}

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

	abstract protected function _notificationEventInfo();

	public function notificationEventIsEnabled() {
		return true;
	}

	public function notificationEventSettingsForm( array $settings, array $parents = array() ) {
		return array();
	}

	public function notificationEventDefaultNotificationData() {
		$body = array( $this->_getDefaultNotificationSalutation() );
		foreach ( (array) $this->_getDefaultNotificationBody() as $_body ) {
			$body[] = $_body;
		}
		$body[] = '%site_name%' . PHP_EOL . '%site_url%';
		return array(
			array(
				'label'  => $label = $this->_getDefaultNotificationLabel(),
				'event'  => array(
					'directories' => '',
				),
				'sender' => array(
					'email' => array(
						'recipients' => $this->_getDefaultNotificationRecipients(),
						'subject'    => '[%site_name%] ' . $label,
						'body'       => implode( PHP_EOL . PHP_EOL, array_filter( array_map( 'trim', $body ) ) ),
					),
				),
			),
		);
	}

	abstract protected function _getDefaultNotificationLabel();
	abstract protected function _getDefaultNotificationBody();

	protected function _getDefaultNotificationSalutation() {
		return $this->_isForAdmin ? __( 'Dear Administrator,', 'directories-pro' ) : sprintf( __( 'Dear %s,', 'directories-pro' ), '%author_name%' );
	}

	protected function _getDefaultNotificationRecipients() {
		if ( $this->_isForAdmin ) {
			return array(
				'role'  => 1,
				'roles' => array_keys( $this->_application->getPlatform()->getAdministratorRoles() ),
			);
		}
		return array(
			'event' => array( 'author' ),
		);
	}

	protected function _getUserTags( ?RegisteredIdentity $identity = null ) {
		if ( ! isset( $identity ) ) {
			return array(
				'%user_id%',
				'%user_usernane%',
				'%user_email%',
				'%user_name%',
				'%user_url%',
			);
		}
		return array(
			'%user_id%'       => $identity->id,
			'%user_username%' => $identity->username,
			'%user_email%'    => $identity->email,
			'%user_name%'     => $identity->name,
			'%user_url%'      => $identity->url,
		);
	}

	public function notificationEventParamSettingsForm( array $settings, array $parents = array() ) {
		return array();
	}

	public function notificationEventParams( array $settings, array $paramSettings ) {
		return array();
	}
}
