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

// 数组去重
export function uniq(array){
    var temp = []; //一个新的临时数组
    for(var i = 0; i < array.length; i++){
        if(temp.indexOf(array[i]) == -1){
            temp.push(array[i]);
        }
    }
    return temp;
}

// 根据给定的位置给栅格元素进行定位
export function gridPosition(splitStatus,count) {
    if(splitStatus === 2){
        // 四分屏
        switch (count) {
            case 1:
                return '1/1/2/2'
            case 2:
                return '1/2/2/3'
            case 3:
                return '2/1/3/2'
            case 4:
                return '2/2/3/3'
        }
    }else if(splitStatus === 3){
        // 九分屏
        switch (count) {
            case 1:
                return '1/1/2/2'
            case 2:
                return '1/2/2/3'
            case 3:
                return '1/3/2/4'
            case 4:
                return '2/1/3/2'
            case 5:
                return '2/2/3/3'
            case 6:
                return '2/3/3/4'
            case 7:
                return '3/1/4/2'
            case 8:
                return '3/2/4/3'
            case 9:
                return '3/3/4/4'
        }
    }else if(splitStatus === 4){
        // 十六分屏
        switch (count) {
            case 1:
                return '1/1/2/2'
            case 2:
                return '1/2/2/3'
            case 3:
                return '1/3/2/4'
            case 4:
                return '1/4/2/5'
            case 5:
                return '2/1/3/2'
            case 6:
                return '2/2/3/3'
            case 7:
                return '2/3/3/4'
            case 8:
                return '2/4/3/5'
            case 9:
                return '3/1/4/2'
            case 10:
                return '3/2/4/3'
            case 11:
                return '3/3/4/4'
            case 12:
                return '3/4/4/5'
            case 13:
                return '4/1/5/2'
            case 14:
                return '4/2/5/3'
            case 15:
                return '4/3/5/4'
            case 16:
                return '4/4/5/5'
        }
    }
}

// 时间戳实现节流函数：
export function throttleFunc(fn, delay) {
    var previous = 0;
    // 使用闭包返回一个函数并且用到闭包函数外面的变量previous
    return function() {
        var _this = this;
        var args = arguments;
        var now = new Date();
        if(now - previous > delay) {
            fn.apply(_this, args);
            previous = now;
        }
    }
}

// 获取绝对位置的横坐标和纵坐标
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}
        }
    }
}


