<?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 Card extends SibsPaymentGateway
{
    public const METHOD = WoocommercePaymentMethods::CARD;

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

        parent::__construct();

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

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

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

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

    /**
     * SIBS: Get Multi Icon.
     *
     * @return string
     */
    public function get_multi_icon()
    {
        $icons_html = '';

        $payment_setting = SibsConfig::getPaymentOption($this->id);
        $cards = $payment_setting['card_types'];

        if (!empty($cards)) {
            foreach ($cards as $card) {
                $method = strtolower($card);
                $icon = SibsConfig::getPluginUrl("assets/images/{$method}.svg");
                $icons_html .= "<img src=\"{$icon}\" alt=\"{$method}\" title=\"{$method}\" 
                    style=\"height:40px; max-height:40px; margin:5px 10px 5px 0; float: none; vertical-align: middle; display: inline;\" />";
            }
        } else {
            $icons_html = "<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;\" />";
        }

        return $icons_html;
    }

    /**
     * SIBS: Get Mode.
     *
     * @return string
     */
    public function get_mode()
    {
        return SibsConfig::getCreditCardTransactionMode();
    }

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

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

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

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

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

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

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