import {IFactory} from './interfaces/IFactory';
import {IFactory as IComponentsFactory} from './interfaces/components/IFactory';
import {Factory as ComponentsFactory} from './components/Factory';
import {IFactory as IBusFactory} from './interfaces/bus/IFactory';
import {Factory as BusFactory} from './bus/Factory';
import {IFactory as ICommandsFactory} from './interfaces/commands/IFactory';
import {Factory as CommandsFactory} from './commands/Factory';
import {Helpers} from '@common/admin/Helpers';

export class Factory implements IFactory {

    protected conf:any;
    
    protected componentsFactory:IComponentsFactory = null;

    protected busFactory:IBusFactory = null;

    protected commandsFactory:ICommandsFactory = null;

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

        let componentsFactory = this.getComponentsFactory();
        componentsFactory.init(appContainer, this.conf.components);
        document.querySelector('body').onclick = ()=>{
            this.getComponentsFactory().getDirContextMenu().hide();
            this.getComponentsFactory().getItemContextMenu().hide();
        };
    }

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

    public getBusFactory():IBusFactory
    {
        if(this.busFactory === null){
            this.busFactory = this.conf.bus.factory || new BusFactory();
            this.busFactory.setAppFactory(this);
            this.busFactory.init(this.conf.bus);
        }
        return this.busFactory;
    }

    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;
    }

}