import Vue from "vue";
// import "@/lib/spark-md5.min.js";
const SparkMD5 = require('spark-md5')


// 缓存
const DicsCatch = {};
// 单级下拉字典项
Vue.prototype.$dicsListsInit = function (selectList) {

    return new Promise((resolve, reject) => {
        let noSelectList = {};
        for (let selectNames in selectList) {
            let dicsName = selectList[selectNames];
            if (!!DicsCatch[dicsName]) {
                this[selectNames] = DicsCatch[dicsName]
            } else {
                noSelectList[selectNames] = dicsName
            }
        }

        if (Object.values(noSelectList).length <= 0) {
            resolve()
        } else {
            this.$axios({
                method: "post",
                url: this.$api.common.getDictListsByTypes,
                data: {
                    dictTypes: Object.values(noSelectList).join(",")
                }
            }).then(res => {
                if (res.code != 200) {
                    this.$message.error(res.message || "字典获取失败");
                    reject();
                    return false;
                }
                let result = res.info;
                for (let selectNames in noSelectList) {
                    let dicsName = noSelectList[selectNames];
                    for (let i = 0; i < result[dicsName].length; i++) {
                        this[selectNames].push({
                            caption: result[dicsName][i].dictName,
                            code: result[dicsName][i].dictCode,
                        });
                    }
                    DicsCatch[dicsName] = this[selectNames]
                }
                resolve();
            }).catch(error => {
                this.$message.error("网络异常");
                reject(error);
            });
        }
    });
}



// 缓存
const DicsTreeCatch = {};
// 单级下拉字典项
Vue.prototype.$dicsTreesInit = function (selectList) {

    return new Promise((resolve, reject) => {
        let noSelectList = {};
        for (let selectNames in selectList) {
            let dicsName = selectList[selectNames];
            if (!!DicsTreeCatch[dicsName]) {
                this[selectNames] = DicsTreeCatch[dicsName]
            } else {
                noSelectList[selectNames] = dicsName
            }
        }

        if (Object.values(noSelectList).length <= 0) {
            resolve()
        } else {
            this.$axios({
                method: "post",
                url: this.$api.common.getDictTreesByTypes,
                data: {
                    dictTypes: Object.values(noSelectList).join(",")
                }
            }).then(res => {
                if (res.code != 200) {
                    this.$message.error(res.message || "字典获取失败");
                    reject();
                    return false;
                }
                let result = res.info;
                for (let selectNames in noSelectList) {
                    let dicsName = noSelectList[selectNames];
                    // for (let i = 0; i < result[dicsName].length; i++) {
                    //     this[selectNames].push({
                    //         name: result[dicsName][i].dictName,
                    //         code: result[dicsName][i].dictCode,
                    //     });
                    // }
                    this[selectNames] = result[dicsName]
                    DicsTreeCatch[dicsName] = this[selectNames]
                }
                resolve();
            }).catch(error => {
                this.$message.error("网络异常");
                reject(error);
            });
        }
    });
}


/**
 * 计算文件的MD5
 * @param file 文件
 * @param chunkSize 分片大小
 * @returns Promise
 */
function md5(file, chunkSize) {
    // element 中组件对 file 进行加工，这里使用未加工的对象，只有未加工的对象才能在 blobSlice.call() 中正常操作
    let fileRaw = file

    // console.log('fileRaw',fileRaw);

    return new Promise((resolve, reject) => {
        let blobSlice = File.prototype.slice || File.prototype.mozSlice || File.prototype.webkitSlice
        let fileReader = new FileReader()
        let chunkSize = 2097152;
        let chunks = Math.ceil(file.size / chunkSize)
        let currentChunk = 0
        let spark = new SparkMD5()

        fileReader.onload = function (e) {
            spark.appendBinary(e.target.result);
            currentChunk++;
            if (currentChunk < chunks) {
                loadNext();
            } else {
                let md5 = spark.end();
                // console.log('computed hash', md5)
                resolve(md5);
            }
        };

        fileReader.onerror = function (e) {
            console.warn('FileReader error.')
            reject(e);
        };

        function loadNext() {
            let start = currentChunk * chunkSize
            let end = ((start + chunkSize) >= file.size) ? file.size : start + chunkSize
            fileReader.readAsBinaryString(blobSlice.call(file, start, end));
        }

        loadNext();
    });
}

// 获取objectPath，由于截取去掉的'0.'后的字符串位数为15-17位不等(不一定是16位)，所以采取拼接的方式
Vue.prototype.$getObjectPathByRandom = function () {
    let a = Math.random().toString().slice(2, 10);
    let b = Math.random().toString().slice(2, 10);
    return a + b;
}

Vue.prototype.$md5 = md5