<?php
/**
 * FunnelKit PayPal Express Checkout for FunnelKit Checkout
 *
 * @package FunnelKit\PayPal\Compatibilities\FunnelKit
 */

namespace FKWCSQ\Includes\FunnelKit;

use FKWCSQ\Includes\Modules\Payment\Gateways\Apple_Pay;
use FKWCSQ\Includes\Modules\Payment\Gateways\Google_Pay;
use FKWCSQ\Includes\SmartButtons;
use FKWCSQ\Includes\Traits\Instance;

defined( 'ABSPATH' ) || exit;

/**
 * Square Gpay Express Checkout for FunnelKit Checkout
 *
 * @since 1.0.0
 */
class Checkout {
	use Instance;

	/**
	 * Constructor
	 *
	 * @since 1.0.0
	 */
	public function __construct() {
		add_filter( 'wfacp_smart_buttons', array( $this, 'register_smart_button' ), 99 );
		add_action( 'wfacp_smart_button_container_fkwcsq_square_gpay', array( $this, 'print_smart_button' ) );
		add_action( 'wfacp_smart_button_container_fkwcsq_apple_pay', array( $this, 'print_apple_smart_button' ) );
		add_filter( 'wfacp_template_localize_data', array( $this, 'localize_data' ) );
	}

	public function localize_data( $data ) {
		if ( $this->is_gpay_enabled() ) {
			$data['smart_button_wrappers']['dynamic_buttons']['#wfacp_smart_button_fkwcsq_square_gpay'] = '#wfacp_smart_button_fkwcsq_square_gpay';
		}
		if ( $this->is_apple_pay_enabled() ) {
			$data['smart_button_wrappers']['dynamic_buttons']['#wfacp_smart_button_fkwcsq_apple_pay'] = '#wfacp_smart_button_fkwcsq_apple_pay';
		}

		return $data;
	}

	/**
	 * Register PayPal smart button
	 *
	 * @param array $buttons Existing smart buttons
	 *
	 * @return array Modified smart buttons
	 */
	public function register_smart_button( $buttons ) {

		if ( $this->is_apple_pay_enabled() ) {
			$buttons['fkwcsq_apple_pay'] = array(
				'iframe' => false,
				'name'   => 'funnelkit Apple gpay',
			);
		}
		if ( $this->is_gpay_enabled() ) {

			$buttons['fkwcsq_square_gpay'] = array(
				'iframe' => false,
				'name'   => 'funnelkit square gpay',
			);


		}
		// Remove default checkout button hooks when registering smart button


		$this->remove_default_buttons();

		return $buttons;
	}

	/**
	 * Print PayPal smart button
	 */
	public function print_smart_button() {
		if ( \FKWCSQ\Square::cart_contains_subscription() ) {
			return;
		}
		Google_Pay::get_instance()->render_button();
	}

	/**
	 * Print PayPal smart button
	 */
	public function print_apple_smart_button() {
		if ( \FKWCSQ\Square::cart_contains_subscription() ) {
			return;
		}
		Apple_Pay::get_instance()->render_button();
	}

	/**
	 * Remove default checkout button hooks
	 */
	public function remove_default_buttons() {

		// Remove all default checkout button positions
		remove_action( 'woocommerce_before_checkout_form', array( SmartButtons::get_instance(), 'print_smart_btn_wrapper' ), 15 );
	}


	/**
	 * Check if PayPal Express is enabled
	 *
	 * @return bool
	 */
	public function is_gpay_enabled() {
		if ( ! class_exists( '\WFACP_Core' ) ) {
			return false;
		}

		$gateway = \FKWCSQ\Includes\Modules\Payment\Gateways\Google_Pay::get_instance();

		if ( ! $gateway || $gateway->enabled !== 'yes' ) {
			return false;
		}

		return true;
	}

	/**
	 * Check if PayPal Express is enabled
	 *
	 * @return bool
	 */
	public function is_apple_pay_enabled() {
		if ( ! class_exists( '\WFACP_Core' ) ) {
			return false;
		}

		$gateway = \FKWCSQ\Includes\Modules\Payment\Gateways\Apple_Pay::get_instance();

		if ( ! $gateway || $gateway->enabled !== 'yes' ) {
			return false;
		}

		return true;
	}
}
