import { createTabs, importData, wasImportedBefore } from "./lib.js";

export default function renderWelcomeScreen() {
    const system = game.modules.get("dmdave-chain-devil-crypts");
    const moduleId = system.id;
    const title = system.data.title;
    const moduleVersion = system.data.version;
    class WelcomeScreen extends Application {
        static get defaultOptions() {
            const options = super.defaultOptions;
            options.template = `modules/dmdave-chain-devil-crypts/template/welcome-screen.html`;
            options.resizable = false;
            options.width = 480;
            options.height = 790;
            options.classes = ["dmdave-chain-devil-crypts-screen dmdave launcher"];
            options.title = `${title} - Welcome Screen`;

            return options;
        }

        getData() {
            let data = {title: title, moduleVersion: moduleVersion, isUnsupported: game.system.id === "pf2e" || game.system.id === "pf1", hasPreviousImport: wasImportedBefore()}
            return data
        }
        

        activateListeners(html) {
            super.activateListeners(html);
            this.createTabs($('html')[0])
            html.find('.import-data').on('click', async ev => {
                await importData();
                let d = new Dialog({
                    title: "Import Successfull",
                    content: "<p>The adventure was imported and is set up. Do you want to hide Adventure Import window until future update?</p>",
                    buttons: {
                     one: {
                      icon: '<i class="fas fa-check"></i>',
                      label: "Yes",
                      callback: () => {
                        game.settings.set("dmdave-chain-devil-crypts", "imported-template-adventure-1.0.0",true);
                        this.close();
                      }
                     },
                     two: {
                      icon: '<i class="fas fa-times"></i>',
                      label: "No",
                      callback: () => {
                      }
                     }
                    },
                    default: "one"
                   });
                   d.render(true);
            })
            html.find('.dmdave-chain-devil-crypts-screen-show-again').on('change', ev => {
                let val = false;
                if (ev.currentTarget.checked)
                    val = true;
                game.settings.set("dmdave-chain-devil-crypts", "imported-template-adventure-1.0.0",val);
            })
        }

        createTabs(html) {
            const __tabs = new TabsV2({navSelector: ".dmdave-chain-devil-crypts-tabs", contentSelector: ".dmdave-chain-devil-crypts-content", initial: "welcome", active: "welcome"});
            __tabs.bind(html);
        }


        
    }

    (new WelcomeScreen()).render(true);
}