<?php

/**
 * Vvveb
 *
 * Copyright (C) 2022  Ziadin Givan
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 *
 */

namespace Vvveb\System;

use Vvveb\System\Cart\Cart;

class Payment {
	/** @var string|null Current payment driver class name. */
	private ?string $driver = null;

	/** @var array<string, array{0: string, 1: array<string, mixed>}> Registered payment methods. */
	private array $methods = [];

	/** @var array<string, PaymentMethod> Instantiated payment method objects. */
	private array $instances = [];

	/** @var PaymentMethod|null The currently active payment method instance. */
	private ?PaymentMethod $instance = null;

	/**
	 * Get singleton instance of the Payment class.
	 *
	 * @param array<string, mixed> $options Payment options.
	 *
	 * @return self
	 */
	public static function getInstance(array $options = []): self {
		static $inst = null;

		if ($inst === null) {
			$inst   = new Payment($options);
		}

		return $inst;
	}

	/**
	 * Constructor.
	 *
	 * @param array<string, mixed> $options Payment options.
	 */
	public function __construct(array $options = []) {
	}

	/**
	 * Get all available payment methods and their data.
	 *
	 * @param array<string, mixed> &$checkoutInfo Checkout information passed by reference.
	 *
	 * @return array<string, array<string, mixed>> Available payment methods with data.
	 */
	public function getMethods(array &$checkoutInfo): array {
		$data = [];

		foreach ($this->methods as $name => $method) {
			list($class, $options)      =  $method;
			$obj                        = new $class(Cart::getInstance(), $options);
			$this->instances[$name]     = $obj;
			$paymentData                = $obj->getMethod($checkoutInfo, $options);
			//if payment method returns false or no data then don't add it to the list
			if ($paymentData) {
				$data[$name] = $paymentData;
			}
		}

		return $data;
	}

	/**
	 * Register a payment method.
	 *
	 * @param string               $method  Method identifier.
	 * @param string               $class   Fully-qualified class name.
	 * @param array<string, mixed> $options Method-specific options.
	 *
	 * @return void
	 */
	public function registerMethod(string $method, string $class, array $options = []): void {
		$options['class']       = $class;
		$this->methods[$method] = [$class, $options];
	}

	/**
	 * Get a specific registered payment method instance by name.
	 * Instantiates the method on demand if it was not already created by getMethods().
	 *
	 * @param string $method Method identifier.
	 *
	 * @return PaymentMethod|null The method instance or null if the method is not registered.
	 */
	public function getMethodInstance(string $method): ?PaymentMethod {
		if (isset($this->instances[$method])) {
			return $this->instances[$method];
		}

		if (isset($this->methods[$method])) {
			list($class, $options) = $this->methods[$method];
			$obj                   = new $class(Cart::getInstance(), $options);
			$this->instances[$method] = $obj;

			return $obj;
		}

		return null;
	}

	/**
	 * Set the active payment method and initialize all methods.
	 *
	 * @param string $method Method identifier.
	 *
	 * @return void
	 */
	public function setMethod(string $method): void {
		foreach ($this->instances as $instance) {
			$instance->init();
		}

		if (isset($this->instances[$method])) {
			$this->instance = $this->instances[$method];
			$this->instance->setMethod();
		}
	}
	
	/**
	 * Authorize the payment using the active method.
	 * authorize an amount on the customer's card
	 *
	 * @param array<string, mixed> &$checkoutInfo Checkout information passed by reference.
	 *
	 * @return bool
	 */
	public function authorize(array &$checkoutInfo = []): bool {
		if ($this->instance) {
			return $this->instance->authorize($checkoutInfo);
		}

		return false;
	}	
	
	/**
	 * Authorize the payment using the active method.
	 * handle return from off-site gateways after authorization
	 *
	 * @param array<string, mixed> &$checkoutInfo Checkout information passed by reference.
	 *
	 * @return bool
	 */
	public function completeAuthorize(array &$checkoutInfo = []): bool {
		if ($this->instance) {
			return $this->instance->completeAuthorize($checkoutInfo);
		}

		return false;
	}	

	
	/**
	 * Capture the payment using the active method.
	 * capture an amount you have previously authorized
	 *
	 * @param array<string, mixed> &$checkoutInfo Checkout information passed by reference.
	 *
	 * @return bool
	 */
	public function capture(array &$checkoutInfo = []): bool {
		if ($this->instance) {
			return $this->instance->capture($checkoutInfo);
		}

		return false;
	}	

	/**
	 * Purchase the payment using the active method.
	 * authorize and immediately capture an amount on the customer's card
	 *
	 * @param array<string, mixed> &$checkoutInfo Checkout information passed by reference.
	 *
	 * @return bool
	 */
	public function purchase(array &$checkoutInfo = []): bool {
		if ($this->instance) {
			return $this->instance->purchase($checkoutInfo);
		}

		return false;
	}	

	/**
	 * completePurchase the payment using the active method.
	 * handle return from off-site gateways after purchase
	 *
	 * @param array<string, mixed> &$checkoutInfo Checkout information passed by reference.
	 *
	 * @return bool
	 */
	public function completePurchase(array &$checkoutInfo = []): bool {
		if ($this->instance) {
			return $this->instance->completePurchase($checkoutInfo);
		}

		return false;
	}
	
	
	/**
	 * Void/Cancel the payment using the active method.
	 * generally can only be called up to 24 hours after submitting a transaction
	 * 
	 * @param array<string, mixed> &$checkoutInfo Checkout information passed by reference.
	 *
	 * @return bool
	 */
	public function void(array &$checkoutInfo = []): bool {
		if ($this->instance) {
			return $this->instance->void($checkoutInfo);
		}

		return false;
	}	

	/**
	 * Refund the payment using the active method.
	 * refund an already processed transaction
	 *
	 * @param array<string, mixed> &$checkoutInfo Checkout information passed by reference.
	 *
	 * @return bool
	 */
	public function refund(array &$checkoutInfo = []): bool {
		if ($this->instance) {
			return $this->instance->refund($checkoutInfo);
		}

		return false;
	}		

	/**
	 * createCard
	 * get a cardReference that can be used for future payments. This might be used in a monthly billing scenario, for example.
	 *
	 * @param array<string, mixed> &$checkoutInfo Checkout information passed by reference.
	 *
	 * @return bool
	 */
	public function createCard(array &$checkoutInfo = []): bool {
		if ($this->instance) {
			return $this->instance->createCard($checkoutInfo);
		}

		return false;
	}	
}
