import {Helper, InitPromise} from "../../../../../js/lib/pwa-lib";
import {UserAction, UserSite} from "../../../../../js/lib/pwa-user-management";
import {DataManager} from "../../../../../js/lib/pwa-core";

export class SelectWordsSite extends UserSite{

    constructor(siteManager) {
        super(siteManager, "version/2/html/selectWords.html", null, "select-words");
    }

    async onConstruct(args) {
        let res = await super.onConstruct(args);
        this.stats = (await DataManager.load("words"))["result"];
        this.words = this.stats["wordsToCheck"];
        console.log(this.stats);
        return res;
    }

    onFirstStart() {
        super.onFirstStart();
        this.findBy("#not-checked").appendChild(document.createTextNode(this.stats["wordsNotChecked"]));
        this.findBy("#checked").appendChild(document.createTextNode(this.stats["wordsChecked"]));
        this.findBy("#not-sure").appendChild(document.createTextNode(this.stats["wordsUnsure"]));
        this.findBy("#deleted").appendChild(document.createTextNode(this.stats["wordsDeleted"]));
        this.findBy("#unused").appendChild(document.createTextNode(this.stats["wordsNotUsed"]));

        let template = this.findBy("#word-template");
        template.id = null;
        template.remove();

        let container = this.findBy("#word-container");

        let numWords = this.words.length;
        for (let i = 0; i < numWords; i++) {
            let wordElement = Helper.cloneNode(template);
            wordElement.dataset["id"] = -1;
            this.setWord(wordElement, this.words[i]);
            container.appendChild(wordElement);

            wordElement.querySelector(".button-ok").addEventListener("click", async () => {
                let newWord = (await DataManager.send("checkWord", {
                    "wordId":wordElement.dataset["id"],
                    "action":"1"
                }))["result"];
                this.setWord(wordElement, newWord[0]);
            });

            wordElement.querySelector(".button-unsure").addEventListener("click", async () => {
                let newWord = (await DataManager.send("checkWord", {
                    "wordId":wordElement.dataset["id"],
                    "action":"2"
                }))["result"];
                this.setWord(wordElement, newWord[0]);
            });

            wordElement.querySelector(".button-delete").addEventListener("click", async () => {
                let newWord = (await DataManager.send("checkWord", {
                    "wordId":wordElement.dataset["id"],
                    "action":"3"
                }))["result"];
                this.setWord(wordElement, newWord[0]);
            });
        }
    }

    setWord(wordElement, word){
        wordElement.querySelector(".word").removeAllChildren().appendChild(document.createTextNode(word["word"]));
        wordElement.dataset["id"] = word["id"];
    }
}

InitPromise.addPromise(app => {
    app.addDefaultAction(new UserAction("select-words", () => {
        app.startSite(SelectWordsSite);
    }, null, null, "select-words"));
});