<?php
/**
 * Alert Trigger abstract class.
 *
 * @package WP_MainWP_Stream
 */

namespace WP_MainWP_Stream;

/**
 * Class Alert_Trigger
 *
 * @package WP_MainWP_Stream
 */
abstract class Alert_Trigger {

	/**
	 * Hold the Plugin class
	 *
	 * @var Plugin
	 */
	public $plugin;

	/**
	 * Unique identifier
	 *
	 * @var string
	 */
	public $slug;

	/**
	 * Class constructor
	 *
	 * @param Plugin $plugin Plugin class.
	 */
	public function __construct( $plugin ) {
		$this->plugin = $plugin;

		add_action( 'wp_mainwp_stream_alert_trigger_form_display', array( $this, 'add_fields' ), 10, 2 );
		add_action( 'wp_mainwp_stream_alert_trigger_form_save', array( $this, 'save_fields' ), 10, 1 );
		add_filter( 'wp_mainwp_stream_alert_trigger_check', array( $this, 'check_record' ), 10, 4 );
	}

	/**
	 * Checks if a record matches the criteria from the trigger.
	 *
	 * @filter wp_mainwp_stream_alert_trigger_check
	 *
	 * @param bool  $success Status of previous checks.
	 * @param int   $record_id Record ID.
	 * @param array $recordarr Record data.
	 * @param Alert $alert The Alert being worked on.
	 * @return bool False on failure, otherwise should return original value of $success.
	 */
	abstract public function check_record ( $success, $record_id, $recordarr, $alert );

	/**
	 * Adds fields to the trigger form.
	 *
	 * @action wp_mainwp_stream_alert_trigger_form_display
	 *
	 * @param Form_Generator $form The Form Object to add to.
	 * @param Alert          $alert The Alert being worked on.
	 * @return void
	 */
	abstract public function add_fields( $form, $alert );

	/**
	 * Validate and save Alert object
	 *
	 * @action wp_mainwp_stream_alert_trigger_form_save
	 *
	 * @param Alert $alert The Alert being worked on.
	 * @return void
	 */
	abstract public function save_fields( $alert );

	/**
	 * Returns the trigger's value for the given alert.
	 *
	 * @param string $context The location this data will be displayed in.
	 * @param Alert  $alert Alert being processed.
	 * @return string
	 */
	abstract public function get_display_value( $context = 'normal', $alert );

	/**
	 * Allow connectors to determine if their dependencies is satisfied or not
	 *
	 * @return bool
	 */
	public function is_dependency_satisfied() {
		return true;
	}
}
