<?php
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

/**
 * @return void
 */
function elementsell_render_billing_admin_notices() {
    if ( ! current_user_can( 'manage_options' ) ) {
        return;
    }

    if ( empty( $_GET['page'] ) || $_GET['page'] !== 'elementsell' ) {
        return;
    }

    if ( isset( $_GET['elementsell_billing_error'] ) ) {
        echo '<div class="notice notice-error elementsell-notice"><p>' . esc_html( wp_unslash( (string) $_GET['elementsell_billing_error'] ) ) . '</p></div>';
    }
}

add_action( 'admin_notices', 'elementsell_render_billing_admin_notices' );

/**
 * @return void
 */
function elementsell_render_billing_section() {
    if ( ! current_user_can( 'manage_options' ) ) {
        return;
    }

    $has_card       = elementsell_billing_has_saved_card();
    $is_blocked     = elementsell_is_billing_blocked();
    $payment_failed = elementsell_billing_is_payment_failed();
    $card_url       = elementsell_billing_get_token_start_url();
    $needs_gateway  = $payment_failed
        || ( $is_blocked && ! $has_card )
        || ( ! $has_card && elementsell_billing_are_invoice_details_complete() );
    $show_debug     = isset( $_GET['elementsell_billing_debug'] );
    $probe          = null;

    if ( ( $needs_gateway || $show_debug ) && function_exists( 'elementsell_billing_probe_credentials' ) ) {
        $probe = elementsell_billing_probe_credentials();
    }

    ?>
    <div class="elementsell-billing-card">
        <h2>Platební karta</h2>

        <?php if ( is_array( $probe ) && empty( $probe['ok'] ) && ( $needs_gateway || $show_debug ) ) : ?>
            <div class="notice notice-error" style="margin:12px 0;">
                <p>
                    <strong>Platba kartou teď nejde připojit.</strong>
                    <?php echo esc_html( (string) $probe['message'] ); ?>
                    <?php if ( $show_debug ) : ?>
                        <br><small>token_len=<?php echo (int) $probe['token_len']; ?>, http=<?php echo (int) $probe['http']; ?>, token_md5=<?php echo esc_html( (string) $probe['token_md5'] ); ?></small>
                    <?php endif; ?>
                </p>
            </div>
        <?php endif; ?>

        <?php if ( $payment_failed ) : ?>
            <div class="elementsell-billing-alert elementsell-billing-alert--error">
                Poplatek za ElementSell se nepodařilo strhnout. Uložte kartu znovu.
            </div>
            <p>
                <a class="button button-primary elementsell-billing-button" href="<?php echo esc_url( $card_url ); ?>">Uložit platební kartu</a>
            </p>
        <?php elseif ( $is_blocked && ! $has_card ) : ?>
            <div class="elementsell-billing-alert elementsell-billing-alert--warning">
                Uložte platební kartu. Bez ní ElementSell nepůjde dál používat.
            </div>
            <p>
                <a class="button button-primary button-hero elementsell-billing-button" href="<?php echo esc_url( $card_url ); ?>">Uložit platební kartu</a>
            </p>
        <?php elseif ( $has_card ) : ?>
            <p class="elementsell-billing-success">
                <span class="dashicons dashicons-yes-alt"></span>
                Platební karta je uložena.
                <a href="<?php echo esc_url( $card_url ); ?>">Změnit</a>
            </p>
        <?php else : ?>
            <?php if ( ! elementsell_billing_are_invoice_details_complete() ) : ?>
                <p class="elementsell-billing-note elementsell-billing-note--warning">
                    Nejdřív vyplňte fakturační údaje výše. Pak zde uložíte kartu (ověření 1 Kč přes ThePay).
                </p>
            <?php else : ?>
                <p>
                    <a class="button button-secondary elementsell-billing-button" href="<?php echo esc_url( $card_url ); ?>">Uložit platební kartu</a>
                </p>
            <?php endif; ?>
        <?php endif; ?>
    </div>
    <?php
}
