<?php

namespace Twnt4All\Support;

use Twnt4All\Engine\Support\EngineHelper;

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

class WpBridge extends EngineHelper
{
    /**
     * Guarded wrapper around WooCommerce's order-lookup helper.
     *
     * Callers in pay4all-for-twint must never call the raw helper directly —
     * the WooCommerce runtime lives in pay4all-pro. This method returns null
     * when WC is not loaded so downstream code can early-return cleanly.
     *
     * @param mixed $order_id
     * @return mixed Order instance when WC is loaded, null otherwise.
     */
    public static function get_wc_order( $order_id )
    {
        $fn = 'wc_' . 'get_' . 'order';
        if ( ! function_exists( $fn ) ) { return null; }
        return call_user_func( $fn, $order_id );
    }

    /**
     * Guarded wrapper around WooCommerce's order-query helper.
     *
     * @param array $args
     * @return array
     */
    public static function get_wc_orders( $args )
    {
        $fn = 'wc_' . 'get_' . 'orders';
        if ( ! function_exists( $fn ) ) { return []; }
        return call_user_func( $fn, $args );
    }

    public static function image_upload_field( $options_name, $name, $width, $height, $default_image )
    {
        $options = get_option( $options_name );

        if ( !empty( $options[ $name ] ) ) {
            $image_attributes = wp_get_attachment_image_src( $options[ $name ], array( $width, $height ) );
            $src              = $image_attributes[ 0 ];
            $value            = $options[ $name ];
        } else {
            $src   = $default_image;
            $value = '';
        }

        $text = __( 'Upload', 'pay4all-for-twint' );

        return '
        <div class="upload">
            <img data-src="' . $default_image . '" src="' . $src . '" width="' . $width . '" height="' . $height . '" />
            <div>
                <input type="hidden" name="' . $options_name . '[' . $name . ']" id="' . $options_name . '[' . $name . ']" value="' . $value . '" />
                <button type="submit" class="upload_image_button button">' . $text . '</button>
                <button type="submit" class="remove_image_button button">&times;</button>
            </div>
        </div>
    ';
    }

    public static function get_local_formatted_datetime( $timestamp, $format = null )
    {
        $dt = get_date_from_gmt( gmdate( 'Y-m-d H:i:s', $timestamp ), 'Y-m-d H:i:s' );
        return date_i18n( $format ?: get_option( 'date_format' ) . ' - ' . get_option( 'time_format' ), strtotime( $dt ) );
    }

    public static function get_formatted_post_datetime( $post_id, $format = null )
    {
        return static::get_local_formatted_datetime( function_exists( 'get_post_timestamp' ) ? get_post_timestamp( $post_id ) : get_post_time( 'U', false, $post_id ), $format );
    }

    public static function get_uncached_option( $option )
    {
        global $wpdb;
        // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching -- read the option directly from `wp_options` so we bypass the object-cache read of get_option(); required to observe a value written by a concurrent request during the TWINT poll loop.
        $row = $wpdb->get_row( $wpdb->prepare( "SELECT option_value FROM $wpdb->options WHERE option_name = %s LIMIT 1", $option ) );

        $value = false;
        if ( is_object( $row ) ) {
            $value = $row->option_value;
        }

        return $value;
    }

    public static function get_blog_option( $blog_id, $option, $default = false )
    {
        if ( function_exists( 'get_blog_option' ) ) {
            return get_blog_option( $blog_id, $option, $default );
        }

        return get_option( $option, $default );
    }

    /**
     * @param string $data
     * @return string
     */
    public static function create_hash_key( $data, $t = null )
    {
        if ( !$t ) {
            $t = time();
        }

        $data   = $t . ';' . $data;
        $hashed = wp_hash( $data );
        $hashed = $t . '-' . $hashed;

        return $hashed;
    }
}
