package com.hainaos.vc.activity.vip;

import android.util.Log;

import androidx.lifecycle.MutableLiveData;

import com.hainaos.vc.base.mvvm.BaseViewModel;
import com.hainaos.vc.bean.ActivationInfo;
import com.hainaos.vc.bean.BaseResponse;
import com.hainaos.vc.bean.SpaceInfo;
import com.hainaos.vc.databinding.ActivityVipBinding;
import com.hainaos.vc.network.NetInterfaceManager;
import com.trello.rxlifecycle4.RxLifecycle;
import com.trello.rxlifecycle4.android.ActivityEvent;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.disposables.Disposable;
import io.reactivex.rxjava3.schedulers.Schedulers;

public class VipViewModel extends BaseViewModel<ActivityVipBinding, ActivityEvent> {
    @Override
    public ActivityVipBinding getVDBinding() {
        return binding;
    }

    @Override
    public void onDestroy() {

    }

    public MutableLiveData<SpaceInfo> mSpaceInfoData = new MutableLiveData<>();

    public void getHomeSpaceInfo() {
        NetInterfaceManager.getInstance().getHomeSpaceInfoControl()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(RxLifecycle.bindUntilEvent(getLifecycle(), ActivityEvent.DESTROY))
                .subscribe(new Observer<BaseResponse<SpaceInfo>>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        Log.e("getHomeSpaceInfo", "onSubscribe: ");
                    }

                    @Override
                    public void onNext(@NonNull BaseResponse<SpaceInfo> spaceInfoBaseResponse) {
                        Log.e("getHomeSpaceInfo", "onNext: " + spaceInfoBaseResponse);
                        if (spaceInfoBaseResponse.code == 200) {
                            SpaceInfo spaceInfo = spaceInfoBaseResponse.data;
                            mSpaceInfoData.setValue(spaceInfo);
                        } else {
                            mSpaceInfoData.setValue(null);
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        Log.e("getHomeSpaceInfo", "onError: " + e.getMessage());
                        onComplete();
                    }

                    @Override
                    public void onComplete() {
                        Log.e("getHomeSpaceInfo", "onComplete: ");
                    }
                });
    }


    public void getActivationInfo() {
        NetInterfaceManager.getInstance()
                .getActivationControl()
                .subscribe(new Observer<BaseResponse<ActivationInfo>>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {

                    }

                    @Override
                    public void onNext(@NonNull BaseResponse<ActivationInfo> baseResponse) {
                        Log.e("getActivationInfo", "onNext: " + baseResponse);
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

}
