package com.common.im.presenter;

import com.common.im.api.IMApi;
import com.common.im.bean.GroupApplyBean;
import com.common.im.contract.JoinGroupApplyContract;
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/13 16:45
 *
 * @author Ryan
 * 类说明：
 */
public class JoinGroupApplyPresenter extends BasePresenter<JoinGroupApplyContract.JoinGroupApplyView> implements JoinGroupApplyContract.Presenter {

    public void groupApplyList(String groupId){
        if (getView() != null) {
            getView().showLoading();
        }
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.putOpt("auditStatus", "AUDITING");
            jsonObject.putOpt("groupId", groupId);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        addSubscribe(create(IMApi.class).groupApplyList(RequestBodyUtil.convertToRequestBodyJson(jsonObject.toString())), new BaseObserver<List<GroupApplyBean>>(getView()) {
            @Override
            protected void onSuccess(List<GroupApplyBean> data) {
                if (getView() != null) {
                    getView().groupApplyListSuccess(data);
                }
            }

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

    public void doGroupApply(String auditIds,String auditStatus,String groupId){
        if (getView() != null) {
            getView().showLoading();
        }
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.putOpt("auditIds", auditIds);
            jsonObject.putOpt("auditStatus", auditStatus);
            jsonObject.putOpt("groupId", groupId);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        addSubscribe(create(IMApi.class).doGroupApply(RequestBodyUtil.convertToRequestBodyJson(jsonObject.toString())), new BaseObserver<Object>(getView()) {
            @Override
            protected void onSuccess(Object data) {
                if (getView() != null) {
                    getView().doGroupApplySuccess();
                }
            }

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

}
