<?php
/**
 * Form assigned Scoring_Rules
 *
 * @link       https://personalizewp.com/
 * @since      3.2.0 Merged into PWP
 * @since      1.1.0
 *
 * @package    PersonalizeWP
 * @subpackage PersonalizeWP/Scoring_Rules
 */

namespace PersonalizeWP\Scoring_Rules;

use PersonalizeWP\Scoring_Rules\Base;

defined( 'ABSPATH' ) || exit; // Exit if accessed directly.

/**
 * Form Scoring Rules
 *
 * Handles Activities with type `form`
 */
class Form extends Base {

	/**
	 * Types of Activity to process
	 *
	 * @var array
	 */
	protected $activity_types = array( 'form' );

	/**
	 * Types of Scoring Rule to process
	 *
	 * @var array
	 */
	protected $score_types = array( 'form' );

	/**
	 * Filter a `form` type Activity for Scoring rules
	 *
	 * @since 1.1.0
	 *
	 * @param array  $activity_args Activity arguments.
	 * @param string $type          Activity Type.
	 *
	 * @return array
	 */
	public function add_activity_score( $activity_args, $type ) {

		// Double check
		if ( ! in_array( $type, $this->activity_types, true ) || empty( $activity_args ) || ! isset( $activity_args['lead_score'] ) ) {
			return $activity_args;
		}

		if ( ! isset( $this->scoring_rules ) ) {
			$this->scoring_rules = $this->get_scoring_rules();
		}

		if ( empty( $this->scoring_rules ) ) {
			return $activity_args;
		}

		foreach ( $this->scoring_rules as $scoring_rule ) {
			// Somehow no score to apply.
			if ( empty( $scoring_rule['lead_score'] ) ) {
				continue;
			}
			if ( empty( $scoring_rule['conditions'] ) ) {
				// Simply tracking the submission of *any* form
				$activity_args['lead_score'] += $scoring_rule['lead_score'];
				continue;
			}

			$args = [
				'activity'   => (object) $activity_args, // Need to convert to object for inherited functions
				'conditions' => json_decode( $scoring_rule['conditions'], true ),
				'operator'   => $scoring_rule['operator'],
			];
			if ( $this->activity_has_matched_conditions( $args ) ) {
				// Add the Scoring Rule score back for the Activity to store.
				$activity_args['lead_score'] += $scoring_rule['lead_score'];
			}
		}

		// Always return activity args for Activity creation.
		return $activity_args;
	}

	/**
	 * Test condition against form criteria, searching for matching form ID and type.
	 *
	 * @since 2.3.0
	 *
	 * @param array $args {
	 *     Array of args to use for matching the condition.
	 *
	 *     @type Activity $activity   Activity object
	 *     @type mixed    $condition  Form ID to match
	 *     @type mixed    $options    Form/plugin type to match
	 * }
	 *
	 * @return boolean
	 */
	protected function condition_has_matched_criteria( $args ) {

		list( 'activity' => $activity, 'condition' => $form_id, 'options' => $form_type ) = $args;

		// Search for form ID match
		$has_same_form_id   = is_numeric( $form_id ) && (int) $activity->object_id === (int) $form_id;
		$has_same_form_type = ! empty( $activity->object_type ) && $form_type === $activity->object_type;

		// Matching form ID and from same plugin.
		if ( $has_same_form_id && $has_same_form_type ) {
			return true;
		}
		return false;
	}
}
