<?php

namespace Svea_Webpay_For_Woocommerce;

use Svea_Webpay_For_Woocommerce\Gateways\WC_Gateway_Svea_Part_Pay;

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

/**
 * Class to handle functions related to wp-admin
 */
class Admin_Functions {

	/**
	 * Nonce for the cancel action, used to authorize
	 * requests
	 *
	 * @var     string
	 */
	const CANCEL_NONCE = 'svea_cancel_sec';

	/**
	 * Nonce for the credit action, used to authorize
	 * requests
	 *
	 * @var     string
	 */
	const CREDIT_NONCE = 'svea_credit_sec';

	/**
	 * Nonce for the deliver action, used to authorize
	 * requests
	 *
	 * @var     string
	 */
	const DELIVER_NONCE = 'svea_deliver_sec';

	/**
	 * Initialize the class
	 *
	 * @return void
	 */
	public function init() {
		add_action( 'admin_notices', [ $this, 'display_admin_notices' ] );
		add_action( 'woocommerce_attribute_label', [ $this, 'label_order_item_meta' ], 20, 2 );
		add_action( 'woocommerce_admin_order_data_after_billing_address', [ $this, 'admin_display_svea_order_id' ] );

		add_filter( 'woocommerce_hidden_order_itemmeta', [ $this, 'hide_order_item_meta' ], 10, 1 );

		// Part payment widget
		add_action( 'init', [ $this, 'product_part_payment_widget' ], 11, 1 );
	}

	/**
	 * Hook the part payment widget function on the part payment gateway
	 *
	 * @return void
	 */
	public function product_part_payment_widget() {
		$wc_gateway_part_pay = WC_Gateway_Svea_Part_Pay::init();

		$product_widget_position = intval( $wc_gateway_part_pay->get_option( 'product_widget_position' ) );

		if ( $product_widget_position <= 0 ) {
			$product_widget_position = 11;
		}

		add_action( 'woocommerce_single_product_summary', [ $wc_gateway_part_pay, 'product_part_payment_widget' ], $product_widget_position, 1 );
	}

	/**
	 * Hide Svea order meta from visitors
	 *
	 * @param array $hidden_meta
	 *
	 * @return array
	 */
	public function hide_order_item_meta( $hidden_meta ) {
		$hidden_meta[] = 'svea_order_number';
		$hidden_meta[] = 'svea_order_id';
		$hidden_meta[] = 'svea_address_selector';

		$hidden_meta[] = 'svea_iv_billing_ssn';
		$hidden_meta[] = 'svea_iv_billing_customer_type';
		$hidden_meta[] = 'svea_iv_billing_org_number';
		$hidden_meta[] = 'svea_iv_billing_initials';
		$hidden_meta[] = 'svea_iv_billing_vat_number';
		$hidden_meta[] = 'svea_iv_birth_date_year';
		$hidden_meta[] = 'svea_iv_birth_date_month';
		$hidden_meta[] = 'svea_iv_birth_date_day';

		return $hidden_meta;
	}

	/**
	 * Display the svea order id in the backend whilst viewing an order processed
	 * through svea
	 *
	 * @param   WC_Order $wc_order
	 * @return  void
	 */
	public function admin_display_svea_order_id( $wc_order ) {
		$svea_order_id = $wc_order->get_meta( '_svea_order_id' );

		/**
		 * Only display the svea order id if this order was processed
		 * through svea
		 */
		if ( ! $svea_order_id || strlen( $svea_order_id ) <= 0 ) {
			return;
		}

		?>
		<div class="order_data_column">
			<div class="address">
				<p>
					<strong><?php esc_html_e( 'Svea Order Id', 'svea-webpay-for-woocommerce' ); ?></strong>
					#<?php echo esc_html( $svea_order_id ); ?>
				</p>
			</div>
		</div>
		<?php
	}

	/**
	 * Makes the labels of order item meta for
	 *
	 * @return string
	 */
	public function label_order_item_meta( $label, $meta_key ) {
		if ( $meta_key === 'svea_delivered' ) {
			return esc_html__( 'Delivered in Svea', 'svea-webpay-for-woocommerce' );
		} else if ( $meta_key === 'svea_credited' ) {
			return esc_html__( 'Credited in Svea', 'svea-webpay-for-woocommerce' );
		}

		return $label;
	}

	/**
	 * Display admin notices saved in the cache.
	 *
	 * @return  void
	 */
	public function display_admin_notices() {
		$notices = get_option( 'sveawebpay_deferred_admin_notices' );

		if ( ! $notices ) {
			return;
		}

		foreach ( $notices as $notice ) {
			echo '<div class="' . esc_attr( $notice['type'] ) . '"><p>' . wp_kses_post( $notice['message'] ) . '</p></div>';
		}

		delete_option( 'sveawebpay_deferred_admin_notices' );
	}
}
