package com.rong.io.live.manager;

import android.text.TextUtils;
import android.util.Log;


import com.cooleshow.usercenter.helper.UserHelper;

import java.util.List;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.core.Observable;
import io.reactivex.rxjava3.functions.Function;
import io.reactivex.rxjava3.functions.Predicate;
import io.reactivex.rxjava3.subjects.PublishSubject;
import io.rong.imlib.IRongCallback;
import io.rong.imlib.IRongCoreCallback;
import io.rong.imlib.IRongCoreEnum;
import io.rong.imlib.RongCoreClient;
import io.rong.imlib.RongIMClient;
import io.rong.imlib.model.Conversation;
import io.rong.imlib.model.Message;
import io.rong.imlib.model.MessageContent;
import io.rong.imlib.model.UserInfo;
import kotlin.jvm.functions.Function1;
import kotlin.jvm.functions.Function2;

/**
 * @author lihao
 * @project RC RTC
 * @date 2022/2/16
 * @time 10:17 上午
 * 统一发送消息管理类
 */
public class RCChatRoomMessageManager {
    public static PublishSubject<MessageWrapperModel> messageSubject = PublishSubject.create();

    /**
     * 发送消息集合
     *
     * @param roomId
     * @param messageContents
     * @param showLocation
     * @param onSuccess
     * @param onError
     */
    public static void sendChatMessages(String roomId,
                                        List<MessageContent> messageContents,
                                        Boolean showLocation,
                                        Function1 onSuccess,
                                        Function2 onError) {
        for (MessageContent messageContent : messageContents) {
            sendChatMessage(roomId, messageContent, showLocation, onSuccess, onError);
        }
    }

    /**
     * 发送单条消息
     *
     * @param roomId
     * @param messageContent
     * @param showLocation
     * @param onSuccess
     * @param onError
     */
    public static void sendChatMessage(String roomId,
                                       MessageContent messageContent,
                                       Boolean showLocation,
                                       Function1 onSuccess,
                                       Function2 onError) {
        //设置用户信息
        messageContent.setUserInfo(new UserInfo(UserHelper.getUserId(),UserHelper.getUserName(),null));
        Message message = Message.obtain(roomId, Conversation.ConversationType.CHATROOM, messageContent);
        RongIMClient.getInstance().sendMessage(message, null, null, new IRongCallback.ISendMessageCallback() {
            @Override
            public void onAttached(Message message) {

            }

            @Override
            public void onSuccess(Message message) {
                if (message != null) {
                    if (onSuccess != null) onSuccess.invoke(message.getMessageId());
                    if (showLocation) {
                        messageSubject.onNext(new MessageWrapperModel(roomId, message));
                    }
                }

            }

            @Override
            public void onError(Message message, RongIMClient.ErrorCode errorCode) {
                if (message != null) {
                    if (onError != null)
                        onError.invoke(errorCode, message.getMessageId());
                }
            }
        });
    }

    /**
     * 发送本地消息，不执行发送逻辑，只做转发到本地接收处
     *
     * @param roomId
     * @param message
     */
    public static void sendLocationMessage(String roomId, Message message) {
        messageSubject.onNext(new MessageWrapperModel(roomId, message));
    }

    /**
     * 消息回调
     *
     * @param roomId
     * @return
     */
    public static @NonNull
    Observable<Message> obMessageReceiveByRoomId(String roomId) {
        return messageSubject.observeOn(AndroidSchedulers.mainThread()).
                filter(new Predicate<MessageWrapperModel>() {
                    @Override
                    public boolean test(MessageWrapperModel messageWrapperModel) {
                        if (messageWrapperModel.message == null) {
                            return false;
                        }
                        if (TextUtils.equals(messageWrapperModel.roomId, roomId)) {
                            return true;
                        }
                        return false;
                    }
                }).map(new Function<MessageWrapperModel, Message>() {
            @Override
            public Message apply(MessageWrapperModel messageWrapperModel) {
                return messageWrapperModel.message;
            }
        });
    }

    /**
     * 接收消息
     *
     * @param roomId
     * @param msg
     */
    public static void onReceiveMessage(String roomId, Message msg) {
        messageSubject.onNext(new MessageWrapperModel(roomId, msg));
    }

    static class MessageWrapperModel {
        public String roomId;
        public Message message;

        public MessageWrapperModel(String roomId, Message msg) {
            this.roomId = roomId;
            this.message = msg;
        }
    }
}
