<?php
/**
 * Gateway for Northmill Invoice
 *
 * @package flo-checkout
 */

namespace Flo_Checkout\Gateways;

use Flo_Checkout\Gateways\Helpers\Gateway_Logger_Trait;
use Flo_Checkout\Gateways\Helpers\Interface_Flo_WC_Payment_Gateway;
use Northmill_Online_Shared_Checkout\Northmill_Online_Shared\Data\Payment_Methods;
use WC_Order;
use WC_Payment_Gateway;

/**
 * Pay with Northmill Invoice
 *
 * @class       Moreflo_Invoice_Pay
 * @extends     WC_Payment_Gateway
 * @version     1.0.0
 */
class Moreflo_Invoice_Pay extends WC_Payment_Gateway implements Interface_Flo_WC_Payment_Gateway {
	use Gateway_Logger_Trait;

	/**
	 * Get Gateway ID.
	 *
	 * @return string
	 */
	public static function get_id(): string {
		return Payment_Methods::NMC_MF_INVOICE;
	}

	/**
	 * Whether the gateway supports blocks.
	 *
	 * @return bool
	 */
	public static function has_block_support(): bool {
		return true;
	}

	/**
	 * Public setup.
	 *
	 * @return void
	 */
	public static function public_setup(): void {}

	public function __construct() {
		$this->id                 = self::get_id();
		$this->method_title       = __( 'Northmill Checkout - Flo Invoice', 'flo-checkout-plugin' );
		$this->method_description = __( 'Use invoice payment.', 'flo-checkout-plugin' );
		$this->has_fields         = false;
		$this->supports           = array(
			'products',
		);

		$this->init_form_fields();
		$this->init_settings();
		$this->title       = $this->get_option( 'title' );
		$this->description = $this->get_option( 'description' );

		add_action( 'add_meta_boxes', array( $this, 'add_meta_box' ) );
		add_action( 'save_post', array( $this, 'save_moreflo_invoice_pay' ) );

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

	public function add_meta_box() {
		global $post;

		$wc_order = wc_get_order( $post->ID );
		if ( $wc_order instanceof WC_Order ) {

			$order          = new WC_Order( $post->ID );
			$payment_method = $order->get_payment_method();

			if ( $this->id === $payment_method ) {
				add_meta_box(
					'moreflo_invoice_meta_box',
					__( 'Northmill Flo Invoice Number', 'flo-checkout-plugin' ),
					array( $this, 'moreflo_invoice_pay_form' ),
					'shop_order',
					'side',
					'core'
				);
			}
		}
	}

	public function moreflo_invoice_pay_form() {
		global $post;

		$meta_field_data = get_post_meta( $post->ID, '_moreflo_invoice_number', true ) ? get_post_meta( $post->ID, '_moreflo_invoice_number', true ) : '';

		echo '<input type="hidden" name="moreflo_invoice_meta_field_nonce" value="' . wp_create_nonce() . '">
        <p style="border-bottom:solid 1px #eee;padding-bottom:13px;">
            <input type="text" style="width:250px;";" name="moreflo_invoice" placeholder="' . $meta_field_data . '" value="' . $meta_field_data . '"></p>';
	}

	function save_moreflo_invoice_pay( $post_id ) {

		if ( ! isset( $_POST['moreflo_invoice_meta_field_nonce'] ) ) {
			return $post_id;
		}
		$nonce = $_REQUEST['moreflo_invoice_meta_field_nonce'];

		if ( ! wp_verify_nonce( $nonce ) ) {
			return $post_id;
		}

		if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
			return $post_id;
		}

		if ( 'page' == $_POST['post_type'] ) {

			if ( ! current_user_can( 'edit_page', $post_id ) ) {
				return $post_id;
			}
		} elseif ( ! current_user_can( 'edit_post', $post_id ) ) {

				return $post_id;
		}

		update_post_meta( $post_id, '_moreflo_invoice_number', $_POST['moreflo_invoice'] );
	}

	public function init_form_fields() {

		$this->form_fields = array(
			'enabled'     => array(
				'title'   => __( 'Enable/Disable', 'flo-checkout-plugin' ),
				'type'    => 'checkbox',
				'label'   => __( 'Enable Invoice Payment', 'flo-checkout-plugin' ),
				'default' => 'no',
			),
			'title'       => array(
				'title'       => __( 'Title', 'flo-checkout-plugin' ),
				'type'        => 'text',
				'description' => __( 'This controls the title which the user sees during checkout within the invoice payment type.', 'flo-checkout-plugin' ),
				'default'     => __( 'Pay with invoice', 'flo-checkout-plugin' ),
			),
			'description' => array(
				'title'   => __( 'Description', 'flo-checkout-plugin' ),
				'type'    => 'text',
				'default' => __( 'Pay with invoice', 'flo-checkout-plugin' ),
			),
		);
	}

	public function process_payment( $order_id ) {

		global $woocommerce;
		$order = new WC_Order( $order_id );

		$order->update_status( 'on-hold', __( 'Awaiting invoice payment', 'flo-checkout-plugin' ) );
		$woocommerce->cart->empty_cart();

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

	public function payment_fields() {
		ob_start();
		if ( $this->description ) {
			echo wpautop( wp_kses_post( $this->description ) );
		}

		$fields_html = ob_get_clean();

		echo apply_filters( 'moreflo_checkout_moreflo_invoice_pay_payment_fields', $fields_html );
	}

	public function payment_scripts() {}

	public function validate_fields() {}
}
