//!wrt $BSPEC:{"icn":null,"cpr":"Copyright (C) Windows 96 Team 2023.","dsc":"Mikesoft Development Environment","frn":"Mikesoft Windows 96 DevEnv","ver":1,"ssy":"gui"}

const{wait:wait,escapeHtml:escapeHtml}=w96.util,{DialogCreator:DialogCreator,MenuBar:MenuBar,Theme:Theme}=w96.ui,{ToolBar:ToolBar,UIComponent:UIComponent}=w96.ui.components,noop=()=>{};let appInstance=null;const DEV_ENV_ROOT="W:/system/apps/devenv",DEV_ENV_PATHS={ROOT:DEV_ENV_ROOT,RSRC:DEV_ENV_ROOT+"/resources",PROJECTS_DIR:"C:/user/documents/DevEnv Projects"};class ExtendedTabControl extends UIComponent{constructor(){super(),this.createdTabs=0,this.activePage=-1,this.pages=[],this.sharedPageSources=[],this.uiComponent=document.createElement("div"),this.uiComponent.classList.add("wde-xtab-control");const t=document.createElement("div");t.classList.add("pages"),this.pagesCntEl=t,this.uiComponent.appendChild(t);const e=document.createElement("div");e.classList.add("content"),this.contentCntEl=e,this.uiComponent.appendChild(e)}hideAllPages(){const t=this.contentCntEl.querySelectorAll(".page");for(let e of t)e.style.display="none"}addPage(t={}){const e={type:"normal",persist:!0,closeable:!0,label:"New Tab",icon:null,pageSource:null,ondraw:async()=>{},onclose:async()=>{},id:this.createdTabs++,...t},a=document.createElement("div");if(a.classList.add("tab"),e.icon){const t=document.createElement("div");t.classList.add("icon"),t.style.backgroundImage=`url(${e.icon})`,a.appendChild(t)}if(e.label){const t=document.createElement("div");t.classList.add("name"),t.textContent=e.label,a.appendChild(t)}if(e.label){const t=document.createElement("div");t.classList.add("close"),t.addEventListener("click",(async t=>{t.stopPropagation(),await e.onclose(),this.closeTab(e.id)})),a.appendChild(t)}return a.addEventListener("click",(()=>{this.activate(e.id)})),a.classList.add(`i-${e.id}`),this.pagesCntEl.appendChild(a),this.pages.push(e),e.id}closeTab(t){const e=this.pages.findIndex((e=>e.id==t)),a=this.pages[e];if(!a)throw new Error("Page not found.");const i=this.contentCntEl.querySelector(`.i-${a.id}`);i&&i.remove();const s=this.pagesCntEl.querySelector(`.i-${a.id}`);s&&s.remove(),this.pages.splice(e,1)}async activate(t){const e=this.pages.find((e=>e.id==t));if(!e)throw new Error("Page not found.");for(let t of this.pagesCntEl.children)t.classList.contains("active")&&t.classList.remove("active");this.hideAllPages();for(let t of this.contentCntEl.children)t.classList.contains("ps")||t.remove();let a=this.contentCntEl.querySelector(`.i-${e.id}`);a?a.style.display="":(a=document.createElement("div"),a.classList.add("page",`i-${e.id}`),await e.ondraw(a),this.contentCntEl.appendChild(a),e.persist&&a.classList.add("ps"));this.pagesCntEl.querySelector(`.i-${e.id}`).classList.add("active")}}async function createAboutWindow(t){const e=t.createWindow({title:"About Mikesoft DevEnv",bodyClass:"wde-app-about-dlg",body:"\n            Mikesoft DevEnv\n        ",noCompose:!0,taskbar:!1,resizable:!1,initialWidth:480,initialHeight:340,controlBoxStyle:"WS_CBX_CLOSE"},!1);return e.center(),e.show(),e}async function createApplicationSplash(t){const e=t.createWindow({title:"Win96 DevEnv",bodyClass:"wde-app-splash",windowClass:"wde-app-splash-wnd",body:`${t.getTextResource("UI/Splash")}\n        <style>\n            ${t.getTextResource("UI/SplashStyles")}\n        </style>`,noCompose:!0,taskbar:!1,resizable:!1,draggable:!1,initialWidth:480,initialHeight:340,controlBoxStyle:"WS_CBX_NONE"},!1);return e.center(),e.show(),await wait(250),e}function createStandardTab(t,e=!1){let a;switch(t){case"start-page":a=appInstance.ide.mainTabCtl.addPage({label:"Start Page",ondraw:t=>{t.classList.add("wde-tab-start-page"),t.innerHTML=appInstance.getTextResource("UI/StartPage")}});break;case"web-browser":a=appInstance.ide.mainTabCtl.addPage({label:"Web Browser",ondraw:t=>{t.classList.add("wde-tab-web-browser"),t.innerHTML=appInstance.getTextResource("UI/Tab/WebBrowser");const e=t.querySelector("input"),a=t.querySelector("iframe");e.addEventListener("keyup",(async t=>{if("Enter"==t.key){let t=e.value;if(/^([a-zA-Z]:)/.test(t)){t=`/_/${t[0]}/${t.substring(3)}`}else t.startsWith("about:")||t.startsWith("/")||t.startsWith("http:")||t.startsWith("https:")||(t="http://"+t);e.value=t,a.src=t}}))}})}e&&appInstance.ide.mainTabCtl.activate(a)}class IDE{constructor(t){this.app=t}async initMenuBar(){const t=this.ideWnd.getBodyContainer(),e=new MenuBar;e.addRoot("File",[{type:"normal",label:"Exit",onclick:()=>this.ideWnd.close()}]),e.addRoot("View",[{type:"normal",label:"Start Page",onclick:()=>createStandardTab("start-page",!0)},{type:"separator"},{type:"submenu",label:"Other Windows",items:[{type:"normal",label:"Web Browser",icon:await Theme.getIconUrl("objects/world","16x16"),onclick:()=>createStandardTab("web-browser",!0)}]}],!1),e.addRoot("Help",[{type:"normal",label:"About",onclick:()=>createAboutWindow(this.app)}]),t.querySelector(".appbar").replaceWith(e.getMenuDiv())}async initToolBar(){const t=this.ideWnd.getBodyContainer(),e=new ToolBar({iconSize:20,defaults:{itemTextPosition:"right"},compact:!0});e.addItem({imageUrl:await Theme.getIconUrl("mime/executable","16x16"),text:"Testing"}),t.querySelector(".toolbar").replaceWith(e.getElement())}async initTabControl(){const t=this.ideWnd.getBodyContainer(),e=new ExtendedTabControl;this.mainTabCtl=e,createStandardTab("start-page",!0),t.querySelector(".tab-ctrl").replaceWith(e.getElement())}async constructWindow(){this.ideWnd=this.app.createWindow({title:"Mikesoft DevEnv",bodyClass:"wde-app-ide",body:'\n            <div class="w96-ui-inset">\n                <div class="appbar"></div>\n                <div class="toolbar"></div>\n            </div>\n\n            <div class="content">\n                <div class="split-pane">\n                    <div class="left">\n                        \x3c!-- Main View --\x3e\n                        <div class="tab-ctrl"></div>\n                    </div>\n                    <div class="right">\n                        <div class="tool-container">\n                            <div class="title">\n                                Solution Browser\n                            </div>\n                            <div class="content">\n                                TODO\n                            </div>\n                        </div>\n                    </div>\n                </div>\n\n                <div class="mini-window-drawer">\n\n                </div>\n            </div>\n\n            <footer class="w96-footer">\n                <div class="cell">\n                    Ready\n                </div>\n            </footer>\n            ',icon:await Theme.getIconUrl("mime/executable","16x16"),taskbar:!0,resizable:!0,initialWidth:800,initialHeight:600},!0),await this.initMenuBar(),await this.initToolBar(),await this.initTabControl()}async begin(){await this.constructWindow(),this.ideWnd.show(),await this.ideWnd.wait(),this.app=null,this.ideWnd=null,this.mainTabCtl=null}}class MDevEnvApplication extends WApplication{constructor(){super(),appInstance=this,this.blobs=[],this.textResourceMap={}}async loadStrRsrcFromFile(t,e){null==this.textResourceMap[t]&&(this.textResourceMap[t]=await FS.readstr(e))}getTextResource(t){return this.textResourceMap[t]}async initializeEnvironment(){await this.loadStrRsrcFromFile("UI/SplashStyles",DEV_ENV_PATHS.RSRC+"/splash.css"),await this.loadStrRsrcFromFile("UI/Splash",DEV_ENV_PATHS.ROOT+"/ui/splash.html");const t=await createApplicationSplash(this),e=t.getBodyContainer().querySelector(".w96-progressbar>.progress");await FS.exists(DEV_ENV_PATHS.PROJECTS_DIR)||await FS.mkdir(DEV_ENV_PATHS.PROJECTS_DIR),e.style.width="10%";const a=document.createElement("style");a.textContent=await FS.readstr(DEV_ENV_PATHS.RSRC+"/app.css"),document.body.appendChild(a),this.styleEl=a,e.style.width="20%",e.style.width="30%",await this.loadStrRsrcFromFile("UI/StartPage",DEV_ENV_PATHS.ROOT+"/ui/start-page.html"),await this.loadStrRsrcFromFile("UI/Tab/WebBrowser",DEV_ENV_PATHS.ROOT+"/ui/tabs/web-browser.html"),e.style.width="100%",t.close()}async main(t){await super.main(t);try{await this.initializeEnvironment()}catch(t){const e=w96.ui.DialogCreator.alert(`The development environment failed to initialize!<br><br>Exception: <span class="ex">\n                <br>\n                Message -> ${escapeHtml(t.message||"<none>")}<br>\n                Stack -> ${escapeHtml(t.stack||"<none>")}<br> \n            </span>`,{icon:"error",title:"DevEnv Error"});return await e.wait(),void this.terminate()}this.ide=new IDE(this),await this.ide.begin(),this.ide=null}async ontermination(){await super.ontermination();for(let t of this.blobs)URL.revokeObjectURL(t);this.styleEl&&this.styleEl.remove(),this.styleEl=null,this.textResourceMap=null,this.ide=null,appInstance=null}}

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