<?php

namespace SugarCalendarBookings\Payments\Gateways\Stripe;

use SugarCalendarBookings\Options;
use SugarCalendarBookings\Payments\Gateways\AbstractPaymentGateway;
use SugarCalendarBookings\Plugin;

/**
 * Stripe payment gateway.
 *
 * Implementation of payment operations for Stripe payments using Connect.
 *
 * @since 1.0.0
 */
class StripePaymentGateway extends AbstractPaymentGateway {

	/**
	 * Gateway ID.
	 *
	 * @since 1.0.0
	 *
	 * @var string
	 */
	const ID = 'stripe';

	/**
	 * Get gateway name.
	 *
	 * @since 1.0.0
	 *
	 * @return string
	 */
	public static function get_name() {

		return esc_html__( 'Stripe', 'sugar-calendar-bookings-scheduling-appointments-lite' );
	}

	/**
	 * Whether gateway is recommended in dashboard.
	 *
	 * @since 1.0.0
	 *
	 * @return bool
	 */
	public static function is_recommended() {

		return true;
	}

	/**
	 * Whether gateway is configured.
	 *
	 * @since 1.0.0
	 *
	 * @return bool
	 */
	public function is_configured() {

		$credentials = $this->get_credentials();

		return (
			! empty( $credentials['publishable_key'] ) &&
			! empty( $credentials['secret_key'] ) &&
			! empty( $credentials['user_id'] )
		);
	}

	/**
	 * Get credentials.
	 *
	 * @since 1.0.0
	 *
	 * @return array
	 */
	public function get_credentials() {

		$options   = Plugin::get( Options::class );
		$test_mode = $options->get( 'stripe.test_mode' );

		if ( $test_mode ) {
			$publishable_key = $options->get( 'stripe.test_publishable_key' );
			$secret_key      = $options->get( 'stripe.test_secret_key' );
			$user_id         = $options->get( 'stripe.test_user_id' );
		} else {
			$publishable_key = $options->get( 'stripe.live_publishable_key' );
			$secret_key      = $options->get( 'stripe.live_secret_key' );
			$user_id         = $options->get( 'stripe.live_user_id' );
		}

		return [
			'publishable_key' => $publishable_key,
			'secret_key'      => $secret_key,
			'user_id'         => $user_id,
		];
	}

	/**
	 * Get gateway settings instance.
	 *
	 * @since 1.0.0
	 *
	 * @return null|StripePaymentGatewaySettings
	 */
	public function get_settings() {

		return Plugin::get( StripePaymentGatewaySettings::class );
	}

	/**
	 * Get payment processor instance.
	 *
	 * @since 1.0.0
	 *
	 * @return StripePaymentProcessor
	 */
	public function get_processor() {

		return Plugin::get( StripePaymentProcessor::class );
	}
}
