package com.common.im.presenter;

import android.content.Context;
import android.net.Uri;
import android.text.TextUtils;
import android.util.Log;

import com.common.im.api.IMApi;
import com.common.im.bean.GroupMemberBean;
import com.common.im.bean.MusicSheetListBean;
import com.common.im.bean.RongIMGroupInfo;
import com.common.im.contract.ChatGroupSettingContract;
import com.cooleshow.base.common.BaseApplication;
import com.cooleshow.base.constanst.Constants;
import com.cooleshow.base.presenter.BasePresenter;
import com.cooleshow.base.rx.BaseObserver;
import com.cooleshow.base.utils.RequestBodyUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

import io.rong.imkit.RongIM;
import io.rong.imlib.RongIMClient;
import io.rong.imlib.model.Conversation;
import io.rong.imlib.model.Group;

/**
 * 创建日期：2022/6/11 14:42
 *
 * @author Ryan
 * 类说明：
 */
public class ChatGroupSettingPresenter extends BasePresenter<ChatGroupSettingContract.ChatGroupSettingView> implements ChatGroupSettingContract.Presenter {
    public void conversationGet(Conversation.ConversationType conversationType, final String targetId) {
        RongIM.getInstance().getConversationNotificationStatus(conversationType, targetId, new RongIMClient.ResultCallback<Conversation.ConversationNotificationStatus>() {
            @Override
            public void onSuccess(Conversation.ConversationNotificationStatus conversationNotificationStatus) {
                if (getView() != null) {
                    getView().onConversation(conversationNotificationStatus.getValue() == 0 ? true : false);
                }
            }

            @Override
            public void onError(RongIMClient.ErrorCode errorCode) {

            }
        });

    }

    public void conversationMute(Conversation.ConversationType conversationType, String targetId, Conversation.ConversationNotificationStatus notificationStatus) {
        RongIM.getInstance().setConversationNotificationStatus(conversationType, targetId, notificationStatus, new RongIMClient.ResultCallback<Conversation.ConversationNotificationStatus>() {
            @Override
            public void onSuccess(Conversation.ConversationNotificationStatus conversationNotificationStatus) {
                if (getView() != null) {
                    getView().onConversation(true);
                }
            }

            @Override
            public void onError(RongIMClient.ErrorCode errorCode) {

            }
        });
    }

    public void conversationGetUnmute(Conversation.ConversationType conversationType, String targetId, Conversation.ConversationNotificationStatus notificationStatus) {
        RongIM.getInstance().setConversationNotificationStatus(conversationType, targetId, notificationStatus, new RongIMClient.ResultCallback<Conversation.ConversationNotificationStatus>() {
            @Override
            public void onSuccess(Conversation.ConversationNotificationStatus conversationNotificationStatus) {
                if (getView() != null) {
                    getView().onConversation(false);
                }
            }

            @Override
            public void onError(RongIMClient.ErrorCode errorCode) {

            }
        });
    }

    public void queryGroupMembers(Context context, String groupId) {
        String tempGroupId = groupId.replace("S", "").replace("s", "").replace("I", "").replace("i", "");

        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("groupId", tempGroupId);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        if (!BaseApplication.Companion.isTeacherClient()) {
            addSubscribe(create(IMApi.class).queryStuGroupMembers(RequestBodyUtil.convertToRequestBodyJson(jsonObject.toString())), new BaseObserver<List<GroupMemberBean>>(getView()) {
                @Override
                protected void onSuccess(List<GroupMemberBean> data) {
                    if (getView() != null) {
                        getView().onGroupUsers(data, groupId);
                    }
                }

                @Override
                public void onComplete() {
                    super.onComplete();
                    if (getView() != null) {
                        getView().hideLoading();
                    }
                }
            });
        } else {
            addSubscribe(create(IMApi.class).queryTeaGroupMembers(RequestBodyUtil.convertToRequestBodyJson(jsonObject.toString())), new BaseObserver<List<GroupMemberBean>>(getView()) {
                @Override
                protected void onSuccess(List<GroupMemberBean> data) {
                    if (getView() != null) {
                        getView().onGroupUsers(data, groupId);
                    }
                }

                @Override
                public void onComplete() {
                    super.onComplete();
                    if (getView() != null) {
                        getView().hideLoading();
                    }
                }
            });
        }
    }

    //查询群详情
    public void queryGroupDetail(Context context, String groupId) {
        String tempGroupId = groupId.replace("S", "").replace("s", "").replace("I", "").replace("i", "");
        if (!BaseApplication.Companion.isTeacherClient()) {
            addSubscribe(create(IMApi.class).queryStuGroupDetail(tempGroupId), new BaseObserver<RongIMGroupInfo>() {
                @Override
                protected void onSuccess(RongIMGroupInfo data) {
                    if (isViewAttached()) {
                        getView().onQueryGroupDetail(data);
                    }
                    if (data != null) {
                        String id = data.getId()+"";
                        String name = data.getName();
                        if (!TextUtils.isEmpty(id)) {
                            RongIM.getInstance().refreshGroupInfoCache(new Group(id, name, Uri.parse(TextUtils.isEmpty(data.getImg()) ? "" : data.getImg())));
                        }
                    }
                }

            });
        }else{
            addSubscribe(create(IMApi.class).queryTeaGroupDetail(tempGroupId), new BaseObserver<RongIMGroupInfo>() {
                @Override
                protected void onSuccess(RongIMGroupInfo data) {
                    if (isViewAttached()) {
                        getView().onQueryGroupDetail(data);
                    }
                    if (data != null) {
                        String id = data.getId()+"";
                        String name = data.getName();
                        if (!TextUtils.isEmpty(id)) {
                            RongIM.getInstance().refreshGroupInfoCache(new Group(id, name, Uri.parse(TextUtils.isEmpty(data.getImg()) ? "" : data.getImg())));
                        }
                    }
                }

                @Override
                public void onError(Throwable e) {
                    super.onError(e);
                }
            });
        }
    }

}
