<?php

namespace Sibs\Stargate\Gateways;

use Exception;
use Sibs\Stargate\Config\SibsConfig;
use Sibs\Stargate\Constants\OrderStatus;
use Sibs\Stargate\Constants\PaymentMethods;
use Sibs\Stargate\Constants\PaymentTypes;
use Sibs\Stargate\Constants\WoocommercePaymentMethods;
use Sibs\Stargate\Loggers\SibsLogger;

defined('ABSPATH') || exit;

class Blik extends SibsPaymentGateway
{
    public const METHOD = WoocommercePaymentMethods::BLIK;

    /**
     * Constructor.
     */
    public function __construct()
    {
        $this->id = self::METHOD;
        $this->payment_method = PaymentMethods::BLIK;
        $this->payment_type = PaymentTypes::PURS;
        $this->title = __('BLIK', 'sibs-woo-stg');
        $this->method_title = __('SIBS BLIK', 'sibs-woo-stg');

        parent::__construct();

        $this->icon = SibsConfig::getPluginUrl('assets/images/blik.svg');
    }

    /**
     * SIBS Get Gateway.
     */
    public function get_gateway(): SibsPaymentGateway
    {
        return $this;
    }

    /**
     * SIBS Get Admin Url.
     */
    public function get_admin_url(): string
    {
        return SibsConfig::getBlikURL();
    }

    /**
     * Is Available.
     *
     * @return bool
     */
    public function is_available()
    {
        return parent::is_available();
    }

    /**
     * Get Multi Icon.
     *
     * @return string
     */
    public function get_multi_icon()
    {
        return "<img src=\"{$this->icon}\" alt=\"{$this->title}\" title=\"{$this->title}\"
                style=\"height:40px; max-height:40px; margin:5px 10px 5px 0; float: none; vertical-align: middle; display: inline;\" />";
    }

    /**
     * SIBS: Get Mode.
     *
     * @return string
     */
    public function get_mode()
    {
        return $this->payment_type;
    }

    /**
     * Thank You Page.
     */
    public function sibs_stg_thankyou_page_template(\WC_Order $wc_order)
    {
        try {
            SibsLogger::debug('[sibs_stg_thankyou_page_template] Thank You Payment Order Id: '.$wc_order->get_id());

            $payment_method = $wc_order->get_payment_method();
            $payment_status = $wc_order->get_status();

            if (WoocommercePaymentMethods::BLIK !== $payment_method) {
                throw new Exception('[sibs_stg_thankyou_page_template] Payment Method not BLIK');
            }

            ?>
                <section class="woocommerce-order-details sibs-payment-details-wrapper">
                    <h2 class="sibs-payment-details__title">
                        <?php echo $this->get_multi_icon(); ?>
                        <?php echo esc_html(__('BLIK', 'sibs-woo-stg')); ?>
                    </h2>

                    <p class="sibs-payment-details__notice">
                        <?php echo (OrderStatus::PROCESSING != $payment_status)
                            ? esc_html(__('Your BLIK payment is pending. You will receive an e-mail when your payment status changes.', 'sibs-woo-stg'))
                            : esc_html(__('The payment was finalized using BLIK.', 'sibs-woo-stg'));
            ?>
                    </p>
                </section>
            <?php
        } catch (Exception $e) {
            SibsLogger::error($e->getMessage());

            ?>
                <ul class="sibs-payment-error">
                    <li>
                        <?php echo esc_attr(__('BLIK Error.', 'sibs-woo-stg')); ?>:
                        <strong><?php echo esc_attr(__('Could not retrieve data from BLIK.', 'sibs-woo-stg')); ?></strong>
                    </li>
                </ul>
            <?php
        }
    }

    /**
     * Needs Setup.
     *
     * @return bool
     */
    public function needs_setup()
    {
        return false;
    }
}
