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.http.ApiService;
import com.hh.arome.http.HttpManager;
import com.hh.arome.mvp.contract.DeviceConfigContract;
import com.hh.arome.mvp.model.DeviceConfigModel;

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


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

    Context mContext;
    DeviceConfigModel mModel;
    DeviceConfigContract.View mView;

    String url = BASE_URL + BASE_API_URL + API_CONFIG_QUERY;

    public DeviceConfigPresenter(DeviceConfigContract.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 requestDeviceConfig(DeviceConfigRequestBean bean) {
        mModel.requestDeviceConfig(bean)
                .compose(RxHttpResponseCompat.<DeviceConfigResultBean>compatResult())
                .subscribe(new ProgressSubscriber<DeviceConfigResultBean>(mContext, mView, "在线配置拉取 --- " + url) {
                    @Override
                    public void onNext(DeviceConfigResultBean response) {
                        mView.showDeviceConfigData(response);
                    }
                });
    }
}
