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.GroupUserInfoBean;
import com.common.im.contract.MessageFragmentContract;
import com.common.im.bean.RongIMGroupInfo;
import com.common.im.bean.RongIMUserInfo;
import com.cooleshow.base.common.BaseApplication;
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 io.rong.imkit.RongIM;
import io.rong.imlib.model.Group;

/**
 * Author by pq, Date on 2022/5/6.
 */
public class MessagePresenter extends BasePresenter<MessageFragmentContract.MessageFragmentView> implements MessageFragmentContract.Presenter {

    public void queryFriendDetail(Context context, String userId) {
        //根据用户编号获取用户基本信息
        if (!BaseApplication.Companion.isTeacherClient()) {
            addSubscribe(create(IMApi.class).queryStuFriendDetail(userId), new BaseObserver<RongIMUserInfo>(getView()) {
                @Override
                protected void onSuccess(RongIMUserInfo data) {
                    if (getView() != null) {
                        getView().onQueryFriendDetail(data);
                    }
                }
            });
        } else {
            addSubscribe(create(IMApi.class).queryTeaFriendDetail(userId), new BaseObserver<RongIMUserInfo>(getView()) {
                @Override
                protected void onSuccess(RongIMUserInfo data) {
                    if (getView() != null) {
                        getView().onQueryFriendDetail(data);
                    }
                }
            });
        }

    }

    /**
     * 查询群成员信息
     * @param groupId
     * @param userId
     */
    public void queryGroupFriendDetail(String groupId, String userId) {
        //根据用户编号获取用户基本信息
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.putOpt("groupId", groupId);
            jsonObject.putOpt("userId", userId);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        if (BaseApplication.Companion.isTeacherClient()) {
            addSubscribe(create(IMApi.class).queryGroupFriendDetailForTeacher(RequestBodyUtil.convertToRequestBodyJson(jsonObject.toString())), new BaseObserver<GroupUserInfoBean>(getView()) {
                @Override
                protected void onSuccess(GroupUserInfoBean data) {
                    if (getView() != null) {
                        getView().onQueryGroupUserInfoSuccess(data);
                    }
                }
            });
        } else {
            addSubscribe(create(IMApi.class).queryGroupFriendDetailForStu(RequestBodyUtil.convertToRequestBodyJson(jsonObject.toString())), new BaseObserver<GroupUserInfoBean>(getView()) {
                @Override
                protected void onSuccess(GroupUserInfoBean data) {
                    if (getView() != null) {
                        getView().onQueryGroupUserInfoSuccess(data);
                    }
                }
            });
        }

    }


    //查询群详情
    public void queryGroupDetail(Context context, String groupId) {
        if (!BaseApplication.Companion.isTeacherClient()) {
            addSubscribe(create(IMApi.class).queryStuGroupDetail(groupId), 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(groupId), 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);
                }
            });
        }
    }


}
