import {IFactory} from './interfaces/IFactory';
import {IFactory as IComponentsFactory} from './interfaces/components/IFactory';
import {Factory as ComponentsFactory} from './components/Factory';
import {IFactory as ICommandsFactory} from './interfaces/commands/IFactory';
import {Factory as CommandsFactory} from './commands/Factory';
import {IFactory as IObjectInputFactory} from '@common/object-input/interfaces/IFactory';
import {Factory as ObjectInputFactory} from '@common/object-input/Factory';
import {Helpers} from '@common/admin/Helpers';

export class Factory implements IFactory {
    
    protected conf:any;
    
    protected componentsFactory:IComponentsFactory = null;

    protected commandsFactory:ICommandsFactory = null;

    protected objectInputFactory:IObjectInputFactory;

    public init(container:HTMLElement, conf:any = {})
    {
        this.conf = {
            objectInput:{
                factory: null,
            },
            components: {
                factory: null,
            },
            commands: {
                factory: null,
            }
        };
        Helpers.objectReplaceRecursive(this.conf, conf);

        this.objectInputFactory = this.conf.objectInput.factory || new ObjectInputFactory();
        this.objectInputFactory.init(this.conf.objectInput);
        this.getComponentsFactory().init(container, this.conf.components);
    }

    public getObjectInputFactory():IObjectInputFactory
    {
        return this.objectInputFactory;
    }
    
    public getComponentsFactory():IComponentsFactory
    {
        if(this.componentsFactory === null){
            this.componentsFactory = this.conf.components.factory || new ComponentsFactory();
            this.componentsFactory.setAppFactory(this);
        }
        return this.componentsFactory;
    }

    public getCommandsFactory():ICommandsFactory
    {
        if(this.commandsFactory === null){
            this.commandsFactory = this.conf.commands.factory || new CommandsFactory();
            this.commandsFactory.setAppFactory(this);
            this.commandsFactory.init(this.conf.commands);
        }
        return this.commandsFactory;
    }

}