package com.mytatvarnsdk.model;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MediatorLiveData;
import androidx.lifecycle.Transformations;

import java.util.List;

import cgmblelib.database.entity.PocDevice;
import cgmblelib.database.entity.PocGlucose;
import cgmblelib.database.repository.RepositoryDevice;
import cgmblelib.database.repository.RepositoryGlucose;

/**
 * 基础的ViewModel
 *
 * @author minyuchun
 */
public class BaseViewModel extends AndroidViewModel {
    /**
     * 设备信息储存库
     */
    private RepositoryDevice mRepositoryDevice;
    /**
     * 血糖信息储存库
     */
    private RepositoryGlucose mRepositoryGlucose;
    /**
     * 最新的设备(数据库中最大Id的设备，无论是绑定还是非绑定)
     */
    private LiveData<PocDevice> mDevice;
    /**
     * 血糖数据集列表持久化
     */
    private LiveData<List<PocGlucose>> mGlucoseList;
    /**
     * 最新的血糖数据持久化，当发射器绑定时，获取最新的，若发射器解除绑定则无
     */
    private LiveData<PocGlucose> mLatestGlucose;
    /***
     * 设备是否绑定
     */
    private MediatorLiveData<Integer> mBindDeviceId = new MediatorLiveData<>();

    public BaseViewModel(@NonNull Application application) {
        super(application);
        mRepositoryDevice = RepositoryDevice.getInstance(application);
        mRepositoryGlucose = RepositoryGlucose.getInstance(application);
        mDevice = mRepositoryDevice.getLiveDataLatestDeviceIoThread();
        mBindDeviceId.addSource(mDevice, device -> {
            if (device != null && device.isBound()) {
                if (mBindDeviceId.getValue() == null || mBindDeviceId.getValue() != device.getId()) {
                    mBindDeviceId.setValue(device.getId());
                }
            } else {
                mBindDeviceId.setValue(0);
            }
        });
        LoadGlucose();
    }

    /**
     * 获取持久化设备数据,获取最新的设备的所有信息
     */
    public synchronized LiveData<PocDevice> getDevice() {
        return mDevice;
    }

    public synchronized LiveData<List<PocGlucose>> getAllGlucose() {
        return mGlucoseList;
    }

    public synchronized LiveData<PocGlucose> getLatestGlucose() {
        return mLatestGlucose;
    }

    /**
     * 获取血糖数据
     */
    private synchronized void LoadGlucose() {
        mGlucoseList = Transformations.switchMap(mBindDeviceId, input -> mRepositoryGlucose.getLiveDataGlucoseListByDeviceId(input));
        mLatestGlucose = Transformations.map(mGlucoseList, input -> {
            if (input != null && input.size() > 0) {
                return input.get(input.size() - 1);
            } else {
                return null;
            }
        });
    }


    public List<PocGlucose> getGlucoseByTime() {
        return mRepositoryGlucose.getGlucoseByDeviceIdAndTimeMillis(7, 1748005587474L);
    }

    public List<PocGlucose> getGlucoseBetweenTime(long startTime) {
        return mRepositoryGlucose.getGlucoseByTimeMillis(startTime, System.currentTimeMillis());
    }

    public PocDevice getDeviceInfo(int deviceId) {
        return mRepositoryDevice.getDeviceByIdIoThread(deviceId);
    }

    public void clearAllGlucoseAndDeviceData() {
        mRepositoryGlucose.deleteAllGlucoseData();
        mRepositoryDevice.deleteAllDeviceData();
    }

}
