<?php
/**
 * Cart handler class
 *
 * @link https://www.fooevents.com
 * @package woocommerce-events
 */

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


/**
 * Cart handler class
 */
class FooEvents_Cart {

	/**
	 * 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;

	/**
	 * 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 Seating attendee object.
	 */
	public function __construct( $config, $fcaf, $fb, $fs ) {

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

	/**
	 * Get attendee details from cart and format to useable format.
	 */
	public function get_event_attendee_items() {

		$event_attendee_items = array();

		$x = 0;
		foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {

			$product_id   = $cart_item['product_id'];
			$variation_id = $cart_item['variation_id'];

			$product = wc_get_product( $product_id );

			$event = get_post_meta( $product_id, 'WooCommerceEventsEvent', true );

			if ( 'Event' === $event ) {

				$quantity  = $cart_item['quantity'];
				$attendees = isset( $cart_item['attendees'] ) ? $cart_item['attendees'] : array();

				$event_attendee_items[ $product_id ]['items'][ $cart_item_key ]['id']                                        = $product_id;
				$event_attendee_items[ $product_id ]['items'][ $cart_item_key ]['variation_id']                              = $variation_id;
				$event_attendee_items[ $product_id ]['items'][ $cart_item_key ]['title']                                     = $product->get_name();
				$event_attendee_items[ $product_id ]['items'][ $cart_item_key ]['quantity']                                  = $quantity;
				$event_attendee_items[ $product_id ]['items'][ $cart_item_key ]['cart_key']                                  = $cart_item_key;
				$event_attendee_items[ $product_id ]['items'][ $cart_item_key ]['WooCommerceEventsType']                     = get_post_meta( $product_id, 'WooCommerceEventsType', true );
				$event_attendee_items[ $product_id ]['items'][ $cart_item_key ]['WooCommerceEventsCaptureAttendeeDetails']   = get_post_meta( $product_id, 'WooCommerceEventsCaptureAttendeeDetails', true );
				$event_attendee_items[ $product_id ]['items'][ $cart_item_key ]['WooCommerceEventsCaptureAttendeeEmail']     = get_post_meta( $product_id, 'WooCommerceEventsCaptureAttendeeEmail', true );
				$event_attendee_items[ $product_id ]['items'][ $cart_item_key ]['WooCommerceEventsCaptureAttendeeTelephone'] = get_post_meta( $product_id, 'WooCommerceEventsCaptureAttendeeTelephone', true );
				$event_attendee_items[ $product_id ]['items'][ $cart_item_key ]['WooCommerceEventsCaptureAttendeeCompany']   = get_post_meta( $product_id, 'WooCommerceEventsCaptureAttendeeCompany', true );
				$event_attendee_items[ $product_id ]['items'][ $cart_item_key ]['WooCommerceEventsCaptureAttendeeDesignation'] = get_post_meta( $product_id, 'WooCommerceEventsCaptureAttendeeDesignation', true );

				if ( $this->fcaf ) {

					$event_attendee_items[ $product_id ]['items'][ $cart_item_key ]['fcaf']['status'] = $this->fcaf->does_product_contain_fcaf( $product_id );

				}

				if ( $this->fb ) {

					$event_attendee_items[ $product_id ]['items'][ $cart_item_key ]['fb']['status']    = $this->fb->does_product_contain_fb( $product_id );
					$event_attendee_items[ $product_id ]['items'][ $cart_item_key ]['fb']['cart_meta'] = $this->fb->add_cart_meta( $cart_item );
					$event_attendee_items[ $product_id ]['items'][ $cart_item_key ]['fb']['view']      = $this->fb->get_booking_view( $product_id );

				}

				if ( $this->fs ) {

					$event_attendee_items[ $product_id ]['items'][ $cart_item_key ]['fs']['status']    = $this->fs->does_product_contain_fs( $product_id );
					$event_attendee_items[ $product_id ]['items'][ $cart_item_key ]['fs']['cart_meta'] = $this->fs->add_cart_meta( $cart_item );

				}

				$event_attendee_items[ $product_id ]['items'][ $cart_item_key ]['attendees'] = $attendees;

				// validation requirements.
				$required         = array();
				$other_validation = array();

				if ( 'on' === $event_attendee_items[ $product_id ]['items'][ $cart_item_key ]['WooCommerceEventsCaptureAttendeeDetails'] ) {

					array_push( $required, 'first_name', 'last_name' );

				}

				if ( 'on' === $event_attendee_items[ $product_id ]['items'][ $cart_item_key ]['WooCommerceEventsCaptureAttendeeEmail'] ) {

					array_push( $required, 'email' );

					$other_validation['email'] = array( 'valid_email' );

				}

				$event_attendee_items[ $product_id ]['items'][ $cart_item_key ]['validation']['required'] = $required;
				$event_attendee_items[ $product_id ]['items'][ $cart_item_key ]['validation']['other']    = $other_validation;

				$event_attendee_items[ $product_id ]['capture_attendees'] = false;

			}

			++$x;
		}

		$capture_attendees = false;

		foreach ( $event_attendee_items as $event_id => $event_item ) {

			$total_event_attendees = 0;

			foreach ( $event_item['items'] as $cart_item_key => $event ) {

				$total_event_attendees += $event['quantity'];

				if ( 'on' === $event['WooCommerceEventsCaptureAttendeeDetails'] ) {

					$event_attendee_items[ $event_id ]['capture_attendees'] = true;

					$capture_attendees = true;

				} elseif ( 'on' === $event['WooCommerceEventsCaptureAttendeeEmail'] ) {

					$event_attendee_items[ $event_id ]['capture_attendees'] = true;

					$capture_attendees = true;

				} elseif ( 'on' === $event['WooCommerceEventsCaptureAttendeeTelephone'] ) {

					$event_attendee_items[ $event_id ]['capture_attendees'] = true;

					$capture_attendees = true;

				} elseif ( 'on' === $event['WooCommerceEventsCaptureAttendeeCompany'] ) {

					$event_attendee_items[ $event_id ]['capture_attendees'] = true;

					$capture_attendees = true;

				} elseif ( 'on' === $event['WooCommerceEventsCaptureAttendeeDesignation'] ) {

					$event_attendee_items[ $event_id ]['capture_attendees'] = true;

					$capture_attendees = true;

				} elseif ( $this->fcaf && $event['fcaf']['status'] ) {

					$event_attendee_items[ $event_id ]['capture_attendees'] = true;

					$capture_attendees = true;

				} elseif ( $this->fb && $event['fb']['status'] && in_array( $event['fb']['view'], array( 'checkout', 'checkoutproduct' ) ) ) {

					$event_attendee_items[ $event_id ]['capture_attendees'] = true;

					$capture_attendees = true;

				}
			}

			$event_attendee_items[ $event_id ]['total_event_attendees'] = $total_event_attendees;

		}

		if ( $capture_attendees ) {

			return $event_attendee_items;

		} else {

			return false;

		}
	}
}
