import {
    upImage,
    bigDataUpload
} from "../api/common.js";
const upVideo = {
    file: null, //文件对象
    size: 0, //分片大小 kb
    progressBar: 0, //进度条 num%
    underWayFn: null, //进度条改变触发函数
    upOverFn: null, //上传完成触发函数
    t: null, //定时器
    // 初始化函数
    init({
        fileObj, //文件对象
        size = 2, //分片大小 默认2  单位m
        underWayFn = function () {}, //进度条改变触发函数
        upOverFn = function () {}, //上传完成触发函数
    }) {

        this.file = fileObj;
        this.size = size * 1024 * 1024;
        this.underWayFn = underWayFn;
        this.upOverFn = upOverFn;
        // 判断是否是满足条件的视频对象 满足条件调用this.upbegin()处理
        this.upbegin();
    },
    // 上传文件
    async upbegin() {
        let videoTime = await this.videoLong(); //视频播放时间
        // 视频分片
        let start = 0,
            end = 0,
            chunkArr = [],
            size = this.size;
        let file = this.file;

        function chuli() {
            end += size;
            var blob = file.slice(start, end);
            start += size;
            if (blob.size) {
                chunkArr.push(blob);
                chuli(file);
            } else {
                return chunkArr
            }
        }
        chuli();
        //预请求接口 然后this.inTurnto(chunkArr); 分片请求
        // 分片请求主体
        this.inTurnto(chunkArr);
    },
    // 缓慢增长到目标进度条
    changeProgressBar(num) {
        clearInterval(this.t)
        this.t = setInterval(() => {
            if (this.progressBar == num) {
                clearInterval(this.t)
                //上传完成
                if (this.progressBar === 100) {
                    this.upOverFn(); //通知上传完成
                    this.clearUpVideo(); //格式化重置
                }
            } else {
                this.progressBar++;
                this.underWayFn(this.progressBar); //改变进度条通知
            }
        }, 50);
    },

    // 多个视频一一请求
    inTurnto(chunkArr) {
        const chunkAllNum = chunkArr.length; //片段总数量
        let count = 0; //完成个数
        chunkArr.forEach((item, index) => {
            // 模拟数据请求
            setTimeout(() => {
                count++; //增加当前进度
                const filename = new Date().toISOString();
                const data = new FormData();
                const files = new File([item], filename + ".mp4");
                data.append("file", files);
                if (item.size != 2097152) {
                    data.append("isEnd", 1);
                }
                bigDataUpload(data).then((res) => {
                    if (res) {
                    }
                });
                this.changeProgressBar(parseInt(count / chunkAllNum * 100)); //改变进度
            }, 1000);
        })
    },


    // 获取视频总时长
    videoLong() {
        return new Promise((resolve) => {
            var url = URL.createObjectURL(this.file);
            var audioElement = new Audio(url);
            audioElement.addEventListener("loadedmetadata", function () {
                var hour = parseInt((audioElement.duration) / 3600);
                if (hour < 10) {
                    hour = "0" + hour;
                }
                var minute = parseInt((audioElement.duration % 3600) / 60);
                if (minute < 10) {
                    minute = "0" + minute;
                }
                var second = Math.ceil(audioElement.duration % 60);
                if (second < 10) {
                    second = "0" + second;
                }
                resolve(hour + ":" + minute + ":" + second)
            });
        })
    },
    // 重置
    clearUpVideo() {
        this.file = null;
        this.size = 0;
        this.progressBar = 0;
        this.underWayFn = null;
        this.upOverFn = null;
        this.t = null;
    },
}


export default upVideo