import streamSaver from "streamsaver";
import "streamsaver/examples/zip-stream.js";
import store from '@/store'
streamSaver.mitm = `${window.location.origin}/mitm.html?version=2.0.0`
// 下载文件并压缩
function zipFiles(zipName, files) {
    return new Promise((resolve) => {
        try {
            console.log("同步下载打包开始时间：" + new Date());
            store.dispatch('trials/setUnLock', true)
            files = formatFiles(files)
            // 创建压缩文件输出流  
            const zipFileOutputStream = streamSaver.createWriteStream(zipName);
            // 创建下载文件流  
            const fileIterator = files.values();
            const readableZipStream = new ZIP({
                async pull(ctrl) {
                    const fileInfo = fileIterator.next();
                    if (fileInfo.done) {//迭代终止  
                        ctrl.close();
                    } else {
                        let { name, url } = fileInfo.value;
                        // url = decodeUtf8(url); // 待定，可能做过特殊处理
                        return fetch(url).then(res => {
                            ctrl.enqueue({
                                name,
                                stream: () => res.body
                            });
                        })
                    }
                }
            });
            if (window.WritableStream && readableZipStream.pipeTo) {
                // 开始下载  
                readableZipStream
                    .pipeTo(zipFileOutputStream)
                    .then(() => {
                        console.log("同步下载打包结束时间：" + new Date());
                        store.dispatch('trials/setUnLock', false)
                        resolve(true)
                    }).catch(err => {
                        console.log(err);
                        resolve(false)
                    });
            } else {
                resolve(false)
            }
        } catch (err) {
            console.log(err);
            resolve(false)
        }
    })

}
// 下载文件并修改名称
async function updateFile(file, name) {
    return new Promise(async resolve => {
        try {
            store.dispatch('trials/setUnLock', true)
            const fileOutputStream = streamSaver.createWriteStream(name);
            // file = decodeUtf8(file);
            let res = await fetch(file);
            res.body.pipeTo(fileOutputStream).then(() => {
                store.dispatch('trials/setUnLock', true)
                resolve(true)
            }).catch(err => {
                console.log(err)
                resolve(false)
            });
        } catch (err) {
            console.log(err)
            resolve(false)
        }
    })
}
//  同名文件修改名称
function formatFiles(files) {
    let fileObj = {};
    files.forEach(file => {
        let arr = Object.keys(fileObj);
        if (!~arr.indexOf(file.name)) {
            fileObj[file.name] = 1;
        } else {
            let name = file.name;
            file.name = name.split(".")[0] + `(${fileObj[name]})` + name
                .substring(name.lastIndexOf('.'))
                .toLocaleLowerCase()
            fileObj[name]++;
        }
    })
    return files;
}
function decodeUtf8(bytes) {
    let str = bytes.split('?');
    let str2 = str[0].split('/');
    let name = str2[str2.length - 1];
    name = encodeURIComponent(name);
    str.shift();
    str2.pop();
    return str2.join("/") + '/' + name;
}
export async function downLoadFile(file, name, type = 'file') {
    if (type === 'zip') return await zipFiles(name, file);
    return await updateFile(file, name)

}