<?php

namespace Hyperpay\Gateways\Brands;

use Hyperpay\Gateways\App\DefaultGateway;
use Hyperpay\Gateways\Helpers\View;
use WC_Order;

class GooglePay extends DefaultGateway
{

  public $trans_mode = 'EXTERNAL';

  /**
   * should be lower case and unique
   * @var string $id 
   */
  public $id = "hyperpay_googlepay";

  /**
   * The title which appear next to gateway on setting page 
   * @var string $method_title
   */
  public $method_title = "Google Pay";

  public $merchant_id = "";

  /**
   * Description of gateways which will appear next to title
   * @var string $method_description
   */
  public $method_description = "Google Pay Plugin for Woocommerce";


  /**
   * the Brands supported by the gateway
   * @var array $supported_brands
   */
  protected $supported_brands = [
    "GOOGLEPAY" => "Google Pay",
  ];


  public function boot()
  {
    $extra_fields = [];
    $extra_fields['merchant_id'] = [
      'title' => __('Merchant ID', 'hyperpay-gateways'),
      'type' => 'text',
      'default' => ''
    ];


    $this->form_fields = array_merge(array_slice($this->form_fields, 0, 7), $extra_fields, array_slice($this->form_fields, 7));

    $this->merchant_id = $this->get_option('merchant_id');
  }

  public function renderPaymentForm(WC_Order $order, $result)
  {


    $token = $result['response']['id'];
    $postBackURL = $result['postBackURL'];

    $payment_brands = $this->brands;

    if (is_array($this->brands))
      $payment_brands = implode(' ', $this->brands);


    $dataObj = [
      'is_arabic' => esc_js($this->is_arabic),
      'style' => esc_html($this->payment_style),
      'postBackURL' => ($postBackURL),
      'payment_brands' => esc_html($payment_brands),
      'custom_style' => esc_html($this->custom_style),
      'scriptURL' => esc_html($this->script_url),
      'checkoutId' => $token,
      'integrity' => esc_html($result['integrity'] ?? ''),
      'orderId' => $order->id,
      'nonce' => $this->NONCE,
      'id' => $this->id,
      'entityId' => $this->entityId,
      'merchantId' => $this->merchant_id
    ];


    if ($this->supported_network) {
      $dataObj['supported_network'] = $this->supported_network;
    }


    add_action('wp_head', [$this, 'custom_add_to_head']);

    $scriptSrc =  "googlepay-script.js";

    return View::render('copy-and-pay.html', compact('dataObj', 'scriptSrc'));
  }
}
