<?php


namespace Simple_Giveaways\AutomatorWP;

use Simple_Giveaways\AutomatorWP_Base_Trigger;
use Simple_Giveaways\SG_AutomatorWP;

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Class Winner_Notified
 *
 * @package Simple_Giveaways\AutomatorWP
 */
class Winner_Notified extends AutomatorWP_Base_Trigger {

	/**
	 * @var string
	 */
	public $trigger = 'giveasap_winner_notified';


	/**
	 * Register the trigger
	 *
	 * @since 1.0.0
	 */
	public function register() {

		$args = array(
			'integration'   => $this->integration,
			'label'         => __( 'User won the giveaway and was notified', 'giveasap' ),
			'select_option' => __( 'User won <strong>the giveaway and was notified</strong>', 'giveasap' ),
			'edit_label'    => __( 'User won the giveaway and was notified', 'giveasap' ),
			'log_label'     => __( 'User won the giveaway and was notified', 'giveasap' ),
			'action'        => 'giveasap_notify_winners',
			'function'      => array( $this, 'listener' ),
			'priority'      => 30,
			'accepted_args' => 3,
			'tags'          => array_merge(
				SG_AutomatorWP::get_tags()
			),
		);

		automatorwp_register_trigger(
			$this->trigger,
			$args
		);
	}

	/**
	 * @param array          $winners Array of winners.
	 * @param integer        $giveaway_id Giveaway ID.
	 * @param string|boolean $subscriber Subscriber Email or false.
	 */
	public function listener( $winners, $giveaway_id, $subscriber = false ) {

		foreach ( $winners as $order => $winner ) {
			if ( false !== $subscriber && $subscriber !== $winner['email'] ) {
				continue;
			}

			$subscriber_id = empty( $winner['id'] ) ? 0 : $winner['id'];

			$giveaway_subscriber = giveasap_get_subscriber( $subscriber_id );

			// Bail if not a valid order.
			if ( ! $giveaway_subscriber ) {
				return;
			}

			$user_id = $giveaway_subscriber->get_wp_user_id();
			automatorwp_trigger_event(
				array(
					'trigger'       => $this->trigger,
					'user_id'       => $user_id,
					'subscriber_id' => $subscriber_id,
					'giveaway_id'   => $giveaway_id,
				)
			);

			if ( $subscriber ) {
				break;
			}
		}
	}

	/**
	 * Register the required hooks
	 *
	 * @since 1.0.0
	 */
	public function hooks() {

		// Log meta data
		add_filter( 'automatorwp_user_completed_trigger_log_meta', array( $this, 'log_meta' ), 10, 6 );

		parent::hooks();
	}

	/**
	 * Trigger custom log meta
	 *
	 * @since 1.0.0
	 *
	 * @param array    $log_meta           Log meta data
	 * @param stdClass $trigger            The trigger object
	 * @param int      $user_id            The user ID
	 * @param array    $event              Event information
	 * @param array    $trigger_options    The trigger's stored options
	 * @param stdClass $automation         The trigger's automation object
	 *
	 * @return array
	 */
	function log_meta( $log_meta, $trigger, $user_id, $event, $trigger_options, $automation ) {

		// Bail if action type don't match this action
		if ( $trigger->type !== $this->trigger ) {
			return $log_meta;
		}

		$log_meta['subscriber_id'] = ( isset( $event['subscriber_id'] ) ? $event['subscriber_id'] : 0 );
		$log_meta['giveaway_id']   = ( isset( $event['giveaway_id'] ) ? $event['giveaway_id'] : 0 );

		return $log_meta;

	}

}

new Winner_Notified();
