import { App } from '@capacitor/app'
import { Capacitor } from '@capacitor/core'
import { Device } from '@capacitor/device'
import { http } from '@/plugins/http'

let initialized = false
let loading = false
async function init() {
    // eruda.init({
    //     useShadowDom: false,
    //     defaults: {
    //         theme: 'Material Oceanic'
    //     }
    // })
    // eruda.add(erudaCode)
    // eruda.add(erudaFeatures)
    // eruda.add(erudaFps)
    // eruda.add(erudaTiming)
    // eruda.add(erudaMemory)
    if (loading || initialized) return
    loading = true
    import('eruda')
        .then(res => {
            loading = false
            let eruda = res.default
            eruda.init({
                useShadowDom: true,
                defaults: {
                    theme: 'Material Oceanic'
                }
            })
            initialized = true
            import('eruda-code').then(res => {
                eruda.add(res.default)
            })
            // import('eruda-features').then(res => {
            //     eruda.add(res.default)
            // })
            // import('eruda-fps').then(res => {
            //     eruda.add(res.default)
            // })
            // import('eruda-timing').then(res => {
            //     eruda.add(res.default)
            // })
            // import('eruda-memory').then(res => {
            //     eruda.add(res.default)
            // })
            let console = eruda.get('console')
            // console.config.set('overrideConsole', false)

            eruda.remove('sources')
            let snippets = eruda.get('snippets')
            snippets.add(
                'Scan Code',
                () => {
                    eruda.hide()
                    window.router.push('/scanCode')
                },
                'Scan Code'
            )
            snippets.add('Set Token', () => {
                const token = prompt('token')
                if (token) {
                    http.setToken(token)
                    document.location.href = './'
                }
            })
            let info = eruda.get('info')
            info.remove('Backers')
            info.remove('About')
            if (Capacitor.isNativePlatform()) {
                App.getInfo().then(appInfo => {
                    info.add(
                        'AppInfo',
                        `<table><tbody>
                        <tr><td class="eruda-device-key">name</td><td>${appInfo.name}</td></tr>
                        <tr><td>id</td><td>${appInfo.id}</td></tr>
                        <tr><td>build</td><td>${appInfo.build}</td></tr>
                        <tr><td>version</td><td>${appInfo.version}</td></tr>
                        </tbody></table>`
                    )
                })
                Device.getInfo().then(deviceInfo => {
                    info.add(
                        'DeviceInfo',
                        `<table><tbody>
                        <tr><td class="eruda-device-key">name</td><td>${deviceInfo.name}</td></tr>
                        <tr><td>model</td><td>${deviceInfo.model}</td></tr>
                        <tr><td>platform</td><td>${deviceInfo.platform}</td></tr>
                        <tr><td>operatingSystem</td><td>${deviceInfo.operatingSystem}</td></tr>
                        <tr><td>osVersion</td><td>${deviceInfo.operatingSystem}</td></tr>
                        <tr><td>manufacturer</td><td>${deviceInfo.manufacturer}</td></tr>
                        <tr><td>isVirtual</td><td>${deviceInfo.isVirtual}</td></tr>
                        <tr><td>memUsed</td><td>${deviceInfo.memUsed}</td></tr>
                        <tr><td>diskFree</td><td>${deviceInfo.diskFree}</td></tr>
                        <tr><td>diskTotal</td><td>${deviceInfo.diskTotal}</td></tr>
                        <tr><td>realDiskFree</td><td>${deviceInfo.realDiskFree}</td></tr>
                        <tr><td>realDiskTotal</td><td>${deviceInfo.realDiskTotal}</td></tr>
                        <tr><td>webViewVersion</td><td>${deviceInfo.webViewVersion}</td></tr>
                        </tbody></table>`
                    )
                })
            }
        })
        .catch(e => {
            loading = false
            console.log(e)
        })
}
export { init }
