package com.common.im.presenter;

import com.common.im.api.IMApi;
import com.common.im.bean.GroupNoticeBean;
import com.common.im.contract.NoticeContract;
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 io.rong.imkit.api.APIService;

/**
 * 创建日期：2022/6/11 18:09
 *
 * @author Ryan
 * 类说明：
 */
public class NoticePresenter extends BasePresenter<NoticeContract.NoticeView> implements NoticeContract.Presenter {
    public void queryGroupNotice(String groupId, int page) {
        if (getView() != null) {
            getView().showLoading();
        }
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.putOpt("groupId", groupId);
            jsonObject.putOpt("page", page);
            jsonObject.putOpt("rows", Constants.DEFAULT_DATA_SIZE);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        addSubscribe(create(IMApi.class).queryTeaGroupNotice(RequestBodyUtil.convertToRequestBodyJson(jsonObject.toString())), new BaseObserver<GroupNoticeBean>(getView()) {
            @Override
            protected void onSuccess(GroupNoticeBean data) {
                if (getView() != null) {
                    getView().queryGroupNoticeSuccess(page, data);
                }
            }

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

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

    public void noticeUpdate(long id, String targetId, String title, String notice, boolean topFlag) {
        if (getView() != null) {
            getView().showLoading();
        }
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.putOpt("id", id);
            jsonObject.putOpt("title", title);
            jsonObject.putOpt("content", notice);
            jsonObject.putOpt("groupId", targetId);
            jsonObject.putOpt("topFlag", topFlag);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        addSubscribe(create(IMApi.class).updateGroupNotice(RequestBodyUtil.convertToRequestBodyJson(jsonObject.toString())), new BaseObserver<Object>(getView()) {
            @Override
            protected void onSuccess(Object data) {
                if (getView() != null) {
                    getView().updateGroupNoticeSuccess();
                }
            }

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

    }

    public void noticeDel(String id) {
        addSubscribe(create(IMApi.class).noticeDel(id), new BaseObserver<String>(getView()) {
            @Override
            protected void onSuccess(String data) {
                if (getView() != null) {
                    getView().delGroupNoticeSuccess();
                }
            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
            }
        });
    }

}
