import { goeasy } from '../main'
import { IMloginStore } from '@/stores/notice.js';
import GoEasy from 'goeasy'

//链接IM(登录IM)
export function goEasyLink(data) {
    const counter = IMloginStore()
    return new Promise((resolve, reject) => {
        goeasy.connect({
        id:data.id, //im必填，最大长度60字符
        data:{"avatar":data.avatar,"nickname":data.nickname},
        otp:data.key,
        onSuccess: function () {  //连接成功
           console.log("连接成功");
           counter.setIMstate(true); //登录IM成功
           resolve(true);
        },
        onFailed: function (error) { //连接失败
            console.log("连接失败，错误码：" + error.code + "，错误信息：" + error.content);
        },
        onProgress:function(attempts) { //连接或自动重连中
            console.log("正在重连中...");    
        }
    });
    })
}
//断开IM
export function goEasyDisConnect() {
    return new Promise((resolve, reject) => {
         goeasy.disconnect({
            onSuccess: function(){
                resolve(true)
            },
            onFailed: function(error){
                console.log("断开失败, code:"+error.code+ ",error:"+error.content);
            }
        });
    });
}

//获取会话列表
export function goEasyGetConversations() {
    var im = goeasy.im;
    return new Promise((resolve, reject) => {
        im.latestConversations({
            onSuccess: function (result) {
                resolve(result);
            },
            onFailed: function (error) {
                console.log("获取会话列表失败，错误码：" + error.code + " content:" + error.content);
            },
        });
    })
}

//获取指定会话的消息列表
export function goEasyGetMessages(data) {
    var im = goeasy.im;
    return new Promise((resolve, reject) => {
        im.history({
            id: data.id,//用户id或者群id
            type: GoEasy.IM_SCENE.PRIVATE, //群聊：GoEasy.IM_SCENE.GROUP, 客服：GoEasy.IM_SCENE.CS,
            lastTimestamp: data.timestamp, //上次查询结果里最后一条消息的时间戳，首次查询传入null即可
            limit: 30, //可选项，返回的消息条数，默认为10条，最多30条
            onSuccess: function (result) {
                resolve(result.content);
            },
            onFailed: function (error) {
                console.log("获取消息列表失败，错误码：" + error.code + " content:" + error.content);
            }  
        });
    })
}

//发送文本消息
export function goEasySendMessage(data) {
    var im = goeasy.im;
    let textMessage = im.createTextMessage({
        text: data.text, //消息内容
        to: {
            type: GoEasy.IM_SCENE.PRIVATE,   //私聊还是群聊，群聊为GoEasy.IM_SCENE.GROUP
            id: data.id, //接收方用户id
            data: {"avatar": data.avatar, "nickname": data.nickname} //接收方用户扩展数据, 任意格式的字符串或者对象，用于更新会话列表conversation.data
        }
    });
    return new Promise((resolve, reject) => {
        im.sendMessage({
            message: textMessage,
            onSuccess: function () { //发送成功
                resolve(textMessage);
            },
            onFailed: function (error) { //发送失败
                console.log('Failed to send private message，code:' + error.code + ' ,error ' + error.content);
            }
        });
    })
}

//发送图片消息
export function goEasySendImageMessage(data) {
    var im = goeasy.im;
    var message = im.createImageMessage({
        file: data.imagefile, //H5获得的图片file对象，Uniapp和小程序调用chooseImage，success时得到的res.tempFiles数组中的元素，比如res.tempFiles[0]即为选择的第一张图片
        to: {
            type: GoEasy.IM_SCENE.PRIVATE,   //私聊还是群聊，群聊为GoEasy.IM_SCENE.GROUP
            id: data.id, //接收方用户id
            data: {"avatar": data.avatar, "nickname": data.nickname} //好友扩展数据, 任意格式的字符串或者对象，用于更新会话列表conversation.data
        },
    });
    return new Promise((resolve, reject) => {
        im.sendMessage({
            message: message,
            onSuccess: function () {  //发送成功
                resolve(message);
            },
            onFailed: function (error) { //发送失败
                console.log('Failed to send message，code:' + error.code + ',error' + error.content);
            }
        })
    })
}
//发送PK消息
export function goEasySendPKMessage(data) {
    var im = goeasy.im;
    const customData = {
        id: data.msgid,
        pkIdA: data.pkIdA,
        pkIdB: data.pkIdB,
      };

      let order = {
        customData: customData,
        link: "https://vv-1317974657.cos.ap-shanghai.myqcloud.com/util/pk.png",
        text: "PK邀请消息",
      };

      var customMessage = im.createCustomMessage({
        type: 'pk',  //字符串，可以任意自定义类型，比如红包'hongbao', 订单'order，处方'chufang'
        payload: order,
        to: {
            type: GoEasy.IM_SCENE.PRIVATE,   //私聊还是群聊，群聊为GoEasy.IM_SCENE.GROUP
            id: data.id, //接收方用户id
            data: {"avatar": data.avatar, "nickname": data.nickname} //好友扩展数据, 任意格式的字符串或者对象，用于更新会话列表conversation.data
        }
    });

    return new Promise((resolve, reject) => {
        im.sendMessage({
            message: customMessage,
            onSuccess: function () {  //发送成功
                resolve(customMessage);
            },
            onFailed: function (error) { //发送失败
                console.log('Failed to send message，code:' + error.code + ',error' + error.content);
            }
        });
    }) 

}
//消息已读
export function goEasyMessageRead(data) {
    var im = goeasy.im;
    return new Promise((resolve, reject) => {
        im.markMessageAsRead({
            id: data.id,
            type: GoEasy.IM_SCENE.PRIVATE,
            onSuccess: function () {
                resolve(true);
            },
            onFailed: function (error) {
                console.log('标记私聊已读失败', error);
            },
        });
    })
}