
import IMSDK,{IMMethods} from "openim-uniapp-polyfill";
import {offlinePushInfo} from "@/util/imCommon";
import {upload} from "@/api/login.js";
import {getVideoInfo} from "@/util/common";
export const imapi = (method,data)=>{
    return new Promise((resolve, reject) => {
        IMSDK.asyncApi(method,IMSDK.uuid(),data)
        .then(res=>{
            return resolve(res);
        })
        .catch(e=>{
            return reject(e);
        })
    });
};
export const copyFileToTempPath = (src) => {
    //plus.io.PRIVATE_DOC;
    console.log('src', src);
    console.log('src', src.includes(plus.io.PRIVATE_DOC));
    return new Promise((resolve, reject) => {
        plus.io.resolveLocalFileSystemURL(src, (entry) => {
            console.log('entry name', entry.name);
            console.log('dir', '_doc/uniapp_temp/');
            entry.copyTo('_doc/uniapp_temp/', entry.name, (newEntry) => {
                console.log('newEntry', newEntry.fullPath);
                resolve(newEntry.toLocalURL());
            }, reject);
        }, reject);
    });
}
export const getPurePath = (path)=>{
    if(!path)return "";
	const prefix = "file://";
	const relativeRrefix = "_doc/";
	if (path.includes(prefix)) {
		path = path.replace(prefix, "");
	}
	if (path.includes(relativeRrefix)) {
		path = plus.io.convertLocalFileSystemURL(path);
	}
	return path;
}
export const createVoiceMessage = async (path)=>{
};
export const createFileMessage = async (path)=>{
};
export const getVideoCover = async (path)=>{
    return new Promise((resolve, reject) => {
        IMSDK.getVideoCover(path)
        .then(res=>{
            console.log('res', res);
            return resolve(res);
        })
        .catch(e=>{
            console.log('e', e);
            return reject(e);
        })
    });
};
export const createVideoMessage = (path)=>{
    return new Promise(async(resolve, reject) => {
        console.log('处理前的可用路径', path);
        const realVideoPath = await copyFileToTempPath(path);
        //const realVideoPath = await getPurePath(path);
        console.log('处理后的可用路径', realVideoPath);
        const info = await getVideoInfo(realVideoPath);
        console.log('info', info);
        const cover = await getVideoCover(realVideoPath);
        console.log('cover', cover);
        const coverresult = await upload(cover,{savePath: "msg"});
        console.log('coverresult', coverresult);
        if(coverresult.code !=0){
            uni.$u.toast(coverresult.msg);
            return reject(coverresult.msg);
        }
        const videoresult = await upload(realVideoPath,{savePath: "msg"},(res)=>{
            uni.showLoading({
                title: res.progress+"%",
            })
        });
        console.log('videoresult', videoresult);
        uni.hideLoading();
        
        if(videoresult.code !=0){
            uni.$u.toast(videoresult.msg);
            return reject(videoresult.msg);
        }
        imapi(IMMethods.CreateVideoMessageByURL,{
            videoPath: realVideoPath,
            duration: info.duration,
            videoType: info.type.split("/")[1],
            snapshotPath: cover,
            videoUUID: IMSDK.uuid(),
            videoUrl: videoresult.data[0].file_url,
            videoSize: videoresult.data[0].size,
            snapshotUUID: IMSDK.uuid(),
            snapshotSize: coverresult.data[0].size,
            snapshotUrl: coverresult.data[0].file_url,
            snapshotWidth: coverresult.data[0].file_width,
            snapshotHeight: coverresult.data[0].file_height,
            snapShotType: coverresult.data[0].extension,
        }).then(res=>{
            console.log('res', res);
            resolve(res);
        }).catch(err=>{
            console.log('err', err);
            reject(err);
        })
        return ;
        //const realVideoPath = await getPurePath(path);
        //console.log('处理后的可用路径', realVideoPath);
        //const info = await getVideoInfo(realVideoPath);
        //const cover = await getVideoCover(path);
        //const res1 = await IMSDK.getVideoCover(path);
        //console.log(res1.path);
        const videoParams = {
            videoPath: realVideoPath,
            videoType: "mp4",
            duration: info.duration,
            snapshotPath: getPurePath(res1.path),
            //snapshotPath: getPurePath(cover),
        };
        console.log('videoParams', videoParams);
        message = await IMSDK.asyncApi(
            IMMethods.CreateVideoMessageFromFullPath,
            IMSDK.uuid(),
            videoParams
        );
    });
};
export const createImageMessage = async (path)=>{
    return new Promise(async (resolve, reject) => {
        const result = await upload(path,{savePath: "msg"});
        if(result.code !=0){
        	uni.$u.toast(result.msg);
        	return ;
        }
        const picBaseInfo = {
        	uuid: IMSDK.uuid(),
        	type: result.data[0].extension,
        	size: result.data[0].size,
        	width: result.data[0].file_width,
        	height: result.data[0].file_height,
        	url: result.data[0].file_url
        };

        const message = await IMSDK.asyncApi(IMMethods.CreateImageMessageByURL,IMSDK.uuid(),{
            sourcePicture: picBaseInfo,
            bigPicture: picBaseInfo,
            snapshotPicture: picBaseInfo,
            sourcePath: result.data[0].file_url
        });
        console.log('message', message);
        return resolve(message);
        const tempPath = await copyFileToTempPath(path);
        console.log(tempPath);
        imapi(IMMethods.CreateImageMessageFromFullPath,getPurePath(tempPath)).then(res=>{
            console.log(res);
            resolve(res);
        }).catch(err=>{
            console.log(err);
            reject(err);
        })
    });
}
export const sendMessage = (message, user_id, group_id) => {
    let method = IMMethods.SendMessage;
    if([MessageType.PictureMessage,MessageType.VoiceMessage,MessageType.VideoMessage,MessageType.FileMessage].includes(message.contentType)){
        method = IMMethods.SendMessageNotOss;
    }
    return imapi(method,{recvID: user_id,groupID: group_id,message,offlinePushInfo,});
};
export default {
    imapi,
    sendMessage,
    createVoiceMessage,
    createVideoMessage,
    createFileMessage,
    createImageMessage,
    getPurePath,
    copyFileToTempPath,
}