//!wrt $BSPEC:{"icn":"C:/local/3dfe/icon.png","cpr":"Copyright (C) Windows 96 Team 2021.","dsc":"Explore your files in 3D!","frn":"3D File Explorer","ver":1,"ssy":"gui"}

const { Theme } = w96.ui;

/**
 * Main 3D file explorer application.
 */
class FE3DApplication extends WApplication {
    constructor() {
        super();

        // Registered blobs registry
        this.registeredBlobs = [];
    }

    /**
     * Main entry point for File Explorer 3D.
     * @param {String[]} argv Arguments.
     */
    async main(argv) {
        await super.main(argv);
        
        // The path to be browsed by 3DFE
        // If its not specified, computer will be the default location.
        const path = argv[1] ?? "computer://";

        // Wait for window closure
        await this.wndProcess(path);

        // Quit
        this.terminate();
    }

    /**
     * Main 3DFE window process.
     * @param {String} path The path of the directory to browse.
     */
    async wndProcess(path) {
        // Create the app window
        const mainwnd = this.createWindow({
            title: "3DFE",
            icon: await Theme.getIconUrl("places/folder", '16x16'),
            bodyClass: "eyeframe-app", //Inherit from iframe app class for now
            body: `<iframe class="app-frame"></iframe>`,
            taskbar: true,
            resizable: true,
            initialWidth: 640,
            initialHeight: 480,
            minWidth: 640,
            minHeight: 480
        }, true);

        // Setup iframe
        const body = mainwnd.getBodyContainer();
        /** @type {HTMLIFrameElement} */
        const iframe = body.querySelector('iframe');

        iframe.onload = async ()=>{
            // Assign some vars
            iframe.contentWindow.FS = w96.FS;
            iframe.contentWindow.FSUtil = w96.FSUtil;
            iframe.contentWindow.current = current;
            iframe.contentWindow.startupPath = path;
            iframe.contentWindow.sys = w96.sys;
            iframe.contentWindow.mainwnd = mainwnd;

            // Needed to keep track of blobs
            iframe.contentWindow.registerBlobURL = (u)=>this.registeredBlobs.push(u);
            
            // Load stylesheets and scripts
            const cd = iframe.contentDocument;
            
            const styleEl = cd.createElement('style');
            styleEl.textContent = await FS.readstr(FSUtil.combinePath(current.modulePath, "assets/css/3dfe.css"));
            cd.head.appendChild(styleEl);

            // Load bundle JS
            const scriptEl = cd.createElement('script');
            scriptEl.textContent = await FS.readstr(FSUtil.combinePath(current.modulePath, "bundle.js"));
            cd.body.appendChild(scriptEl);
        };

        // On deactivate
        mainwnd.ondarkenelements = () => {
            iframe.blur();
        }

        // On activate
        mainwnd.onlightenelements = ()=> {
            iframe.focus();
        }

        // Set source to 3dfe html
        iframe.srcdoc = " ";

        // Show window
        mainwnd.show();
        await mainwnd.wait();
    }

    /**
     * Called on program exit.
     * 
     * Here we deregister all used blobs.
     */
    ontermination() {
        super.ontermination();

        for(let b of this.registeredBlobs)
            URL.revokeObjectURL(b);
    }
} 
return await WApplication.execAsync(new FE3DApplication(), this.boxedEnv.args); 
