<?php

/**
* @package CembraPay Payment Gateway for WooCommerce
* @author  Adrian Emil Tudorache
* @license GPL-2.0+
* @link    https://www.tudorache.me/
**/

if ( ! defined( 'ABSPATH' ) ) {
    header( 'Status: 403 Forbidden' );
	header( 'HTTP/1.1 403 Forbidden' );
    exit;
}

use \Automattic\WooCommerce\Blocks\Payments\Integrations\AbstractPaymentMethodType;
use \CembraPayWoo\Service;

/**
 * Dummy Payments Blocks integration
 *
 * @since 1.0.3
 */
final class WC_Gateway_CembraPay_Blocks extends AbstractPaymentMethodType {

	/**
	 * The gateway instance.
	 *
	 * @var WC_Gateway_CembraPay_Blocks
	 */
	private $gateway;

	/**
	 * Payment method name/id/slug.
	 *
	 * @var string
	 */
	protected $name = 'cembrapay';

	/**
	 * Initializes the payment method type.
	 */
	public function initialize() {
		$this->settings = Service::get_payment_gateway_settings();
	}

    /**
     * Get gateway instance (lazy loading)
     */
    private function get_gateway() {

        if ( ! $this->gateway ) {
            $gateways = WC()->payment_gateways->payment_gateways();
            if ( isset( $gateways[ $this->name ] ) ) {
                $this->gateway = $gateways[ $this->name ];
            }
        }

        return $this->gateway;

    }

    public function register_assets() {

        wp_enqueue_style(
            'cembrapay-blocks',
            CEMBRAURL . 'assets/css/frontend/blocks.css',
            [],
        );

        $script_path       = 'assets/build/frontend/blocks.js';
		$script_asset_path = CEMBRAPATH . 'assets/build/frontend/blocks.asset.php';
		$script_asset      = file_exists( $script_asset_path )
			? require( $script_asset_path )
			: array(
				'dependencies' => array(),
				'version'      => ''
			);
		$script_url        = CEMBRAURL . $script_path;

        $style_path       = 'assets/build/frontend/blocks.js';

		wp_register_script(
			'cembrapay-blocks',
			$script_url,
			$script_asset[ 'dependencies' ],
			$script_asset[ 'version' ],
			true
		);

        $locale = get_locale();
        $locale = explode('_', $locale);
        $date_format = get_option( 'date_format' );
        wp_localize_script(
            'cembrapay-blocks',
            'cembrapayBlocks',
            array(
                'locale'      => $locale[0] ?? 'en',
                'fieldLabels' => CembraPayWoo\Service::get_field_labels(),
                'dateFormat'  => $date_format,
                'defaultBirthdate' => date( $date_format, strtotime( '1990-01-01' ) ),
                'logo'        => CEMBRAURL . 'assets/img/CembraPay.gif',
            )
        );

		if ( function_exists( 'wp_set_script_translations' ) ) {
			wp_set_script_translations( 'cembrapay-blocks', 'cembrapay', CEMBRAPATH . 'languages/' );
		}

    }

	/**
	 * Returns if this payment method should be active. If false, the scripts will not be enqueued.
	 *
	 * @return boolean
	 */
	public function is_active() {
		$gateway = $this->get_gateway();
        return $gateway ? $gateway->is_available() : false;
	}

	/**
	 * Returns an array of scripts/handles to be registered for this payment method.
	 *
	 * @return array
	 */
	public function get_payment_method_script_handles() {
		$this->register_assets();
		return [ 'cembrapay-blocks' ];
	}

	/**
	 * Returns an array of key=>value pairs of data made available to the payment methods script.
	 *
	 * @return array
	 */
	public function get_payment_method_data() {
        return $this->settings;
	}
}
