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.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 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);
                    }
                }
            });
        }

    }


    //查询群详情
    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);
                }
            });
        }
    }



}
