package io.rong.imkit.utils.helper;

import android.content.Context;
import android.text.TextUtils;

import com.cooleshow.base.utils.ToastUtil;
import com.cooleshow.base.utils.UiUtils;
import com.cooleshow.base.utils.Utils;

import io.rong.imkit.RongIM;
import io.rong.imkit.widget.RCChatShareLiveMessage;
import io.rong.imkit.widget.RCChatShareMusicMessage;
import io.rong.imlib.IRongCallback;
import io.rong.imlib.MessageTag;
import io.rong.imlib.RongIMClient;
import io.rong.imlib.model.Conversation;
import io.rong.imlib.model.Message;

/**
 * Author by pq, Date on 2022/7/4.
 */
public class OpenChatHelper {
    public static final String SINGLE_CHAT = "single";//单聊

    public static void goChat(Context context, String targetId, String groupName) {
        if (TextUtils.isEmpty(targetId)) {
            return;
        }
        RongIM.getInstance().startPrivateChat(context, targetId, groupName);
    }

    public static void goGroupChat(Context context, String groupId, String groupName) {
        if (UiUtils.isFastClick()) {
            return;
        }
        if (TextUtils.isEmpty(groupId)) {
            ToastUtil.getInstance().showShort("报名未结束，暂无群组");
            return;
        }
        RongIM.getInstance().startGroupChat(context, groupId, groupName);
    }

    public static void sendShareLiveMessage(String targetId, int targetConversationType, String liveRoomId, String teacherAvatar, String teacherName, String liveDes) {
        RCChatShareLiveMessage liveMessage = new RCChatShareLiveMessage();

        liveMessage.setTeacherAvatar(teacherAvatar);
        liveMessage.setRoomUID(liveRoomId);
        liveMessage.setTeacherName(teacherName);
        liveMessage.setLiveDescMessage(liveDes);
        Conversation.ConversationType conversationType = getConversationType(targetConversationType);
        Message locationMsg = Message.obtain(targetId, conversationType, liveMessage);
        MessageTag annotation = liveMessage.getClass().getAnnotation(MessageTag.class);
        if (annotation != null) {
            locationMsg.setObjectName(annotation.value());
        }
        RongIM.getInstance().sendMessage(locationMsg, "直播分享", "[曲谱分享]", new IRongCallback.ISendMessageCallback() {
            @Override
            public void onAttached(Message message) {

            }

            @Override
            public void onSuccess(Message message) {
                ToastUtil.getInstance().showShort("分享成功");
            }

            @Override
            public void onError(Message message, RongIMClient.ErrorCode errorCode) {
                ToastUtil.getInstance().showShort("分享失败");
            }
        });
    }

    public static Conversation.ConversationType getConversationType(int value) {
        Conversation.ConversationType[] values = Conversation.ConversationType.values();
        for (int i = 0; i < values.length; i++) {
            if (values[i].getValue() == value) {
                return values[i];
            }
        }
        return Conversation.ConversationType.PRIVATE;
    }
}
