"use strict";
import { util } from '../../../util/util.js'
import { fullMap } from '../../../fullMap.js';
import { khcAlert } from '../../../component/alert/khcAlert.js'
import {config} from "../../../config.js";
import {httpUtil} from "../../../util/httpUtil.js";
let functionMap=new Map();// 使用 Map 来存储多个函数。每个函数都通过 set 方法添加到 Map 中，键是函数名，值是对应的函数。
export function main(jsonConfig) {
    try {
        functionMap.get(jsonConfig.button_method).apply(jsonConfig);
    } catch (error) {
        khcAlert.showSystemError(error.message);
        console.log(error);
    }
}
function networkTest(){
    let jsonConfig=this;
    let nsTemplate=`<div class="authentication-wrapper authentication-2 px-4">
    <div class="authentication-inner py-5">
        <!-- Form -->
        <form class="card">
            <div class="p-4 p-sm-5">
                <h5 class="text-center font-weight-normal mb-4" _msttexthash="13531791" _msthash="1">网络状态监控</h5>
                <hr class="mt-0 mb-4">
                <div id="showStatusDiv"></div>
            </div>
        </form>
        <!-- / Form -->
    </div>
</div>`
    document.getElementById(objParentObjID).innerHTML=nsTemplate
    util.loadScript('../css/vendor/libs/knob/knob.js','text/javaScript',function (){
        if ('connection' in navigator) {
            const connection = navigator.connection || navigator.mozConnection || navigator.webkitConnection;
            // 监听网络连接的变化
            connection.addEventListener('change', function() {
                getNetworkStatus()
            });
        }
        getNetworkStatus()
        // Knob
        $('.knob-example input').knob({
            'readonly':"readonly" //只读，不允许修改
        });
    })

}
functionMap.set('networkTest', networkTest);// 将函数添加到 Map 中

function getNetworkStatus(){
    if ('connection' in navigator) {
        const connection = navigator.connection || navigator.mozConnection || navigator.webkitConnection;
        const status = `
            <p>网络类型： ${connection['effectiveType']}</p>
            <p>网络带宽： ${connection['downlink']} Mbps</p>
            <p>网络延迟： ${connection['rtt']} ms</p>
        `;
        document.getElementById('showStatusDiv').innerHTML=status;
    } else {
        document.getElementById('showStatusDiv').innerHTML=`<p>该浏览器不支持 navigator.connection API</p>`;
    }
}
functionMap.set('getNetworkStatus', getNetworkStatus);// 将函数添加到 Map 中