//!wrt $BSPEC:{"icn":"apps/dosbox","cpr":"Copyright (C) Windows 96 Team 2023.","dsc":"DOSBox DOS Emulator","frn":"DOSBox","ver":1,"ssy":"gui"}

const{WindowParams:WindowParams,WRT:WRT}=w96,{DialogCreator:DialogCreator,Theme:Theme,OpenFileDialog:OpenFileDialog,MsgBoxSimple:MsgBoxSimple,MenuBar:MenuBar}=w96.ui,{isURL:isURL,isDataURL:isDataURL,dataURItoBlob:dataURItoBlob}=w96.util;class DOSBOXApplication extends WApplication{constructor(){super()}async main(e){super.main(e),await this._createWindow(null!=e[1]?e[1]:"W:/system/apps/dosbox/zips/msd-essentials.zip")}showOpenZipDialog(){new OpenFileDialog("w:/system/apps/dosbox/zips",["zip","ZIP"],(async e=>{this._fnUrl&&URL.revokeObjectURL(this._fnUrl),this._fnUrl=URL.createObjectURL(await FS.toBlob(e)),this.appFrame.onload=()=>{this.appFrame.contentWindow.app.init(this,this.mainwnd,this._fnUrl)},this.appFrame.contentDocument.location.reload()})).show()}async _createWindow(e){var t;let i={args:[],ui:{showMenuBar:!0,width:642,height:441},scripts:{init:null}};if(e.endsWith(".json"))try{i={...i,...JSON.parse(await FS.readstr(e))},i.zip&&(t=await FS.toBlob(i.zip)),i.icon&&(i.icon=URL.createObjectURL(await FS.toBlob(i.icon)))}catch(e){MsgBoxSimple.error("DOSBox","Error reading configuration.","OK"),this.terminate()}else try{if(isURL(e)){const i=await fetch(e,{method:"GET",cache:"no-cache"});t=await i.blob()}else t=isDataURL(e)?dataURItoBlob(e):await FS.toBlob(e)}catch(e){console.error(e),MsgBoxSimple.error("DOSBox","Cannot mount ZIP file!","OK"),this.terminate()}const a=new WindowParams;a.title="DOSBox",a.initialWidth=i.ui.width,a.initialHeight=i.ui.height;const o=this.createWindow(a,!0);o.registerWindow(),o.registerAppBar(),o.setHtml('<div class="appbar"></div><iframe src="/system/apps/dosbox"></iframe>');const n=o.wndObject.querySelector(".window-html-content");n.classList.add("dosbox-app"),o.setWindowIcon(await Theme.getIconUrl("apps/dosbox","16x16"));const s=new MenuBar;s.addRoot("File",[{type:"normal",label:"Open ZIP",onclick:()=>{this.showOpenZipDialog()}},{type:"separator"},{type:"normal",label:"Exit",onclick:()=>{this.terminate()}}]),s.addRoot("Scale",[{type:"normal",label:"Aspect",onclick:()=>{this.appFrame.contentWindow.app.setScaleMode("aspect")}},{type:"normal",label:"Stretch",onclick:()=>{this.appFrame.contentWindow.app.setScaleMode("stretch")}},{type:"separator"},{type:"normal",label:"Fullscreen",onclick:()=>{this.appFrame.contentWindow.app.setScaleMode("fullscreen")}}]),s.addRoot("Help",[{type:"normal",label:"About",onclick:()=>{w96.ui.MsgBoxSimple.info("About NES",'<span class="bold-noaa">JSDOSBox Emulator</span><br>Version 1.0<br><br>Powered by <a href="https://js-dos.com/">jsdos</a>',"OK").dlg.setSize(320,140)}}]),n.querySelector(".appbar").replaceWith(s.getMenuDiv()),i.icon&&o.setWindowIcon(i.icon),i.title&&o.setTitle(i.title),i.ui.showMenuBar||(s.getMenuDiv().style.display="none");const r=o.wndObject.querySelector("iframe"),l=URL.createObjectURL(t);this._fnUrl=l,r.onload=()=>{r.contentWindow.app.init(this,o,l,this.gameCfg.args,(()=>{this.gameCfg.ui.aspect&&this.appFrame.contentWindow.app.setScaleMode(this.gameCfg.ui.aspect)}))},this.appFrame=r,this.mainwnd=o,o.show(),this.gameCfg=i}async procScript(e,t){const i=this.gameCfg.scripts[e];if(!i)return;const a=MsgBoxSimple.idleProgress("DOSBox","Running scripts, please wait...");try{await WRT.runFile(i,{boxedEnv:{instance:this,...t}})}catch(e){DialogCreator.create({icon:"error",title:"DOSBox",body:`Failed to execute script ${i}:\n`+new String(e)})}a.closeDialog()}ontermination(){super.ontermination(),URL.revokeObjectURL(this._fnUrl),this.gameCfg.icon&&URL.revokeObjectURL(this.gameCfg.icon)}}

return await WApplication.execAsync(new DOSBOXApplication(), this.boxedEnv.args, this);