package com.hh.arome.mvp.presenter;

import android.app.Activity;
import android.content.Context;

import androidx.fragment.app.Fragment;

import com.common.lib_base.common.http.rx.RxHttpResponseCompat;
import com.common.lib_base.common.http.rx.subscriber.ProgressSubscriber;
import com.hh.arome.bean.DeviceConfigRequestBean;
import com.hh.arome.bean.DeviceConfigResultBean;
import com.hh.arome.bean.SimpleResultBean;
import com.hh.arome.http.ApiService;
import com.hh.arome.http.HttpManager;
import com.hh.arome.mvp.contract.DeviceConfigUpdateContract;
import com.hh.arome.mvp.contract.DeviceConfigUpdateContract;
import com.hh.arome.mvp.model.DeviceConfigModel;

import static com.hh.arome.common.Constants.Url.BASE_API_URL;
import static com.hh.arome.common.Constants.Url.API_CONFIG_UPDATE;
import static com.hh.arome.common.Constants.Url.BASE_URL;


/**
 * Date: 2022/8/3 0003 10:51
 * Description:
 */
public class DeviceConfigUpdatePresenter implements DeviceConfigUpdateContract.Presenter {

    Context mContext;
    DeviceConfigModel mModel;
    DeviceConfigUpdateContract.View mView;

    String url = BASE_URL + BASE_API_URL + API_CONFIG_UPDATE;

    public DeviceConfigUpdatePresenter(DeviceConfigUpdateContract.View mView) {
        this.mView = mView;
        if (mView instanceof Fragment) {
            mContext = ((Fragment) mView).getActivity();
        } else {
            mContext = (Activity) mView;
        }
        this.mModel = new DeviceConfigModel(HttpManager.getInstance(mContext.getApplicationContext()).create(ApiService.class));
    }

    @Override
    public void requestDeviceConfigUpdate(DeviceConfigResultBean bean) {
        mModel.requestDeviceConfigUpdate(bean)
                .compose(RxHttpResponseCompat.<SimpleResultBean>compatResult())
                .subscribe(new ProgressSubscriber<SimpleResultBean>(mContext, mView, "在线配置更新 --- " + url) {
                    @Override
                    public void onNext(SimpleResultBean response) {
                        mView.showDeviceConfigUpdateData(response);
                    }
                });
    }
}
