package com.common.im.presenter;

import com.common.im.api.IMApi;
import com.common.im.contract.NoticeEditContract;
import com.cooleshow.base.presenter.BasePresenter;
import com.cooleshow.base.rx.BaseObserver;
import com.cooleshow.base.utils.RequestBodyUtil;
import com.cooleshow.usercenter.helper.UserHelper;

import org.json.JSONException;
import org.json.JSONObject;

import io.rong.imkit.userinfo.db.model.User;

/**
 * 创建日期：2022/6/11 19:38
 *
 * @author Ryan
 * 类说明：
 */
public class NoticeEditPresenter extends BasePresenter<NoticeEditContract.NoticeEditView> implements NoticeEditContract.Presenter {
    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 createGroupNotice(String title,String notice,String groupId,boolean topFlag){
        if (getView() != null) {
            getView().showLoading();
        }
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.putOpt("avatar", UserHelper.getUserAvatar());
            jsonObject.putOpt("username", UserHelper.getUserName());
            jsonObject.putOpt("title", title);
            jsonObject.putOpt("content", notice);
            jsonObject.putOpt("groupId", groupId);
            jsonObject.putOpt("topFlag", topFlag);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        addSubscribe(create(IMApi.class).createGroupNotice(RequestBodyUtil.convertToRequestBodyJson(jsonObject.toString())), new BaseObserver<Object>(getView()) {
            @Override
            protected void onSuccess(Object data) {
                if (getView() != null) {
                    getView().createGroupNoticeSuccess();
                }
            }

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