<?php
/**
 * Bookings payment gateway.
 *
 * @package WooCommerce Bookings
 */

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

/**
 * WC_Bookings_Gateway class.
 *
 * Handles payment gateway for bookings.
 *
 * @version 3.8.0
 */
class WC_Bookings_Gateway extends WC_Payment_Gateway {

	/**
	 * Constructor for the gateway.
	 */
	public function __construct() {
		$this->id                 = 'wc-bookings-gateway';
		$this->icon               = '';
		$this->has_fields         = false;
		$this->method_title       = __( 'WooCommerce Bookings', 'woocommerce-bookings' );
		$this->method_description = __( 'Lets customers request a booking that requires confirmation before they pay. Customers are not charged at checkout; you confirm the booking later and the customer pays then.', 'woocommerce-bookings' );
		$this->order_button_text  = __( 'Request Confirmation', 'woocommerce-bookings' );

		$this->init_form_fields();
		$this->init_settings();

		// This gateway only backs the per-product "Requires confirmation" flow and is
		// force-loaded whenever such a booking is in the cart, so it has no enable/disable
		// option of its own and is always considered enabled.
		$this->enabled     = 'yes';
		$this->title       = $this->get_option( 'title' );
		$this->description = $this->get_option( 'description' );

		add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, array( $this, 'process_admin_options' ) );
		add_action( 'woocommerce_thankyou_' . $this->id, array( $this, 'thankyou_page' ) );
	}

	/**
	 * Define the settings shown on the gateway admin screen.
	 */
	public function init_form_fields() {
		$this->form_fields = array(
			'title'       => array(
				'title'       => __( 'Title', 'woocommerce-bookings' ),
				'type'        => 'text',
				'description' => __( 'The label customers see for this payment method during checkout.', 'woocommerce-bookings' ),
				'default'     => __( 'Check booking availability', 'woocommerce-bookings' ),
				'desc_tip'    => true,
			),
			'description' => array(
				'title'       => __( 'Description', 'woocommerce-bookings' ),
				'type'        => 'textarea',
				'description' => __( 'The description customers see for this payment method during checkout.', 'woocommerce-bookings' ),
				'default'     => __( 'Use this payment method to check out and get availability from the shop owner.', 'woocommerce-bookings' ),
				'desc_tip'    => true,
			),
		);
	}

	/**
	 * Check if this gateway is enabled.
	 *
	 * @return bool
	 */
	public function is_available() {
		if ( 'yes' !== $this->enabled ) {
			return false;
		}

		if ( wc_booking_cart_requires_confirmation() ) {
			return true;
		}

		return false;
	}

	/**
	 * Process the payment and return the result
	 *
	 * @param  int $order_id
	 *
	 * @return array
	 */
	public function process_payment( $order_id ) {
		$order = new WC_Order( $order_id );

		// Add custom order note.
		$order->add_order_note( __( 'This order is awaiting confirmation from the shop manager', 'woocommerce-bookings' ) );

		// Remove cart
		WC()->cart->empty_cart();

		// Return thankyou redirect.
		return array(
			'result'   => 'success',
			'redirect' => $this->get_return_url( $order ),
		);
	}

	/**
	 * Output for the order received page.
	 */
	public function thankyou_page( $order_id ) {
		$order = new WC_Order( $order_id );

		if ( 'completed' == $order->get_status() ) {
			echo '<p>' . esc_html__( 'Your booking has been confirmed. Thank you.', 'woocommerce-bookings' ) . '</p>';
		} else {
			echo '<p>' . esc_html__( 'Your booking is awaiting confirmation. You will be notified by email as soon as we\'ve confirmed availability.', 'woocommerce-bookings' ) . '</p>';
		}
	}
}
