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

export default class PaymentGatewaysPageDao implements PaymentGatewaysPageDaoInterface
{

	private _dataSource: DataSourceInterface;

	private _page: number;

	private _size = 10;

	private _filter: string | null = null

	private _paymentGatewaysPage: PaymentGatewaysPage | null = null;

	constructor(
		dataSource: DataSourceInterface,
		page: number,
		size = 10,
		filter: string | null = null
	) {
		this._dataSource = dataSource;
		this._page = page;
		this._size = size;
		this._filter = filter;
	}

	public async getEntity(): Promise<PaymentGatewaysPage | null>
	{
		if (this._paymentGatewaysPage === null) {
			const paymentGatewaysPage = await this._dataSource.gatewaysPage(this._page, this._size, this._filter);
			this._paymentGatewaysPage = paymentGatewaysPage;
		}

		return this._paymentGatewaysPage;
	}

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

	public async getGateways(): Promise<PaymentGateway[] | null>
	{
		const entity = await this.getEntity();
		return entity ? entity.getGateways() : null;
	}

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

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

}
