
var canvas = "MainPanel";
var ScrollLeft = 0;
var ScrollTop = 0;
var Images = {};
var ImageSource = "go_static/images/";
var ImageNames = ["checkbox_true.png", "checkbox_false.png", "down_white12_8.png","SearchButton.png","down_black10_6.png",
    "doubledown.png", "searchnormal.png", "searchhover.png", "addnew.png","addnewhover.png", "close.png","radioButtonNormal.png",
    "radioButtonHover.png","radioButtonSelected.png","cycle13.png", "unloaded.png", "edit.png","delete.png", "back.png","list.png","person.png", "watch.png","right_black6_10.png", "littleadd.png", "littledelete.png"];

var NewComp = {template:{type:'Stack'}};

var windowChangeEvent = new CustomEvent('WindowChanged', {
    detail: {
        title: 'Window has Changed!',
    },
});

var addImageEvent = new CustomEvent("AddImage", {
    detail:{
        title:'New image has been added to imageset!'
    },
});



var WasmURL= ()=>{
    try {
        if (HostApp == undefined){
            throw e
        }else{
            return 'http://127.0.0.1:8089/test.wasm'
        }
    }catch (e) {
        return 'test.wasm'
    }
}

getImages(ImageNames, ImageSource);

//窗口大小变化，获取窗口大小
window.onresize = findDimensions


if (!WebAssembly.instantiateStreaming) { // polyfill
    WebAssembly.instantiateStreaming = async (resp, importObject) => {
        const source = await (await resp).arrayBuffer();
        return await WebAssembly.instantiate(source, importObject);
    };
}
const go = new Go();
let mod, inst;


async function loadWasm() {
    return WebAssembly.instantiateStreaming(fetch(WasmURL()), go.importObject).then((result) => {
        mod = result.module;
        inst = result.instance;
        button = document.getElementById("runButton");
        // 有button，button变可用，否则，直接调用运行过程
        if (button) {
            button.disabled = false;
        }
    }).catch((err) => {
        console.error(err);
    });
}

async function run() {
    // 加载字体
    // await loadFonts({
    //     'PingFang-SC-Bold':'url(./static/font/PingFang_Bold_1.otf)',
    //     'PingFang-SC-Medium':'url(./static/font/PingFang_Medium_1.otf)',
    // });

    let select = document.getElementById("conv");
    if (select) {
        global.canvas = select.options[select.selectedIndex].value
    }
    let content = document.getElementById("content");
    addCanvas(content)

    //content.innerHTML = '<canvas id="'+ canvas + '" style="width:100%;height:100%;"></canvas>';

    console.log(content.getBoundingClientRect().x, content.getBoundingClientRect().y);
    // console.clear();
    await go.run(inst);
    inst = await WebAssembly.instantiate(mod, go.importObject); // reset instance
}

// 产生canvas元素，并插入content中
function addCanvas(content) {
    let canvasEle = document.createElement("canvas");
    canvasEle.id = canvas
    canvasEle.width = content.offsetWidth
    canvasEle.height = content.offsetHeight
    content.appendChild(canvasEle)
}

async function loadFonts(fonts) {
    for(var key in fonts){
        let myFont = new FontFace(key, fonts[key]);
        let font = await myFont.load();
        document.fonts.add(font)
    }
    
    function getWasmUrl(){
        let url = ''
        try {
            HostApp
            url='http://127.0.0.1:8089/test.wasm'
        }catch (e) {
            url='test.wasm'
        }finally {
            return url
        }
    }
}