import { IWidgetHandler, WidgetContext } from "@paperbits/common/editing";
import { EventManager, Events } from "@paperbits/common/events";
import { DragSession } from "@paperbits/common/ui/draggables";
import { IContextCommandSet, ViewManager } from "@paperbits/common/ui";
import { WidgetModel } from "@paperbits/common/widgets";
import { PopupInstanceModel } from "./popupModel";


export class PopupHandlers implements IWidgetHandler<PopupInstanceModel> {
    constructor(
        private readonly viewManager: ViewManager,
        private readonly eventManager: EventManager
    ) { }

    public canAccept(dragSession: DragSession): boolean {
        return !["section", "row", "column", "popup"].includes(dragSession.sourceBinding.name);
    }

    public getContextCommands(context: WidgetContext): IContextCommandSet {
        const popupContextualEditor: IContextCommandSet = {
            color: "#4c5866",
            hoverCommands: [],
            selectCommands: [{
                displayName: "Edit popup",
                controlType: "toolbox-button",
                position: "top right",
                color: "#4c5866",
                callback: () => this.viewManager.openWidgetEditor(context.binding)
            },
            {
                controlType: "toolbox-splitter"
            }]
        };

        if (context.model.widgets.length === 0) {
            popupContextualEditor.hoverCommands.push({
                controlType: "toolbox-button",
                color: "#607d8b",
                position: "center",
                iconClass: "paperbits-icon paperbits-simple-add",
                tooltip: "Add widget",
                component: {
                    name: "widget-selector",
                    params: {
                        onRequest: () => context.providers,
                        onSelect: (widget: WidgetModel) => {
                            context.model.widgets.push(widget);
                            context.binding.applyChanges();
                            this.eventManager.dispatchEvent(Events.ContentUpdate);
                            this.viewManager.clearContextualCommands();
                        }
                    }
                }
            });
        }

        return popupContextualEditor;
    }
}