<?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;
use Sibs\Stargate\Utils\SibsBrowser;

defined('ABSPATH') || exit;

class Xpay extends SibsPaymentGateway
{
    public const METHOD = WoocommercePaymentMethods::XPAY;

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

        parent::__construct();

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

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

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

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

        $browser = new SibsBrowser();
        if (SibsBrowser::BROWSER_SAFARI == $browser->getBrowser()) {
            $is_available = false;
        }

        return $is_available;
    }

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

        $apayIcon = SibsConfig::getPluginUrl('assets/images/apay.svg');
        $icon_html .= "<img src=\"{$apayIcon}\" alt=\"{$this->title}\" title=\"{$this->title}\"
                style=\"height:40px; max-height:40px; margin:5px 10px 5px 0; float: none; vertical-align: middle; display: inline;\" />";

        return $icon_html;
    }

    /**
     * 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('Thank You Payment Order Id: '.$wc_order);

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

            if (WoocommercePaymentMethods::XPAY !== $payment_method) {
                throw new Exception('[sibs_stg_thankyou_page_template] Payment Method not Google Pay / Apple Pay');
            }

            ?>
                <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(__('Google Pay / Apple Pay', 'sibs-woo-stg')); ?>
                    </h2>

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

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

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