<?php

namespace FKWCSQ\Includes\Conversions;

use FKWCSQ\Includes\Traits\Instance;

class Controller {
	use Instance;

	/**
	 * @var array
	 */
	protected $registry = array();
	protected $payment_method = '';
	/**
	 * @var \WC_Order
	 */
	protected $renewal_order = '';
	/**
	 * @var \WC_Subscription
	 */
	protected $subscription = '';

	/**
	 * Constructor
	 */
	public function __construct() {
		$this->register_converters();
		$this->init_hooks();
	}

	public function init_hooks() {
		// Payment method conversion
		add_filter( 'woocommerce_order_get_payment_method', array( $this, 'convert_payment_method' ), 11, 2 );
		add_filter( 'woocommerce_subscription_get_payment_method', array( $this, 'convert_payment_method' ), 11, 2 );
	}

	public function set_data( $renewal_order, $subscription ) {
		$this->renewal_order = $renewal_order;
		$this->subscription  = $subscription;
	}

	/**
	 * Register gateway converters
	 */
	protected function register_converters() {
		$this->registry['square_credit_card'] = new WCSquare();
	}


	/**
	 * Convert payment method
	 *
	 * @param string $payment_method
	 * @param \WC_Order $order
	 *
	 * @return string
	 * @since 1.0.0
	 */
	public function convert_payment_method( $payment_method ) {
		if ( true === $this->maybe_prevent_change_method() ) {
			return $payment_method;
		}
		// Map of old gateway IDs to our gateway ID
		$gateway_map = array(
			'square_credit_card' => 'fkwcsq_square',
		);

		if ( isset( $gateway_map[ $payment_method ] ) ) {
			$this->payment_method = $payment_method;


			return $gateway_map[ $payment_method ];
		}

		return $payment_method;
	}


	/**
	 * Determines whether to prevent changing the payment method.
	 *
	 * This method checks if the current action is a scheduled subscription payment
	 * and whether the Square payment gateway is not configured. If both conditions
	 * are met, it returns true to prevent changing the payment method.
	 *
	 * @since 1.0.0
	 *
	 * @return bool True if the payment method change should be prevented, false otherwise.
	 */
	private function maybe_prevent_change_method() {

		if ( current_action() === 'woocommerce_scheduled_subscription_payment' && ! \FKWCSQ\Square::is_configured() ) {
			return true;
		}
		
		return false;
	}

	public function get_payment_token() {
		return $this->registry[ $this->payment_method ]->get_payment_token( $this->renewal_order, $this->subscription );
	}

	public function get_token_source() {

		if ( ! isset( $this->registry[ $this->payment_method ] ) ) {
			return array();
		}

		return array(
			'token'       => $this->get_payment_token(),
			'customer_id' => $this->get_customer_id(),
			'location_id' => $this->get_location_id(),
		);
	}

	public function get_customer_id() {
		return $this->registry[ $this->payment_method ]->get_customer_id( $this->renewal_order, $this->subscription );
	}


	public function get_location_id() {
		return $this->registry[ $this->payment_method ]->get_location_id( $this->renewal_order, $this->subscription );
	}

	public function get_payment_method() {
		return $this->payment_method;
	}
}
