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.contract.ContactListFragmentContract;
import com.common.im.contract.MessageFragmentContract;
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;

/**
 * Author by pq, Date on 2022/5/6.
 */
public class ContactListPresenter extends BasePresenter<ContactListFragmentContract.ContactListFragmentView> implements ContactListFragmentContract.Presenter {

    @Override
    public void queryFriendList(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).queryStuFriendList(RequestBodyUtil.convertToRequestBodyJson(jsonObject.toString())), new BaseObserver<List<ContactListBean>>(getView()) {
                @Override
                protected void onSuccess(List<ContactListBean> data) {
                    if (getView() != null) {
                        getView().getFriendListSuccess(data);
                    }
                }

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

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

    }
}
