<?php
/**
 * Fired during plugin activation
 *
 * @link       https://www.francescopepe.com
 * @since      1.0.0
 *
 * @package    Popper
 * @subpackage Popper/includes
 */

namespace Popper;

use Popper\Rakit\Validation\Validator;

/**
 * Fired during plugin activation.
 *
 * This class defines all code necessary to run during the plugin's activation.
 *
 * @since      1.0.0
 * @package    Popper
 * @subpackage Popper/includes
 * @author     Francesco Pepe <info@francescopepe.com>
 */
class Submission {
	/**
	 * The ID of this plugin.
	 *
	 * @since    1.0.0
	 * @access   private
	 * @var      array $data The submission data.
	 */
	private $data;

	/**
	 * Process form submission.
	 *
	 * @since 1.0
	 */
	public function handle_form() {
		if ( check_ajax_referer( 'popper', '_nonce', false ) ) {
			// sanitize the request.
			$this->data = map_deep( $_POST, 'sanitize_text_field' );

			$this->validate();

		} else {
			wp_send_json_error( __( 'Are you a bot?', 'popper' ) );
		}
	}

	/**
	 * Process actions.
	 *
	 * @return void
	 */
	private function process() {
		$actions = get_post_meta( $this->data['_popper_id'], '_popper_actions', true );
		$popper_id = $this->data['_popper_id'];

		$this->data = $this->clean_data();

		foreach ( $actions as $settings ) {
			/**
			 * Processes the specified form action and passes related data.
			 *
			 * @param array $settings
			 * @param Form $form
			 */
			if ( $settings['async'] ) {
				wp_schedule_single_event(
					time() + 60,
					'popper_process_form_action_' . $settings['type'],
					array(
						'settings'  => $settings,
						'data'      => $this->data,
						'popper_id' => $popper_id,
					),
					true
				);
			} else {
				do_action(
					'popper_process_form_action_' . $settings['type'],
					$settings,
					$this->data,
					$popper_id,
				);
			}
		}
	}

	/**
	 * Validate the data.
	 *
	 * This will check only for email field.
	 *
	 * @return boolean
	 */
	private function validate() {

		if ( empty( $this->data['_popper_id'] ) ) {
			wp_send_json_error(
				array(
					'errors' => array(),
					'message' => __( 'Ops, an error occurred.', 'popper' ),
				)
			);
		}

		$fields = get_post_meta( $this->data['_popper_id'], '_popper_fields', true );

		$validator = new Validator();

		$validation = $validator->validate( $this->data, $fields );

		if ( $validation->passes() ) {
			$this->process();
			wp_send_json_success(
				array(
					'errors' => $validation->errors()->all( ':message' ),
					'message' => __( 'Form submitted, thanks!', 'popper' ),
				)
			);
		} else {
			wp_send_json_error(
				array(
					'errors' => $validation->errors()->all( ':message' ),
					'message' => __( 'Ops, an error occurred.', 'popper' ),
				)
			);
		}
	}

	/**
	 * Clean the data.
	 *
	 * This will remove all non field value(ex. _nonce, ajax action...).
	 *
	 * @return boolean
	 */
	private function clean_data() {
		$fields = get_post_meta( $this->data['_popper_id'], '_popper_fields', true );
		return array_intersect_key( $this->data, $fields );
	}
}
