import LayoutItem from "../LayoutItem";
import LayoutItemFactory, {LayoutItemImplConfig} from "../LayoutItemFactory";

export default class Html extends LayoutItem{
    public html: string = "";

    constructor(params: Partial<Html> = {}) {
        super(params);
        Object.assign(this, params);
    }

    public placeHtml(container: HTMLElement): void {
        const htmlDiv = document.createElement("div");
        htmlDiv.classList.toggle("pad-hidden", !this.visible);
        htmlDiv.innerHTML = this.html;
        htmlDiv.classList.add("pad-html");
        container.append(htmlDiv);
        this.placedHtml.push(htmlDiv);
    }
}

export function addHtmlToFactory(factory: LayoutItemFactory): void {
    factory.registerConstructor("html", (config, params) => {
        const impl = config.impl;
        if (!isHtmlConfig(impl)) return null;
        const htmlParams: Partial<Html> = params as Partial<Html>;
        htmlParams.html = impl.html;
        return new Html(htmlParams);
    })
}

function isHtmlConfig(config:LayoutItemImplConfig): config is HtmlConfig{
    return config.type === "html";
}

interface HtmlConfig extends LayoutItemImplConfig{
    type: "html";
    html: string;
}