<?php
namespace ApproveOrders\Modules;

use ApproveOrders\Helper as AOFWC_Helper;
use ApproveOrders\Compatibility\PreOrderBlocksSupport;

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


/**
 * Class PrePayGateway
 *
 * @package ApproveOrders\Modules
 * @since 1.0.0
 */
class PrePayGateway extends \WC_Payment_Gateway {

	/**
	 * Instructions that will be added to the thank you page and emails.
	 *
	 * @var string
	 */
	public $instructions;

	/**
	 * Constructor for the gateway.
	 */
	public function __construct() {

		$this->id                 = 'preordergateway-approve-orders';
		$this->icon               = apply_filters( 'woocommerce_pre_order_icon', '' );
		$this->has_fields         = false;
		$this->method_title       = __( 'WooCommerce Approve Orders', 'approve-orders' );
		$this->method_description = __( 'Allows customers to place orders without making a payment.', 'approve-orders' );

		// Load the settings.
		$this->init_form_fields();
		$this->init_settings();

		// Define user settings variables.
		$this->title        = $this->get_option( 'title', '' );
		$this->description  = $this->get_option( 'description', '' );
		$this->instructions = $this->get_option( 'instructions', '' );
		$this->enabled      = $this->get_option( 'enabled', '' );

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

		// Customer Emails
		add_action( 'woocommerce_email_before_order_table', array( $this, 'email_instructions' ), 10, 3 );
		// Add Prepay Gateway to Block Checkout
		$this->init_blocks_integration();
	}

	/**
	 * Initialize gateway settings form fields
	 */
	public function init_form_fields() {
		$this->form_fields = array(
			'enabled'      => array(
				'title'   => __( 'Enable/Disable', 'approve-orders' ),
				'type'    => 'checkbox',
				'label'   => __( 'Enable Pre Order Payment', 'approve-orders' ),
				'default' => 'yes',
			),
			'title'        => array(
				'title'       => __( 'Title', 'approve-orders' ),
				'type'        => 'text',
				'description' => __( 'This controls the title which the user sees during checkout.', 'approve-orders' ),
				'default'     => __( 'Pre Order', 'approve-orders' ),
				'desc_tip'    => true,
			),
			'description'  => array(
				'title'       => __( 'Description', 'approve-orders' ),
				'type'        => 'textarea',
				'description' => __( 'This controls the description which the user sees during checkout.', 'approve-orders' ),
				'default'     => __( 'Place an order without making a payment. Payment will be processed later.', 'approve-orders' ),
			),
			'instructions' => array(
				'title'       => __( 'Instructions', 'approve-orders' ),
				'type'        => 'textarea',
				'description' => __( 'This controls the description which the user sees during checkout.', 'approve-orders' ),
				'default'     => __( 'Thank you! Your order is pending approval. You will be notified once reviewed.', 'approve-orders' ),
			),
		);
	}

	/**
	 * Thank you oage function fo PrePay Gateway
	 */
	public function thankyou_page() {
		if ( $this->instructions ) {
			echo wp_kses_post( wpautop( wptexturize( $this->instructions ) ) );
		}
	}

	/**
	 * Email instructions for PrePay Gateway
	 *
	 * @param WC_Order $order
	 * @param bool     $sent_to_admin
	 * @param bool     $plain_text
	 *
	 * @since 1.0.0
	 *
	 * @return void
	 */
	public function email_instructions( $order, $sent_to_admin, $plain_text = false ) {
		if ( $this->instructions && ! $sent_to_admin && 'preorder-approve-orders' === $order->get_payment_method() ) {
			echo wp_kses_post( wpautop( wptexturize( $this->instructions ) ) ) . PHP_EOL;
		}
	}

	/**
	 * Process Payment for PrePay Gateway
	 */
	public function process_payment( $order_id ) {

		if ( absint( $order_id ) > 0 ) {
			$order = wc_get_order( $order_id );

			// Mark as Awaiting Approval (we're awaiting the payment)
			AOFWC_Helper::get_instance()->update_order_status( $order_id, 'awaiting-approval' );

			// Reduce stock levels
			wc_reduce_stock_levels( $order_id );

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

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

	/**
	 * Initialize blocks integration.
	 */
	public function init_blocks_integration() {
		// Make sure WC Blocks is loaded first
		if ( ! class_exists( 'Automattic\WooCommerce\Blocks\Payments\Integrations\AbstractPaymentMethodType' ) ) {
			return;
		}

		// Register payment method with blocks
		add_action(
			'woocommerce_blocks_payment_method_type_registration',
			function ( $registry ) {
				if ( ! class_exists( __NAMESPACE__ . '\PreOrderBlocksSupport' ) ) {
					require_once __DIR__ . '/PreOrderBlocksSupport.php';
				}
				$registry->register( new PreOrderBlocksSupport() );
			}
		);

		// Make sure gateway is included in the block checkout frontend
		add_filter(
			'woocommerce_block_cart_and_checkout_payment_methods',
			function ( $payment_method_ids ) {
				if ( in_array( $this->id, WC()->payment_gateways->get_payment_gateway_ids(), true ) ) {
					$payment_method_ids[] = $this->id;
				}
				return array_unique( $payment_method_ids );
			}
		);
	}
}
