import {IFactory} from '../interfaces/bus/IFactory';
import {IFactory as IAppFactory} from '../interfaces/IFactory';
import {IObjectBus} from '../interfaces/bus/IObjectBus';
import {ObjectBus} from './ObjectBus';
import {IFileInputBus} from '../interfaces/bus/IFileInputBus';
import {FileInputBus} from './FileInputBus';
import {TreeSelectBus} from './TreeSelectBus';
import {ITreeSelectBus} from '../interfaces/bus/ITreeSelectBus';
import {Helpers} from '../../admin/Helpers';

export class Factory implements IFactory {

    protected conf:any;
    
    protected appFactory:IAppFactory;

    protected objectBus:IObjectBus = null;

    protected fileInputBus:IFileInputBus = null;

    protected treeSelectBus:ITreeSelectBus = null;

    public init(conf:any): void
    {
        this.conf = {
            objectBus: null,
            fileInputBus: null,
            treeSelectBus: null,
        };
        Helpers.objectReplaceRecursive(this.conf, conf);
    }

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

    public getObjectBus():IObjectBus
    {
        if(this.objectBus === null){
            this.objectBus = this.conf.objectBus || new ObjectBus();
            let componentsFactory = this.appFactory.getComponentsFactory();
            this.objectBus.setComponentsFactory(componentsFactory);
        }
        return this.objectBus;
    }

    public getFileInputBus():IFileInputBus
    {
        if(this.fileInputBus === null){
            this.fileInputBus = this.conf.fileInputBus || new FileInputBus();
            let componentsFactory = this.appFactory.getComponentsFactory();
            this.fileInputBus.setComponentsFactory(componentsFactory);
        }
        return this.fileInputBus;
    }

    public getTreeSelectBus():ITreeSelectBus
    {
        if(this.treeSelectBus === null){
            this.treeSelectBus = this.conf.treeSelectBus || new TreeSelectBus();
            let componentsFactory = this.appFactory.getComponentsFactory();
            this.treeSelectBus.setComponentsFactory(componentsFactory);
        }
        return this.treeSelectBus;
    }

}