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

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


/**
 * Form handler class
 */
class FooEvents_Form {


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

	/**
	 * On class load
	 *
	 * @param array $config configuration values.
	 */
	public function __construct( $config ) {

		$this->config = $config;
	}

	/**
	 * Output text field
	 *
	 * @param string $field_name input field name.
	 * @param string $label input label output.
	 * @param string $field_type input field type.
	 * @param string $required is input required.
	 * @param array  $event event details, cart details.
	 * @param int    $i attendee number.
	 * @param array  $prev_post previously POSTed attendee details.
	 */
	public function text( $field_name, $label, $field_type = 'text', $required = '', $event = array(), $i = 0, $prev_post = array() ) {

		/*echo "<pre>";
			print_r($event);
		echo "</pre>";*/

		$attendees = isset( $event['attendees'] ) ? $event['attendees'] : array();
		$event_id  = $event['id'];

		$value = '';

		if ( isset( $attendees[ $i ][ $field_name ] ) ) {

			$value = esc_attr( $attendees[ $i ][ $field_name ] );

		} elseif ( ! empty( $prev_post ) && isset( $prev_post[ $event['cart_key'] ][ $event_id ][ $i ][ $field_name ] ) ) {

			$value = esc_attr( $prev_post[ $event['cart_key'] ][ $event_id ][ $i ][ $field_name ] );

		}

		require $this->config->template_path . 'attendee/form-text.php';
	}

	/**
	 * Render the copy-details control for an attendee row.
	 *
	 * @param array  $event Event/cart context for the item.
	 * @param int    $i Zero-based attendee index.
	 * @param string $copy_purchaser 'text', 'icon', or 'textandicon'.
	 * @return void
	 */
	public function copy( $event, $i, $copy_purchaser, $event_item = null ) {

		$label    = __( 'Copy attendee details', 'woocommerce-events' );
		$override = get_option( 'WooCommerceEventsCopyOverride', '' );

		if ( is_string( $override ) ) {

			$override = trim( $override );

		}

		if ( ! empty( $override ) && 1 !== (int) $override ) {

			$label = $override;

		}

		require $this->config->template_path . 'attendee/form-copy.php';
	}

	/**
	 * Output a products variation
	 *
	 * @param array $event Event/cart context for the item.
	 */
	public function variation( $event ) {

		$variation = wc_get_product( $event['variation_id'] );

		if ( $variation && $variation->is_type( 'variation' ) ) {

			$variations        = $this->get_readable_variation( $variation );
			$number_variations = count( $variations );

			$x = 1;

			require $this->config->template_path . 'attendee/form-variation.php';

		}
	}

	/**
	 * Get variations in a readable format.
	 *
	 * @param object $variation the variaiton.
	 */
	private function get_readable_variation( $variation ) {

		if ( ! $variation || ! $variation->is_type( 'variation' ) ) {

			return false;

		}

		$attributes = $variation->get_variation_attributes();

		$variations = array();

		foreach ( $attributes as $name => $value ) {

			$variation_name = str_replace( 'attribute_', '', $name );
			$variation_name = str_replace( 'pa_', '', $variation_name );
			$variation_name = str_replace( '_', ' ', $variation_name );
			$variation_name = str_replace( '-', ' ', $variation_name );
			$variation_name = str_replace( 'Pa_', '', $variation_name );
			$variation_name = ucwords( $variation_name );
			$variation_name = urldecode( $variation_name );

			$variation_value = str_replace( '_', ' ', $value );
			$variation_value = str_replace( '-', ' ', $variation_value );
			$variation_value = ucwords( $variation_value );

			$variations[ $variation_name ] = $variation_value;

		}

		return $variations;
	}
}
