<?php

namespace WeDevs\AppBridge\Integrations\WooCommerce;

use WeDevs\AppBridge\Core\Contracts\Hookable;
use WeDevs\AppBridge\Core\Auth\JwtAuth;

/**
 * Class Dokan_App_Payment
 *
 * @author weDevs
 */
class PaymentHandler implements Hookable {

    /**
     * Dokan_App_Payment constructor.
     */
    public function register_hooks(): void {
        add_action( 'woocommerce_api_dokan-app-payment', [ $this, 'handle_app_payment' ] );
        // Append custom parameter to the checkout order received URL.
        add_filter( 'woocommerce_get_checkout_order_received_url', [ $this, 'append_custom_param_to_order_received_url' ], 10, 1 );
        // Enqueue custom styles for mobile device based on for-mobile parameter on url.
        add_action( 'wp_enqueue_scripts', [ $this, 'enqueue_custom_styles' ] );
    }

    /**
     * Handle app payment
     *
     * @return void
     */
    public function handle_app_payment() {
        $jwt_auth = new JwtAuth();
        $token = $jwt_auth->validate_token_for_request(null, $_REQUEST['dokan_app_token']); // phpcs:ignore

        if ( is_wp_error( $token ) ) {
            echo 'Error in authentication';
            exit;
        }

        wp_clear_auth_cookie();
        wp_set_current_user( $token->data->user->id );

        if ( is_ssl() === true ) {
            wp_set_auth_cookie( $token->data->user->id, true, true );
        } else {
            wp_set_auth_cookie( $token->data->user->id, true, false );
        }

        $order_id = $_GET['order-id']; // phpcs:ignore

        $order = wc_get_order( $order_id );

        if ( ! $order ) {
            echo 'Error in order';
            exit();
        }

        $order_payment_url = $order->get_checkout_payment_url();

        // Append the 'for-mobile' parameter to the payment URL.
        $url = add_query_arg( 'for-mobile', 'true', $order_payment_url );

        // Redirect to the modified URL.
        wp_safe_redirect( $url );

        exit();
    }

    /**
     * Append custom parameter to the checkout order received URL
     *
     * @param string $url
     *
     * @return string
     */
    public function append_custom_param_to_order_received_url( $url ) {
        // Unsanitized URL may contain slashes, so we remove them first
        $url = esc_url_raw( $url );

        // Append the custom parameter
        $final_url = add_query_arg( 'order-received-url', 'true', $url );

        return $final_url;
    }

    /**
     * Enqueue custom styles for mobile device based on for-mobile parameter on URL
     *
     * @return void
     */
    public function enqueue_custom_styles() {
        // Check if the 'for-mobile' parameter is set and equals 'true'
        if ( empty( $_GET['for-mobile'] ) || sanitize_text_field( wp_unslash( $_GET['for-mobile'] ) ) !== 'true' ) { // phpcs:ignore
            return;
        }

        wp_enqueue_style(
			'mobile-hide',
			APP_BRIDGE_ASSETS . '/css/payment-for-app-bridge.css',
			[],
	        APP_BRIDGE_VERSION,
        );
    }
}
