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.LogcatAddRequestBean;
import com.hh.arome.bean.SimpleResultBean;
import com.hh.arome.common.Constants;
import com.hh.arome.http.ApiService;
import com.hh.arome.http.HttpManager;
import com.hh.arome.mvp.contract.LogcatAddContract;
import com.hh.arome.mvp.model.LogcatAddModel;

import static com.hh.arome.common.Constants.IS_OPEN_LOG_URL;
import static com.hh.arome.common.Constants.Url.API_CONFIG_UPDATE;
import static com.hh.arome.common.Constants.Url.API_LOGCAT_ADD;
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 LogcatAddPresenter implements LogcatAddContract.Presenter {

    Context mContext;
    LogcatAddModel mModel;
    LogcatAddContract.View mView;


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


    @Override
    public void requestLogcatAdd(LogcatAddRequestBean bean) {
//        if(!IS_OPEN_LOG_URL){
//            // 关闭请求，以免日志太多
//            return;
//        }
        mModel.requestLogcatAdd(bean)
                .compose(RxHttpResponseCompat.<SimpleResultBean>compatResult())
                .subscribe(new ProgressSubscriber<SimpleResultBean>(mContext, mView, "在线日志--- " + bean.getUrl()) {
                    @Override
                    public void onNext(SimpleResultBean response) {
                        mView.showLogcatAddResult(response);
                    }
                });
    }
}
