package com.sybotan.android.demo.retrofit;

import com.google.gson.Gson;
import com.sybotan.android.demo.viewmodel.BaseViewModel;

import org.json.JSONObject;

import java.util.HashMap;

import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.schedulers.Schedulers;
import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * Created by weimo on 2020/7/13.
 */
public abstract class BaseRemoteData {
    private CompositeDisposable compositeDisposable;
    private BaseViewModel baseViewModel;
    protected final String pubName = "ipad";
    protected String macAddress;
    private Gson mGson;

    public BaseRemoteData(BaseViewModel baseViewModel) {
        this.baseViewModel = baseViewModel;
        this.compositeDisposable = new CompositeDisposable();
        //华为
//        macAddress = "34:46:ec:aa:55:41";
        //联想
//        macAddress = "84:B8:B8:10:A7:83";
//        macAddress = "BE:66:AB:F8:77:C4";
        mGson = new Gson();
    }

    private void showLoading(String message) {
        if (baseViewModel != null) {
            baseViewModel.showLoading(message);
        }
    }

    private void dismissLoading() {
        if (baseViewModel != null) {
            baseViewModel.dismissLoading();
        }
    }

    /**
     * 取消订阅
     */
    public void dispose() {
        if (!compositeDisposable.isDisposed()) {
            compositeDisposable.dispose();
        }
    }

    public BaseViewModel getBaseViewModel() {
        return baseViewModel;
    }

    /**
     * 发送网络请求
     * @param observable
     * @param observer
     * @param isShow
     * @param message
     * @param <T>
     */
    protected <T> void toSubcribe(Observable<T> observable, Observer<T> observer, boolean isShow, String message) {
        observable.subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .doOnSubscribe(disposable -> {//允许我们在观察者订阅时立即执行动作
                    if (disposable != null) {
                        compositeDisposable.add(disposable);
                    }
                    if (isShow) {
                        showLoading(message);
                    }
                }).subscribe(observer);
    }

    /**
     * 请求body包装
     *
     * @param requestBean 请求body
     * @param <T>         body类泛型
     * @return RequestBody包装
     */
    public <T> RequestBody getRequestBody(T requestBean) {
        String params, contentType = "";
        if (requestBean instanceof JSONObject) {
            JSONObject jsonObject = (JSONObject) requestBean;
            params = jsonObject.toString();
        } else if (requestBean instanceof HashMap) {
            HashMap<Object, Object> hashMap = (HashMap<Object, Object>) requestBean;
            params = mGson.toJson(hashMap);
        } else {
            params = mGson.toJson(requestBean);
        }
        contentType = "application/json";
        return RequestBody.create(MediaType.parse(contentType), params);
    }
}
