import { ReflectiveInjector } from "@angular/core";
import { Events } from "ionic-angular";
import { EventSlots } from "./event-slot";

export abstract class EventsManager {

    private static EVENTS_INSTANCE;

    /**
     * 
     */
    public static getManager(): Events {
        if (!EventsManager.EVENTS_INSTANCE) {
            EventsManager.EVENTS_INSTANCE = ReflectiveInjector.resolveAndCreate([Events]).get(Events);
        }

        return EventsManager.EVENTS_INSTANCE;
    }

    /**
     * 
     * @param name 
     * @param data 
     */
    public static publish(name: string , data?: any): void {
        EventsManager.getManager().publish(name, data);
    }

    /**
     * 
     * @param name 
     */
    public static subscribe(name: string, handler: Function): any {
        EventsManager.getManager().subscribe(name, handler);
    }

    /**
     * 
     * @param name 
     */
    public static unsubscribe(name: string): void {
        EventsManager.getManager().unsubscribe(name);
    }
}