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

const{MsgBoxSimple:MsgBoxSimple,SaveFileDialog:SaveFileDialog,DialogCreator:DialogCreator,Theme:Theme,MenuBar:MenuBar}=w96.ui,{TreeView:TreeView}=w96.ui.components;class DevMgrApplication extends WApplication{constructor(){super()}async main(e){if(super.main(e),document.querySelector(".devmgr-app"))return;const t=this.createWindow({title:"Device Manager",icon:await Theme.getIconUrl("apps/devmgr","16x16"),taskbar:!0,initialWidth:530,initialHeight:400,bodyClass:"devmgr-app",body:'\n            <div class="appbar"></div>\n            <div class="tree w96-tree" style="height: 100%;"></div>\n            <footer class="w96-footer">\n                <div class="cell">\n                    No errors found\n                </div>\n            </footer>\n            '},!0),i=t.getBodyContainer(),a=new MenuBar;a.addRoot("File",[{type:"normal",label:"Save Report",onclick:()=>new SaveFileDialog("c:/user/documents",[],(async e=>{null!=e&&""!=e.trim()&&(await FS.writestr(e,`\nDevice Manager Report\nGenerated at ${new Date}\n=====================\nMonitor: ${(await this.getMonitor())[0].label}\nCPU: ${(await this.getCPU())[0].label}\nGPU: ${await this.getGPU()}\nMounted Disks:\n${FS.list().join("\n")}`),DialogCreator.create({body:"Report has been generated.",title:"Information",icon:"info"}))})).show()},{type:"separator"},{type:"normal",label:"Exit",onclick:()=>t.close()}]),a.addRoot("Help",[{type:"normal",label:"About",onclick:()=>DialogCreator.alert('<span class="bold-noaa">Device Manager</span><br>Version 1.0<br>',{title:"About Device Manager",icon:"info"})}]),i.querySelector(".appbar").replaceWith(a.getMenuDiv());const o=MsgBoxSimple.status("Device Manager","Computing resources...");o.show(),i.querySelector(".tree").replaceWith(new TreeView([{label:"Computer",icon:await Theme.getIconUrl("devices/computer","16x16"),items:[{label:"File System Interfaces",icon:await Theme.getIconUrl("mime/executable","16x16"),opened:!1,items:await this.computeAvailableFSDrivers()},{label:"GPU",icon:await Theme.getIconUrl("devices/card","16x16"),opened:!1,items:[{label:await this.getGPU(),icon:await Theme.getIconUrl("devices/card","16x16")}]},{label:"Monitors",icon:await Theme.getIconUrl("devices/monitor","16x16"),opened:!1,items:await this.getMonitor()},{label:"Processors",icon:await Theme.getIconUrl("devices/cpu","16x16"),opened:!1,items:await this.getCPU()},{label:"Volumes",icon:await Theme.getIconUrl("devices/hdd","16x16"),opened:!1,items:await(async()=>{const e=[];for(let t of FS.list())e.push({icon:await Theme.getIconUrl("devices/hdd","16x16"),label:t});return e})()}],opened:!0}]).getElement()),o.close(),t.show()}async computeAvailableFSDrivers(){const e=[];return void 0!==window.indexedDB&&e.push({label:"Indexed Storage",icon:await Theme.getIconUrl("devices/hdd","16x16")}),void 0!==window.localStorage&&e.push({label:"Local Storage",icon:await Theme.getIconUrl("devices/floppy","16x16")}),e.push({label:"In-memory Storage",icon:await Theme.getIconUrl("devices/memory","16x16")}),e}async getGPU(){const e=document.createElement("canvas").getContext("webgl");try{let t=e.getParameter(e.RENDERER);if("WebKit WebGL"!==t)return t;{const t=e.getExtension("WEBGL_debug_renderer_info");if(t&&t.UNMASKED_RENDERER_WEBGL)return e.getParameter(t.UNMASKED_RENDERER_WEBGL)}}catch(e){}return"Unknown GPU"}async getCPU(){return[{label:"Generic Processor with "+navigator.hardwareConcurrency+" logical processors",icon:await Theme.getIconUrl("devices/cpu","16x16")}]}async getMonitor(){return screen&&screen.width&&screen.height?[{label:`Monitor (${screen.width*window.devicePixelRatio}x${screen.height*window.devicePixelRatio})`,icon:await Theme.getIconUrl("devices/monitor","16x16")}]:[{label:"Unknown Monitor",icon:await Theme.getIconUrl("devices/monitor","16x16")}]}}

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