import {IAdminPanelBus} from '../interfaces/bus/IAdminPanelBus';
import {IFactory as IComponentsFactory} from '../interfaces/components/IFactory';
import {IBox} from '../interfaces/components/IBox';
import { EEvents } from '../types/EEvents';

type TEvents = {
    [e:number]: any[],
}

export class AdminPanelBus implements IAdminPanelBus {

    protected componentsFactory:IComponentsFactory;

    protected eventsListeners:TEvents = {};

    public addEventsListener(event:EEvents, callback:any): void
    {
        if(!this.eventsListeners[event]){
            this.eventsListeners[event] = [];
        }
        this.eventsListeners[event].push(callback);
    }

    public setComponentsFactory(factory:IComponentsFactory)
    {
        this.componentsFactory = factory;
    }

    public toggleSidebar()
    {
        let isCollapsed = this.componentsFactory.getAdminPanel().toggleSidebar();
        for(let i in this.eventsListeners[EEvents.onSidebarToggle] || []){
            this.eventsListeners[EEvents.onSidebarToggle][i](isCollapsed);
        }
    }

    public collapseSidebar()
    {
        this.componentsFactory.getAdminPanel().collapseSidebar();
    }

    public execMessageModal(header:string, message:string)
    {
        let modal = this.componentsFactory.getMessageModal();
        modal.show(header, message);
    }

    public execConfirmModal(header:string, message:string):Promise<any>
    {
        let modal = this.componentsFactory.getConfirmModal();
        return modal.show(header, message);
    }

    public fullscreen(box:IBox): void
    {
        this.componentsFactory.getAdminPanel().getFullScreenContainer().style.height = '100vh';
        box.element.style.height = '100vh';
        this.componentsFactory.getAdminPanel().getFullScreenContainer().append(box.element);
        for(let i in this.eventsListeners[EEvents.onBoxFullScreen] || []){
            this.eventsListeners[EEvents.onBoxFullScreen][i](box);
        }
    }

    public collapsescreen(box:IBox): void
    {
        box.element.removeAttribute('style');
        this.componentsFactory.getAdminPanel().getFullScreenContainer().removeAttribute('style');

        for(let i in this.eventsListeners[EEvents.onBoxCollapsedScreen] || []){
            this.eventsListeners[EEvents.onBoxCollapsedScreen][i](box);
        }
    }

}