<?php

namespace Simple_Giveaways;

/**
 * Class SG_AutomatorWP
 *
 * @package Simple_Giveaways
 */
class SG_GamiPress extends GA_Integration {

	/**
	 * Get Categories
	 *
	 * @return string[]
	 */
	public function get_categories() {
		return [
			'automation',
			'gamification',
		];
	}

	/**
	 * Setting initial params
	 */
	public function __construct() {

		$this->title       = __( 'GamiPress', 'giveasap' );
		$this->settings_id = 'sg_gamipress';
		$this->image       = untrailingslashit( GASAP_URI ) . '/includes/integrations/assets/gamipress-logo.png';
		$this->desc        = __( 'Gamify through Giveaways.', 'giveasap' );
		
		add_filter( 'gamipress_specific_activity_trigger_label', array( $this, 'specific_triggers_labels' ) );
		add_filter( 'gamipress_activity_triggers', array( $this, 'register_triggers' ) );
		add_filter( 'gamipress_specific_activity_triggers', array( $this, 'specific_activity_triggers' ) );
		add_action( 'giveasap_register_user', array( $this, 'trigger_register_user' ), 99, 5 );
		add_filter( 'gamipress_log_event_trigger_meta_data', array( $this, 'log_event_trigger_extended_meta_data' ), 10, 5 );
		add_filter( 'gamipress_specific_trigger_get_id', array( $this, 'specific_trigger_get_id' ), 20, 3 );
		add_filter( 'gamipress_trigger_duplicity_check', array( $this, 'duplicity_check' ), 20, 5 );
		add_action( 'giveasap_notify_winners', array( $this, 'trigger_winner' ), 99, 2 );
		parent::__construct();
	}

	/**
	 * Check Duplicate records.
	 *
	 * @param boolean $return  Return.
	 * @param integer $user_id User ID.
	 * @param string  $trigger Trigger.
	 * @param integer $site_id Site ID.
	 * @param array   $args    Arguments.
	 *
	 * @return bool|mixed|null
	 */
	public function duplicity_check( $return, $user_id, $trigger, $site_id, $args ) {
		if ( ! $return ) {
			return $return;
		}

		switch ( $trigger ) {
			case 'sg_gamipress_register_user':
			case 'sg_gamipress_register_user_specific':
				$log_meta['giveaway_id'] = $args[0];
				$log_meta['post_id']     = $args[0];
				$return = (bool) ( gamipress_get_user_last_log( $user_id, $log_meta ) === false );
				break;
			case 'sg_gamipress_winner_user':
			case 'sg_gamipress_winner_specific':
				$log_meta['giveaway_id'] = $args[0];
				$log_meta['post_id']     = $args[0];
				$return = (bool) ( gamipress_get_user_last_log( $user_id, $log_meta ) === false );
			break;
		}

		return $return;
	}

	/**
	 * Return the ID for the specific trigger.
	 *
	 * @param integer $specific_id Specific ID.
	 * @param string  $trigger     Trigger.
	 * @param array   $args        Arguments.
	 *
	 * @return mixed
	 */
	public function specific_trigger_get_id( $specific_id, $trigger, $args ) {

		switch ( $trigger ) {
			case 'sg_gamipress_register_user_specific':
				$specific_id = $args[0];
				break;
		}

		return $specific_id;
	}

	/**
	 * Log the meta.
	 * @param array   $log_meta Array of meta.
	 * @param integer $user_id  User ID.
	 * @param string  $trigger  Trigger.
	 * @param integer $site_id  Site ID.
	 * @param array   $args     Arguments.
	 *
	 * @return mixed
	 */
	public function log_event_trigger_extended_meta_data( $log_meta, $user_id, $trigger, $site_id, $args ) {

		switch ( $trigger ) {
			case 'sg_gamipress_register_user':
			case 'sg_gamipress_register_user_specific':
			case 'sg_gamipress_winner':
			case 'sg_gamipress_winner_specific':
				$log_meta['post_id']     = gamipress_get_event_arg( $args, 'post_id', 0 );
				$log_meta['giveaway_id'] = gamipress_get_event_arg( $args, 'giveaway_id', 0 );
		}
		return $log_meta;
	}

	/**
	 * Trigger the User Register hook.
	 * @param integer $entry_id    Subscriber ID.
	 * @param integer $giveaway_id Giveaway ID.
	 * @param string  $email       Email.
	 * @param object  $entry       Subscriber object.
	 * @param string  $ref_id      Referrer ID.
	 */
	public function trigger_register_user( $entry_id, $giveaway_id, $email, $entry, $ref_id ) {
		do_action( 'sg_gamipress_register_user', $giveaway_id, $email, get_current_user_id(), $entry );
		do_action( 'sg_gamipress_register_user_specific', $giveaway_id, $email, get_current_user_id(), $entry );
	}

	/**
	 * Trigger the Winner Hook
	 * @param integer $entry_id    Subscriber ID.
	 * @param integer $giveaway_id Giveaway ID.
	 * @param string  $email       Email.
	 * @param object  $entry       Subscriber object.
	 * @param string  $ref_id      Referrer ID.
	 */
	public function trigger_winner( $winners, $giveaway_id ) {

		foreach ( $winners as $winner ) {
			$user = get_user_by( 'email', $winner['email'] );


			$subscriber = giveasap_get_subscriber( $winner['id'] );
			$subscriber->get_data();

			do_action( 'sg_gamipress_winner', $giveaway_id, $winner['email'], $user->ID, $subscriber );
			do_action( 'sg_gamipress_winner_specific', $giveaway_id,  $winner['email'], $user->ID, $subscriber  );
		}
	}

	/**
	 * Specific Trigger Labels.
	 *
	 * @param array $labels Labels.
	 *
	 * @return mixed
	 */
	public function specific_triggers_labels( $labels ) {
		$labels['sg_gamipress_register_user_specific'] = __( 'Subscribes to %s', 'giveasap' );
		$labels['sg_gamipress_winner_specific'] = __( 'Won in %s', 'giveasap' );

		return $labels;
	}

	/**
	 * Post Types for Specific Triggers.
	 *
	 * @param array $specific_triggers Triggers.
	 *
	 * @return mixed
	 */
	public function specific_activity_triggers( $specific_triggers ) {

		$specific_triggers['sg_gamipress_register_user_specific'] = array( 'giveasap' );
		$specific_triggers['sg_gamipress_winner_specific'] = array( 'giveasap' );

		return $specific_triggers;
	}

	/**
	 * Registering GamiPress Triggers
	 *
	 * @param array $triggers Array of actions.
	 *
	 * @return mixed
	 */
	public function register_triggers( $triggers ) {

		$triggers[ __( 'Simple Giveaways', 'giveasap' ) ] = array(
			'sg_gamipress_register_user' => __( 'User Subscribers to a Giveaway', 'giveasap' ),
			'sg_gamipress_register_user_specific' => __( 'User Subscribers to a Specific Giveaway', 'giveasap' ),
			'sg_gamipress_winner' => __( 'User won a Giveaway', 'giveasap' ),
			'sg_gamipress_winner_specific' => __( 'User won a Specific Giveaway', 'giveasap' ),
		);

		return $triggers;
	}
}

add_filter( 'giveasap_integrations', '\Simple_Giveaways\sg_gamipress_register' );

/**
 * Registering Math Captcha
 *
 * @return array
 */
function sg_gamipress_register( $integrations ) {

	$integrations['sg_gamipress'] = '\Simple_Giveaways\SG_GamiPress';

	return $integrations;
}
