package com.cooleshow.teacher.presenter.message;

import android.text.TextUtils;

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 com.cooleshow.teacher.api.APIService;
import com.cooleshow.teacher.bean.CountOfUnreadBean;
import com.cooleshow.teacher.bean.SystemMessageBean;
import com.cooleshow.teacher.contract.MessageBoxContract;

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

import java.util.List;

/**
 * 创建日期：2022/5/24 11:52
 *
 * @author Ryan
 * 类说明：
 */
public class MessageBoxPresenter extends BasePresenter<MessageBoxContract.MessageBoxView> implements MessageBoxContract.Presenter {


    public void sysMessageList(boolean isLoading, int page, String group) {
        if (isLoading && getView() != null) {
            getView().showLoading();
        }
        JSONObject jsonObject = new JSONObject();
        try {
            if (!TextUtils.isEmpty(group)) {
                jsonObject.put("group", group);
            }
            //消息类型；1，表示短信；2，表示邮件; 3,app推送消息
            jsonObject.put("type", 3);
            jsonObject.putOpt("page", page);
            jsonObject.putOpt("rows", Constants.DEFAULT_DATA_SIZE);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        addSubscribe(create(APIService.class).sysMessageList(RequestBodyUtil.convertToRequestBodyJson(jsonObject.toString())), new BaseObserver<SystemMessageBean>(getView()) {
            @Override
            protected void onSuccess(SystemMessageBean data) {
                if (getView() != null) {
                    getView().sysMessageListSuccess(page, data);
                }
            }

            @Override
            public void onComplete() {
                super.onComplete();
                getView().hideLoading();
            }

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

    public void batchSetRead() {
        getView().showLoading();
        addSubscribe(create(APIService.class).batchSetRead(), new BaseObserver<Object>(getView()) {
            @Override
            protected void onSuccess(Object data) {
                if (getView() != null) {
                    getView().batchSetReadSuccess();
                }
            }

            @Override
            public void onComplete() {
                super.onComplete();
                getView().hideLoading();
            }

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

            }
        });
    }

    public void setCurrentRead(long id){
        getView().showLoading();
        addSubscribe(create(APIService.class).setCurrentRead(id), new BaseObserver<Object>(getView()) {
            @Override
            protected void onSuccess(Object data) {
                if (getView() != null) {
                    getView().setCurrentReadSuccess(id);
                }
            }

            @Override
            public void onComplete() {
                super.onComplete();
                getView().hideLoading();
            }

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

            }
        });
    }


    public void queryCountOfUnread() {
        getView().showLoading();
        addSubscribe(create(APIService.class).queryCountOfUnread(), new BaseObserver<List<CountOfUnreadBean>>(getView()) {
            @Override
            protected void onSuccess(List<CountOfUnreadBean> data) {
                if (getView() != null) {
                    getView().queryCountOfUnreadSuccess(data);
                }
            }

            @Override
            public void onComplete() {
                super.onComplete();
                getView().hideLoading();
            }

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

            }
        });
    }
}
