<?php
/**
 * GFB Admin Class
 *
 * @package GFB.
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}

if ( ! class_exists( 'APBGF_WOO_GF_Product_Addon' ) ) {
	/**
	 * Checks if WooCommerce is enabled.
	 */
	class APBGF_WOO_GF_Product_Addon {
		/**
		 * Instance
		 *
		 * @var $instance
		 */
		private static $instance = null;

		/**
		 * Init the Dependencies.
		 */
		private function __construct() {
			add_filter( 'wcgf_gform_field_value', array( $this, 'apbgf_woo_value' ), 10, 4 );

			add_filter('woocommerce_gforms_get_item_data', array( $this, 'apbgf_item_meta' ), 90, 4);

			add_action('woocommerce_gravityforms_entry_created', array( $this, 'apbgf_entry_created' ), 10, 5);

			add_filter( 'woocommerce_gforms_strip_meta_html', array( $this, 'apbgf_to_false' ), 10, 3 );

			add_filter( 'woocommerce_gforms_get_cart_item_total', array( $this, 'apbgf_total_price_callback' ), 99, 2);
		}

		public function apbgf_total_price_callback( $total, $cart_item ) {

			if ( is_array( $cart_item ) && isset( $cart_item['_gravity_form_lead'] ) ) {

				$lead = $cart_item['_gravity_form_lead'];

				$form_meta = RGFormsModel::get_form_meta( $lead['form_id'] );

				foreach ( $form_meta['fields'] as $field ) {
					if ( $field->type == 'apbgf_appointment_calendar' ) {
						$appointment_data = $lead[ $field->id ];
						$cost = (float) $appointment_data['cost'];
						break;
					}
				}
		
				return $total + $cost;
			}

			return $total;
		}

		public function apbgf_to_false( $strip_html, $display_value, $field ) {

			if ( $field['type'] == 'apbgf_appointment_calendar' ) {
				return false;
			}
			return true;
		}

		public function apbgf_entry_created( $entry_id, $order_id, $order_item, $form_data, $lead_data ) {

			$form_meta = RGFormsModel::get_form_meta( $form_data['id'] );
			foreach ( $form_meta['fields'] as $field ) {
				if ( $field->type == 'apbgf_appointment_calendar' ) {
					$_POST[ 'input_' . $field->id ] = $lead_data[ $field->id ];
					gform_update_meta( $entry_id, $field->id, $lead_data[ $field->id ]);
					break;
				}
			}
		}

		public function apbgf_item_meta( $item_data, $field, $lead, $form_meta ) {

			if ( $field->type === 'apbgf_appointment_calendar' ) {

				$value = $lead[ $field->id ];

				if ( is_array( $value ) ) { 
					$html = '<br/>';
					foreach ( $value as $k => $val ) {
						if ( 'location' == $k && 'all' != $val ) {
							// Translators: %1$s is for Location Name
							$html .= sprintf(esc_html__('Location Name: %1$s', 'appointment-and-booking-for-gravity-forms'), get_the_title($val));
							$html .= '<br/>';
						}

						if ( 'service' == $k ) {
							// Translators: %1$s is for Service Name
							$html .= sprintf(esc_html__('Service Name: %1$s', 'appointment-and-booking-for-gravity-forms'), get_the_title($val));
							$html .= '<br/>';
						}

						if ( 'staff' == $k ) {
							// Translators: %1$s is for Staff Name
							$html .= sprintf(esc_html__('Staff Name: %1$s', 'appointment-and-booking-for-gravity-forms'), get_the_title($val));
							$html .= '<br/>';
						}

						if ( 'date' == $k ) {
							$val = apply_filters('apbgf_entry_value_date_format', $val );                     
							// Translators: %1$s is for Appointment Date                       
							$html .= sprintf(esc_html__('Appointment Date: %1$s', 'appointment-and-booking-for-gravity-forms'), $val);
							$html .= '<br/>';
						}

						if ( 'time' == $k ) {
							$val = apply_filters('apbgf_entry_value_time_format', $val );
							if ( isset($value['apbgf_booking_type']) && 'fullday' === $value['apbgf_booking_type'] ) {
								// Translators: %1$s is for Appointment Time
								$html .= sprintf(esc_html__('Appointment Time: %1$s', 'appointment-and-booking-for-gravity-forms'), esc_html__('Fullday', 'appointment-and-booking-for-gravity-forms'));
							} else {
								// Translators: %1$s is for Appointment Time
								$html .= sprintf(esc_html__('Appointment Time: %1$s', 'appointment-and-booking-for-gravity-forms'), $val);      
							}
							$html .= '<br/><br/>';
						}

						if ( 'slot' == $k ) {
							// Translators: %1$s is for Slot Capacity
							$html .= sprintf(esc_html__('Slot Capacity: %1$s', 'appointment-and-booking-for-gravity-forms'), $val);                   
							$html .= '<br/><br/>';
						}

						if ( 'cost' == $k ) {
							// Translators: %1$s is for Total Cost
							$html .= sprintf(esc_html__('Total Cost: %1$s', 'appointment-and-booking-for-gravity-forms'), apbgf_price_format($val));
							$html .= '<br/>';
						}
					}
				}

				$item_data['display'] = $html;
				$item_data['value'] = $lead[ $field->id ];
			}

			return $item_data;
		}

		public function apbgf_woo_value( $value, $product_id, $variation_id, $field ) {

			if ( $field->type == 'apbgf_appointment_calendar' ) {
				$value = rgpost( 'input_' . $field->id );
			}

			return $value;
		}

		/**
		 * Singleton Class Instatnce.
		 */
		public static function get_instance() {
			if ( null === self::$instance ) {
				self::$instance = new APBGF_WOO_GF_Product_Addon();
			}

			return self::$instance;
		}
	}

	APBGF_WOO_GF_Product_Addon::get_instance();
}
