import {Dialog, Helper, ThemeManager, Translator, ViewInflater} from "../../../../../js/lib/pwa-lib";

export class ChooseThemeDialog extends Dialog {

    constructor() {
        let viewPromise = ViewInflater.inflate("html/application/dialog/chooseTheme.html").then(view => {

            let template = view.querySelector("#choose-theme-template");
            template.remove();
            template.id = null;

            let themeTemplateContainer = view.querySelector("#theme-choose-container");

            for (let i = 0; i < ThemeManager.themes.length; i++) {
                let themeElem = Helper.cloneNode(template);
                let theme = ThemeManager.themes[i];
                themeElem.querySelector(".name").appendChild(Translator.makePersistentTranslation(theme._name));
                themeElem["dataset"]["theme"] = theme._name;

                themeElem.addEventListener("click", () => {
                    this.result = themeElem["dataset"]["theme"];
                    this.close()
                });
                themeTemplateContainer.appendChild(themeElem);
            }

            return view;
        });

        super(viewPromise, "choose-theme-dialog-title");
    }
}