package com.common.im.presenter;

import android.content.Context;

import com.common.im.api.IMApi;
import com.common.im.bean.ContactListBean;
import com.common.im.bean.ContactRoomListBean;
import com.common.im.contract.ContactRoomListFragmentContract;
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 java.util.List;

/**
 * 创建日期：2022/6/10 11:32
 *
 * @author Ryan
 * 类说明：
 */
public class ContactRoomListPresenter extends BasePresenter<ContactRoomListFragmentContract.ContactRoomListFragmentView> implements ContactRoomListFragmentContract.Presenter {

    @Override
    public void queryGroupList(Context context, String searchCondition) {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.putOpt("search", searchCondition);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        if (!BaseApplication.Companion.isTeacherClient()) {
            addSubscribe(create(IMApi.class).queryStuGroupList(RequestBodyUtil.convertToRequestBodyJson(jsonObject.toString())), new BaseObserver<List<ContactRoomListBean>>(getView()) {
                @Override
                protected void onSuccess(List<ContactRoomListBean> data) {
                    if (getView() != null) {
                        getView().getGroupListSuccess(data);
                    }
                }

                @Override
                public void onError(Throwable e) {
                    super.onError(e);
                    if (getView() != null) {
                        getView().getGroupListError();
                    }
                }
            });
        }else{
            addSubscribe(create(IMApi.class).queryTeaGroupList(RequestBodyUtil.convertToRequestBodyJson(jsonObject.toString())), new BaseObserver<List<ContactRoomListBean>>(getView()) {
                @Override
                protected void onSuccess(List<ContactRoomListBean> data) {
                    if (getView() != null) {
                        getView().getGroupListSuccess(data);
                    }
                }

                @Override
                public void onError(Throwable e) {
                    super.onError(e);
                    if (getView() != null) {
                        getView().getGroupListError();
                    }
                }
            });
        }

    }
}