//!wrt $BSPEC:{"icn":"apps/textpad","cpr":"Copyright (C) Windows 96 Team 2021.","dsc":"Basic text editor","frn":"TextPad","ver":1,"ssy":"gui"}

const{MsgBoxSimple:MsgBoxSimple,OpenFileDialog:OpenFileDialog,SaveFileDialog:SaveFileDialog,Theme:Theme,MenuBar:MenuBar,DialogCreator:DialogCreator}=w96.ui,{sysConf:SCM}=w96;class TextpadApplication extends WApplication{constructor(){super(),this.lastDir="c:/",this.uiState={wordWrapItem:null},this.wordWrap=!0}async main(t){super.main(t),null==t[1]?this.currentFile=null:this.currentFile=t[1];const e=this.createWindow({title:(null==t[1]?"Untitled":FSUtil.fname(t[1]))+" - Textpad",initialHeight:400,initialWidth:600,taskbar:!0,bodyClass:"textpad-app",icon:await Theme.getIconUrl("apps/textpad","16x16")},!0),i=e.getBodyContainer(),a=new MenuBar;a.addRoot("File",[{type:"normal",label:"New",onclick:()=>{this.currentFile=null,this.loadDocument(null)}},{type:"separator"},{type:"normal",label:"Open",onclick:()=>{new OpenFileDialog(this.lastDir,["txt","log","json"],(t=>{t&&(this.currentFile=t,this.loadDocument(t),this.appWindow.activate(),this.lastDir=FSUtil.getParentPath(t))})).show()}},{type:"normal",label:"Save",onclick:()=>{null!=this.currentFile?FS.writestr(this.currentFile,this.textArea.value).catch((t=>{MsgBoxSimple.error("File Save Error","Failed to write to ["+this.currentFile+"]!\nReason: "+t,"OK")})):this._saveAs()}},{type:"normal",label:"Save As",onclick:()=>{this._saveAs()}},{type:"separator"},{type:"normal",label:"Exit",onclick:()=>{this.terminate()}}]),a.addRoot("Edit",[this.uiState.wordWrapItem={type:"normal",label:"Word Wrap",checked:this.wordWrap,onclick:()=>this.setWordWrap(!this.wordWrap)}]),a.addRoot("Help",[{type:"normal",label:"About Textpad",onclick:()=>DialogCreator.alert('<span class="bold-noaa">Textpad</span><br>Version 1.0 <br>',{title:"About Textpad",icon:"info"})}]);const l=document.createElement("textarea");l.classList.add("main-editor");const r=document.createElement("footer");r.classList.add("w96-footer");const o=document.createElement("div");o.classList.add("cell"),r.appendChild(o),i.appendChild(a.getMenuDiv()),i.appendChild(l),i.appendChild(r),e.show(),e.center(),this.mainwnd=e,this.textArea=l,this.footerCell=o,this.loadDocument(this.currentFile),SCM.get("Software/Textpad")||await SCM.setAndSync("Software/Textpad/WordWrap",this.wordWrap),this.setWordWrap(SCM.get("Software/Textpad/WordWrap"))}setWordWrap(t){t?this.textArea.classList.add("word-wrap"):this.textArea.classList.remove("word-wrap"),this.uiState.wordWrapItem.checked=t,this.wordWrap=t,SCM.setAndSync("Software/Textpad/WordWrap",t)}ontermination(){super.ontermination(),this.textArea=null,this.footerCell=null,this.mainwnd=null,this.uiState=null}loadDocument(t){if(null==t)return this.textArea.value="",this._setWinTitle(null),void(this.footerCell.textContent="Untitled");FS.readstr(t).then((e=>{this.textArea.value=e,this.footerCell.textContent=t,this._setWinTitle(t)}))}_setWinTitle(t){this.mainwnd.setTitle((null==t?"Untitled":FSUtil.fname(t))+" - Textpad")}_saveAs(){new SaveFileDialog(this.lastDir,["txt","log","json"],(t=>{t&&FS.writestr(t,this.textArea.value).then((()=>{this.currentFile=t,this._setWinTitle(t),this.footerCell.textContent=t,this.lastDir=FSUtil.getParentPath(t)})).catch((e=>{MsgBoxSimple.error("File Save Error","Failed to write to ["+t+"]!\nReason: "+e,"OK")}))})).show()}}

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