package com.cooleshow.student.presenter.mine;

import com.cooleshow.base.data.net.ApiException;
import com.cooleshow.base.presenter.BasePresenter;
import com.cooleshow.base.rx.BaseObserver;
import com.cooleshow.base.utils.RequestBodyUtil;
import com.cooleshow.base.utils.ToastUtils;
import com.cooleshow.base.utils.ErrorParse;
import com.cooleshow.student.api.APIService;
import com.cooleshow.student.contract.EditAddressContract;

/**
 * 创建日期：2022/5/18 10:20
 *
 * @author Ryan
 * 类说明：
 */
public class EditAddressPresenter extends BasePresenter<EditAddressContract.EditAddressView> implements EditAddressContract.Presenter {

    public void createNewAddress(String jsonStr) {
        getView().showLoading();
        addSubscribe(create(APIService.class).createNewAddress(RequestBodyUtil.convertToRequestBodyJson(jsonStr)), new BaseObserver<Object>(getView()) {
            @Override
            protected void onSuccess(Object data) {
                if (getView() != null) {
                    getView().createNewAddressSuccess(data);
                }
            }

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

            @Override
            public void onError(Throwable e) {
                super.onError(e);
                if (e instanceof ApiException) {
                    ErrorParse.getInstance().parseError(e);
                    ApiException apiException = (ApiException) e;
                    ToastUtils.showShort(apiException.getErrmsg());
                }
            }
        });
    }

    public void updateAddress(int id,String jsonStr) {
        getView().showLoading();
        addSubscribe(create(APIService.class).updateAddress(id,RequestBodyUtil.convertToRequestBodyJson(jsonStr)), new BaseObserver<Object>(getView()) {
            @Override
            protected void onSuccess(Object data) {
                if (getView() != null) {
                    getView().createNewAddressSuccess(data);
                }
            }

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

            @Override
            public void onError(Throwable e) {
                super.onError(e);
                if (e instanceof ApiException) {
                    ErrorParse.getInstance().parseError(e);
                    ApiException apiException = (ApiException) e;
                    ToastUtils.showShort(apiException.getErrmsg());
                }
            }
        });
    }

}
