<?php
/**
 * Attendee Page Validation class
 *
 * @link https://www.fooevents.com
 * @package woocommerce-events
 */

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


/**
 * Attendee page validation class
 */
class FooEvents_Attendee_Page_Validation {

	/**
	 * Configuration object
	 *
	 * @var array $config
	 */
	public $config;

	/**
	 * FooEvents Custom Attendee Fields
	 *
	 * @var array $fcaf
	 */
	public $fcaf;

	/**
	 * FooEvents Bookings
	 *
	 * @var array $fb
	 */
	public $fb;

	/**
	 * FooEvents Seating
	 *
	 * @var array $fs
	 */
	public $fs;

	/**
	 * FooEvents Cart
	 *
	 * @var array $fooevents_cart
	 */
	public $fooevents_cart;

	/**
	 * Attendees
	 *
	 * @var array $attendees
	 */
	public $attendees;

	/**
	 * On class load
	 *
	 * @param array  $config configuration values.
	 * @param object $fcaf FooEvents Custom Attendees attendee object.
	 * @param object $fb FooEvents Bookings attendee object.
	 * @param object $fs FooEvents Seatings attendee object.
	 */
	public function __construct( $config, $fcaf, $fb, $fs ) {

		$this->config = $config;
		$this->fcaf   = $fcaf;
		$this->fb     = $fb;
		$this->fs     = $fs;

		require_once $this->config->class_path . '/attendee/class-fooevents-cart.php';
		$this->fooevents_cart = new FooEvents_Cart( $config, $fcaf, $fb, $fs );
		$this->attendees      = $this->fooevents_cart->get_event_attendee_items();
	}

	/**
	 * Main validation method
	 */
	public function do_validation() {

		$errors = array();

		$errors = $this->security_check();

		if ( empty( $errors ) ) {

			$required_errors = $this->required_fields();
			if ( $required_errors ) {

				$errors = array_merge( $errors, $required_errors );

			}

			$email_errors = $this->email_validation();
			if ( $email_errors ) {

				$errors = array_merge( $errors, $email_errors );

			}

			$unique_attendee_errors = $this->unique_attendee_validation();
			if ( $unique_attendee_errors ) {

				$errors = array_merge( $errors, $unique_attendee_errors );

			}

			if ( $this->fcaf ) {

				$fcaf_errors = $this->fcaf->validate();
				if ( $fcaf_errors ) {

					$errors = array_merge( $errors, $fcaf_errors );

				}
			}

			if ( $this->fb ) {

				$fb_errors = $this->fb->validate();
				if ( $fb_errors ) {

					$errors = array_merge( $errors, $fb_errors );

				}
			}

			if ( $this->fs ) {

				$fs_errors = $this->fs->validate();
				if ( $fs_errors ) {

					$errors = array_merge( $errors, $fs_errors );

				}
			}
		}

		return $errors;
	}

	/**
	 * Do nonce security check.
	 */
	public function security_check() {

		if ( isset( $_POST ) && isset( $_POST['fooevents_submit_attendees'] ) ) {

			if ( ! isset( $_POST['fooevents_attendee_nonce'] ) || ! wp_verify_nonce( $_POST['fooevents_attendee_nonce'], 'fooevents_attendee_details_page' ) ) {

				return array( __( 'Security check failed. Please try again later', 'woocommerce-events' ) );

			}
		}

		return array();
	}

	/**
	 * Check required fields.
	 */
	public function required_fields() {

		$attendees = $this->attendees;
		$errors    = array();

		foreach ( $_POST['attendees'] as $event_id => $event_item ) {

			$attendee_num = 1;

			foreach ( $event_item as  $cart_item_key => $event_attendees ) {

				$required = $attendees[ $event_id ]['items'][ $cart_item_key ]['validation']['required'];
				foreach ( $event_attendees as $key => $attendee ) {

					foreach ( $required as $field ) {

						if ( empty( $attendee[ $field ] ) ) {

							$event_name = get_the_title( $event_id );
							$errors[]   = 'Missing required field "' . $field . '" for attendee s' . $attendee_num . ' in ' . $event_name . '.';

						}
					}

					++$attendee_num;

				}
			}
		}

		return $errors;
	}

	/**
	 * Validate email address.
	 */
	public function email_validation() {

		$attendees = $this->attendees;
		$errors    = array();

		foreach ( $_POST['attendees'] as $event_id => $event_item ) {

			$attendee_num = 1;

			foreach ( $event_item as  $cart_item_key => $event_attendees ) {

				if ( isset( $attendees[ $event_id ]['items'][ $cart_item_key ]['validation']['other']['email'] ) ) {

					foreach ( $event_attendees as $key => $attendee ) {

						if ( ! filter_var( $attendee['email'], FILTER_VALIDATE_EMAIL ) ) {

							$event_name = get_the_title( $event_id );
							$errors[]   = 'Invalid email for attendee ' . $attendee_num . ' in ' . $event_name . '.';

						}

						++$attendee_num;

					}
				}
			}
		}

		return $errors;
	}

	/**
	 * Validate unique email address.
	 */
	public function unique_attendee_validation() {

		$errors = array();

		foreach ( $_POST['attendees'] as $event_id => $event_item ) {

			$attendee_num = 1;

			$do_unique_validation = get_post_meta( $event_id, 'WooCommerceEventsUniqueEmail', true );

			if ( 'on' === $do_unique_validation ) {

				$unique_emails = array();

				foreach ( $event_item as  $cart_item_key => $event_attendees ) {

					foreach ( $event_attendees as $key => $attendee ) {

						$unique_emails[] = strtolower( trim( $attendee['email'] ) );

					}

					if ( ! empty( $unique_emails ) ) {

						if ( count( $unique_emails ) !== count( array_unique( $unique_emails ) ) ) {

							$event_name = get_the_title( $event_id );
							$number     = $attendee_num;
							$errors[]   = 'All attendee email addresses that are used to register for ' . $event_name . ' must be unique';

						}
					}

					++$attendee_num;

				}
			}
		}

		return $errors;
	}
}
