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

const{WindowParams:WindowParams}=w96,{MenuBar:MenuBar,OpenFileDialog:OpenFileDialog,SaveFileDialog:SaveFileDialog,MsgBoxSimple:MsgBoxSimple,Theme:Theme}=w96.ui;class NESEmulatorApplication extends WApplication{constructor(){super()}async main(e){super.main(e),null!=e[1]?await this._createWindow(e[1]):new OpenFileDialog("c:/",["nes","NES"],(e=>{e?this._createWindow(e):this.terminate()})).show()}showOpenRomDialog(){new OpenFileDialog("c:/",["nes","NES"],(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 a=new WindowParams;a.title=FSUtil.fname(e)+" - NES",a.initialWidth=256,a.initialHeight=281;var t=this.createWindow(a,!0);t.registerWindow(),t.registerAppBar(),t.setHtml('<div class="appbar"></div><iframe src="/system/apps/nes"></iframe>');const n=t.wndObject.querySelector(".window-html-content");n.classList.add("nes-app"),t.setWindowIcon(await Theme.getIconUrl("apps/nes","16x16"));const o=new MenuBar;o.addRoot("File",[{type:"normal",label:"Open ROM",onclick:()=>{this.showOpenRomDialog()}},{type:"separator"},{type:"normal",label:"Exit",onclick:()=>{this.terminate()}}]),o.addRoot("Scale",[{type:"normal",label:"Stretch",onclick:()=>{this.appFrame.contentWindow.app.setScaleMode("")}},{type:"normal",label:"Zoom",onclick:()=>{this.appFrame.contentWindow.app.setScaleMode("zoom-scale")}},{type:"normal",label:"Pixel Perfect",onclick:()=>{this.appFrame.contentWindow.app.setScaleMode("pixel-perfect")}}]),o.addRoot("Debug",[{type:"normal",label:"Dump Memory",onclick:()=>{const e=Array.from(this.appFrame.contentWindow.app.nes.cpu.mem),a=new SaveFileDialog("c:/",["mem"],(async a=>{a&&await FS.writebin(a,e)}));a.show(),a.fileBox.value="Dump_"+(new Number(new Date)+0).toString()+".mem"}},{type:"normal",label:"Load Dump",onclick:()=>{new OpenFileDialog("c:/",["mem"],(async e=>{e&&(this.appFrame.contentWindow.app.nes.cpu.mem=Array.from(await FS.readbin(e)))})).show()}}]),o.addRoot("Help",[{type:"normal",label:"Controls",onclick:()=>{w96.ui.MsgBoxSimple.info("Controls",'<span class="bold-noaa">Controls</span><br>a =&gt; A<br>s =&gt; B<br>Arrow Keys =&gt; DPAD<br>Enter =&gt; START<br>Tab =&gt; SELECT',"OK").dlg.setSize(320,170)}},{type:"separator"},{type:"normal",label:"About",onclick:()=>{w96.ui.MsgBoxSimple.info("About NES",'<span class="bold-noaa">NES Emulator</span><br>Version 1.0<br><br>Powered by <a href="https://github.com/bfirsh/jsnes">JSNES</a>',"OK").dlg.setSize(320,140)}}]),n.querySelector(".appbar").replaceWith(o.getMenuDiv());const i=t.wndObject.querySelector("iframe"),l=URL.createObjectURL(await FS.toBlob(e));this._fnUrl=l,i.onload=()=>{i.contentWindow.app.init(this,t,l)},this.appFrame=i,this.mainwnd=t,t.show()}async ontermination(){await super.ontermination(),URL.revokeObjectURL(this._fnUrl)}}

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