import PaymentGateway from '../../Entity/PaymentGateway/PaymentGateway';
import { DataSourceInterface } from '../../Contract/DataSource/DataSourceInterface';
import { PaymentGatewayDaoInterface } from '../../Contract/Dao/PaymentGateway/PaymentGatewayDaoInterface';

export default class PaymentGatewayDao implements PaymentGatewayDaoInterface
{

	private _dataSource: DataSourceInterface;

	private _name: string;

	private _paymentGateway: PaymentGateway | null = null;

	constructor(dataSource: DataSourceInterface, name: string)
	{
		this._dataSource = dataSource;
		this._name = name;
	}

	public async getEntity(): Promise<PaymentGateway>
	{
		if (this._paymentGateway === null) {
			const paymentGateway = await this._dataSource.findGateway(this._name);

			if (paymentGateway === null) {
				throw 'Entity with name "' + this._name + '" not found.';
			}

			this._paymentGateway = paymentGateway;
		}

		return this._paymentGateway;
	}

	public async getCode(): Promise<string>
	{
		const entity = await this.getEntity();
		return entity ? entity.getCode() : null;
	}

	public async isEnabled(): Promise<boolean>
	{
		const entity = await this.getEntity();
		return entity ? entity.isEnabled() : null;
	}

	public async getId(): Promise<number | string>
	{
		const entity = await this.getEntity();
		return entity ? entity.getId() : null;
	}

	public async getName(): Promise<string>
	{
		const entity = await this.getEntity();
		return entity ? entity.getName() : null;
	}

}
