import {IFactory} from '../interfaces/components/IFactory';
import {IFactory as IAppFactory} from '../interfaces/IFactory';
import {IAppContainer} from '../interfaces/components/IAppContainer';
import {AppContainer} from './AppContainer';
import {IAdminForm} from '../interfaces/components/IAdminForm';
import {AdminForm} from './AdminForm';
import {Helpers} from '@common/admin/Helpers';

export class Factory implements IFactory {

    protected appFactory:IAppFactory;

    protected appContainer:IAppContainer = null;

    protected conf:any = {};

    public setAppFactory(factory:IAppFactory): void
    {
        this.appFactory = factory;
    }

    public getAppContainer(): IAppContainer
    {
        return this.appContainer;
    }

    public init(container:HTMLElement, conf:any = {}): void
    {
        this.conf = {
            'appContainer': null,
            'adminForm': null,
        }
        Helpers.objectReplaceRecursive(this.conf, conf);

        this.appContainer = this.conf.appContainer || new AppContainer();
        let adminForm = this.createAdminForm();
        this.appContainer.setAdminForm(adminForm);
        this.appContainer.init(container);
    }

    protected createAdminForm(): IAdminForm
    {
        let form = this.conf.adminForm || new AdminForm();
        let appCommands = this.appFactory.getCommandsFactory().getAppCommands();
        form.setAppCommands(appCommands);
        return form;
    }

}