import { MessageInterface } from '../communication-interface/MessageInterface';
import { ContactManager } from '../contact-management/ContactManager';
import { LongAsyncRemoteFunction } from '../objectTransport/LongAsyncRemoteFunction';
import { BaseDto, DtoClass, ObjectStore } from './DBAccess';
import { DBFactory } from './DBFactory';

export interface BaseDataStoreClass {
    new(dbFactory: DBFactory, messageInterface: MessageInterface, contactManager: ContactManager): BaseDataStore;
}

export class BaseDataStore {
    private dbFactory: DBFactory;
    private messageInterface: MessageInterface;
    protected contactManager: ContactManager;
    protected _initPromise: Promise<void>;
    private _larfList: LongAsyncRemoteFunction<any>[] = [];

    async getLarfList(): Promise<LongAsyncRemoteFunction<any>[]> {
        await this._initPromise;
        return this._larfList;
    }


    protected init(): Promise<void> {
        return Promise.resolve();
    }

    constructor(dbFactory: DBFactory, messageInterface: MessageInterface, contactManager: ContactManager) {
        this.dbFactory = dbFactory;
        this.messageInterface = messageInterface;
        this.contactManager = contactManager;
        this._initPromise = this.init();
    }

    getLarf<T>(nameSpace: string) {
        const newLarf = new LongAsyncRemoteFunction<T>(this.messageInterface, nameSpace);
        this._larfList.push(newLarf);
        return newLarf;
    }

    async getDao<T extends ObjectStore<BaseDto>>(objectClass: DtoClass): Promise<T> {
        return await this.dbFactory.getDao(objectClass) as T;
    }
}