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

const{Theme:Theme,MenuBar:MenuBar,MsgBoxSimple:MsgBoxSimple,DialogCreator:DialogCreator}=w96.ui,{sideloadZip:sideloadZip}=w96.util,{execCmd:execCmd}=w96.sys,THEME_PATH_BASE="c:/user/appdata/MsgRoom/themes";class Win96ChatApplication extends WApplication{constructor(){super()}async main(e){if(super.main(e),this.config={currentTheme:null,disableWin96Detection:!1},await FS.exists("C:/user/appdata/MsgRoom")||await FS.mkdir("C:/user/appdata/MsgRoom"),await FS.exists("C:/user/appdata/MsgRoom/config.json")||await FS.writestr("C:/user/appdata/MsgRoom/config.json",JSON.stringify(this.config,null,4)),this.config={...this.config,...JSON.parse(await FS.readstr("C:/user/appdata/MsgRoom/config.json"))},!await FS.exists("C:/user/appdata/MsgRoom/themes")){await FS.mkdir("C:/user/appdata/MsgRoom/themes");const e=MsgBoxSimple.idleProgress("MsgRoom","Loading default themes...");try{await sideloadZip("/system/data/msgroom/themes_default.zip")}catch(e){DialogCreator.create({icon:"warning",body:"Error fetching default themes!<br><br>Please check your internet connection or contact the developers."})}e.closeDialog()}this._createWindow(e)}async applyTheme(e){const t=THEME_PATH_BASE+"/"+e+".css";if(!await FS.exists(t))return void DialogCreator.create({icon:"warning",body:"Theme does not exist."});const i=await FS.readstr(t);this.appFrame.contentWindow.postMessage({op:"wui-css-set",css:i},this.CHAT_URL),this.config.currentTheme!=e&&(this.config.currentTheme=e,await FS.writestr("C:/user/appdata/MsgRoom/config.json",JSON.stringify(this.config,null,4)))}async readThemesMenu(){const e=[];return(await FS.readdir(THEME_PATH_BASE)).forEach((t=>{if(!t.endsWith(".css"))return;const i=FSUtil.fname(t),a=i.substring(0,i.length-4);e.push({type:"normal",label:a,onclick:()=>this.applyTheme(a)})})),[...e,{type:"separator"},{type:"normal",label:"Open themes directory",onclick:()=>{execCmd("explorer",[THEME_PATH_BASE])}}]}async _createWindow(e){let t="https://devel.windows96.net:4096/";e.includes("--devel-local")?t="http://127.0.0.1:4096/":e.includes("--devel-local")&&(t="https://devel.windows96.net:4096/"),e.forEach(((e,i,a)=>{"--server"!=e||i+1>=a.length||(t=a[i+1])})),this.CHAT_URL=t;const i=this.createWindow({title:"MsgRoom",initialWidth:640,initialHeight:441,taskbar:!0,body:`<div class="appbar"></div><iframe src="${t}" allow="clipboard-write"></iframe>`,bodyClass:"win96chat-app",icon:await Theme.getIconUrl("apps/chat","16x16"),iframeFix:!0},!0),a=i.getBodyContainer(),s=a.querySelector("iframe"),o=new MenuBar;o.addRoot("File",[{type:"normal",label:"Exit",onclick:()=>this.mainwnd.close()}]),o.addRoot("Session",[{type:"submenu",label:"Themes",removeIconPrespace:!0,items:await this.readThemesMenu()},{type:"normal",label:"Change Nick",onclick:()=>MsgBoxSimple.prompt("Set Nickname","Enter a new nickname","anonymoose",(e=>{null!=e&&""!=e.trim()&&s.contentWindow.postMessage({op:"wui-nick-change",args:{value:e}},t)}))}]),o.addRoot("Help",[{type:"normal",label:"View Help",onclick:()=>{s.contentWindow.postMessage({op:"wui-print-help",args:{}},t)}},{type:"separator"},{type:"normal",label:"About",onclick:async()=>{const e=this.createWindow({title:"About MsgRoom",controlBoxStyle:"WS_CBX_CLOSE",initialHeight:165,initialWidth:276,taskbar:!1,bodyClass:"msgroom-about-dlg",body:`<div class="icon" style="background-image: url(${await Theme.getIconUrl("apps/chat","32x32")})"></div>\n                        <div class="p2">\n                            <div class="title">MsgRoom</div>\n                            <div class="subtext">Social messaging chatroom<br>\n                            Version 1.0<br>\n                            <br>\n                            Copyright (C) windows96.net 2023.\n                            </div>\n                            <div class="buttons">\n                                <button class="w96-button close">OK</button>\n                            </div>\n                        </div>`,center:!0,resizable:!1},!1);e.show(),e.getBodyContainer().querySelector(".close").addEventListener("click",(()=>e.close()))}}]),a.querySelector(".appbar").replaceWith(o.getMenuDiv()),s.onload=()=>{this.config.currentTheme&&this.applyTheme(this.config.currentTheme),s.contentWindow.postMessage({op:"w96-app-init",args:{pid:this.appId,disableWin96Detection:this.config.disableWin96Detection}},t)},this.appFrame=s,this.mainwnd=i,i.show()}}

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