//!wrt $BSPEC:{"icn":"mime/pdf","cpr":"Copyright (C) Windows 96 Team 2023.","dsc":"PDF Reader","frn":"PDF Reader","ver":1,"ssy":"gui"}

const{MenuBar:MenuBar,Theme:Theme,OpenFileDialog:OpenFileDialog}=w96.ui;class PDFReaderApplication extends WApplication{constructor(){super(),this.currentBlob=null,this.lastDir="c:/user/documents";const e=this.createWindow({title:"PDF Reader",initialWidth:780,initialHeight:665,resizable:!0,taskbar:!0,body:'\n                <div class="appbar"></div>\n                <iframe src="/system/apps/pdf"></iframe>\n                <footer class="w96-footer" style="margin-top: 2px">\n                    <div class="cell">\n                        &lt;unknown file&gt;\n                    </div>\n                </footer>\n            ',bodyClass:"pdf-reader-app"},!0);Theme.getIconUrl("mime/pdf","16x16").then((t=>e.setWindowIcon(t)));const t=e.getBodyContainer(),n=t.querySelector("iframe"),i=new MenuBar;i.addRoot("File",[{type:"normal",label:"Open",onclick:()=>this.openFileUI()},{type:"separator"},{type:"normal",label:"Exit",onclick:()=>e.close()}]),i.addRoot("View",[{type:"normal",label:"Presentation Mode",onclick:()=>n.contentWindow.PDFViewerApplication.requestPresentationMode()}]),i.addRoot("Help",[{type:"normal",label:"About",onclick:()=>alert('<span class="bold-noaa">PDF Reader</span><br>Version 1.0<br><br>Uses pdf.js, available from <a href="https://github.com/mozilla/pdf.js" target="_blank">https://github.com/mozilla/pdf.js</a>',{icon:"info"})}]),t.querySelector(".appbar").replaceWith(i.getMenuDiv()),this.mainwnd=e}openFileUI(){new OpenFileDialog(this.lastDir,["pdf","PDF"],(e=>{null!=e&&""!==e.trim()&&this.loadFile(e)})).show()}async loadFile(e){if(!await FS.exists(e))return alert("Cannot load file.",{icon:"error"});this.currentBlob&&(URL.revokeObjectURL(this.currentBlob),this.currentBlob=null);const t=this.mainwnd.getBodyContainer(),n=t.querySelector("iframe").contentWindow;try{this.currentBlob=URL.createObjectURL(await FS.toBlob(e)),this.lastDir=FSUtil.getParentPath(e),t.querySelector("footer .cell").textContent=e,this.mainwnd.setTitle(`${FSUtil.fname(e)} - PDF Reader`),n.PDFViewerApplication.open(this.currentBlob)}catch(e){console.error(e)}}async main(e){super.main(e),this.mainwnd.show();const t=this.mainwnd.getBodyContainer().querySelector("iframe").contentWindow;t.onload=async()=>{if(null!==e[1]){if(!await FS.exists(e[1]))return alert("Error: File not found.",{icon:"error"});this.loadFile(e[1])}t.document.querySelector("#openFile").addEventListener("click",(()=>this.openFileUI()))}}}

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