/*工具函数模块*/
import axios from 'axios'

// 获取绝对位置的横坐标和纵坐标
export function getElementLeft(element){
    var actualLeft = element.offsetLeft;
    var current = element.offsetParent;
    while (current !== null){
        actualLeft += current.offsetLeft;
        current = current.offsetParent;
    }
    return actualLeft;
}

export function getElementTop(element){
    var actualTop = element.offsetTop;
    var current = element.offsetParent;
    while (current !== null){
        actualTop += current.offsetTop;
        current = current.offsetParent;
    }
    return actualTop;
}

// 通过axios静态获取本地资源
export function getStaticFile(url) {
    return new Promise(function (resolve) {
        let promise
        promise = axios.get(`static/Data/${url}`)

        promise.then(function (response) {
            resolve(response.data)
        })
    })
}

// 生产guid的工具函数
export function guId() {
    return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
        var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8)
        return v.toString(16)
    })
}

// 根据当前分屏的宽高和栅格位置和大屏在视口中的位置来确定元素定位
export function elePosition(width,height,count,top,left,splitNum) {
    if(splitNum === 1){
        // 1分屏
        return {top:top,left:left}
    }else if(splitNum === 2){
        // 四分屏
        if(count<=2){
            return {top:top,left:left+(count-1)*width}
        }else{
            return {top:top+height,left:left+(count-3)*width}
        }
    }else if(splitNum === 3){
        // 九分屏
        if(count<=3){
            return {top:top,left:left+(count-1)*width}
        }else if(count<=6){
            return {top:top+height,left:left+(count-4)*width}
        }else{
            return {top:top+2*height,left:left+(count-7)*width}
        }
    }else if(splitNum === 4){
        // 十六分屏
        if(count<=4){
            return {top:top,left:left+(count-1)*width}
        }else if(count<=8){
            return {top:top+height,left:left+(count-5)*width}
        }else if(count<=12){
            return {top:top+2*height,left:left+(count-9)*width}
        }else {
            return {top:top+3*height,left:left+(count-13)*width}
        }
    }
}

// 获取每个window窗口上需要挂载的元素（接受windowId,返回需要挂载的元素）
export async function getNeedMoutedEleArr (windowId){
    const arr = []
    // 1.大屏
    const bigScreenJson = await getStaticFile('EnityBigScreen.Data')
    for (const bigscreen of bigScreenJson){
        if(bigscreen.WindowID === windowId){
            arr.push(bigscreen)
        }
    }
    // 2.按钮
    const buttonJson = await getStaticFile('EnityButton.Data')
    for (const button of buttonJson){
        if(button.WindowID === windowId){
            arr.push(button)
        }
    }
    // 3.label
    const labelJson = await getStaticFile('EnityLable.Data')
    for (const label of labelJson){
        if(label.WindowID === windowId){
            arr.push(label)
        }
    }
    // 4.image
    const imageJson = await getStaticFile('EnityImage.Data')
    for (const image of imageJson){
        if(image.WindowID === windowId){
            arr.push(image)
        }
    }
    return arr
}


