package cgmblelib.database.repository;

import android.content.Context;

import androidx.annotation.NonNull;
import androidx.lifecycle.LiveData;

import java.util.Date;
import java.util.List;

import cgmblelib.database.AppDatabase;
import cgmblelib.database.dao.DaoGlucose;
import cgmblelib.database.entity.PocGlucose;
import cgmblelib.database.source.SourceGlucose;
import cgmblelib.database.source.db.GlucoseDBDataSource;
import cgmblelib.utils.AppExecutors;


/**
 * Created by minyuchun on 2018/9/17.
 * repository to database Glucose
 *
 * @author minyuchun
 */
public class RepositoryGlucose implements SourceGlucose {

    private static RepositoryGlucose INSTANCE;

    private final GlucoseDBDataSource mGlucoseDBDataSource;

    public RepositoryGlucose(Context context) {
        DaoGlucose mDaoGlucose = AppDatabase.getInMemoryDatabase(context).mDaoGlucose();
        mGlucoseDBDataSource = GlucoseDBDataSource.getInstance(new AppExecutors(), mDaoGlucose);
    }

    public static RepositoryGlucose getInstance(Context context) {
        if (INSTANCE == null) {
            synchronized (RepositoryGlucose.class) {
                if (INSTANCE == null) {
                    INSTANCE = new RepositoryGlucose(context);
                }
            }
        }
        return INSTANCE;
    }

    @Override
    public void delete(long deviceId, int glucoseId) {
        mGlucoseDBDataSource.delete(deviceId, glucoseId);
    }

    @Override
    public long insertGlucoseIoThread(PocGlucose glucose) {
        return mGlucoseDBDataSource.insertGlucoseIoThread(glucose);
    }

    @Override
    public long[] insertGlucosesIoThread(@NonNull PocGlucose... glucose) {
        return mGlucoseDBDataSource.insertGlucosesIoThread(glucose);
    }

    @Override
    public int updateGlucosesIoThread(@NonNull PocGlucose... glucose) {
        return mGlucoseDBDataSource.updateGlucosesIoThread(glucose);
    }

    @Override
    public LiveData<List<PocGlucose>> getLiveDataGlucoseListByDeviceId(long deviceId) {
        return mGlucoseDBDataSource.getLiveDataGlucoseListByDeviceId(deviceId);
    }

    @Override
    public PocGlucose getLatestGlucoseByDeviceIdIoThread(long deviceId) {
        return mGlucoseDBDataSource.getLatestGlucoseByDeviceIdIoThread(deviceId);
    }

    @Override
    public List<PocGlucose> getGlucosesByIdIoThread(long deviceId) {
        return mGlucoseDBDataSource.getGlucosesByIdIoThread(deviceId);
    }

    @Override
    public List<PocGlucose> getGlucosesByIdIoThreadDesc(long deviceId) {
        return mGlucoseDBDataSource.getGlucosesByIdIoThreadDesc(deviceId);
    }


    @Override
    public List<PocGlucose> getEffectiveGlucosesByIdIoThread(long deviceId) {
        return mGlucoseDBDataSource.getEffectiveGlucosesByIdIoThread(deviceId);
    }

    @Override
    public PocGlucose getGlucoseByIdIoThread(long deviceId, int glucoseId) {
        return mGlucoseDBDataSource.getGlucoseByIdIoThread(deviceId, glucoseId);
    }

    @Override
    public PocGlucose getGlucoseByTimeIoThread(long deviceId, long timeMillis) {
        return mGlucoseDBDataSource.getGlucoseByTimeIoThread(deviceId, timeMillis);
    }


    @Override
    public int getMaxGlucoseIdIoThread(long deviceId) {
        return mGlucoseDBDataSource.getMaxGlucoseIdIoThread(deviceId);
    }

    @Override
    public int getCountGlucoseIoThread(long deviceId) {
        return mGlucoseDBDataSource.getCountGlucoseIoThread(deviceId);
    }

    @Override
    public float[] getIwsIoThread(long deviceId, float Iw) {
        return mGlucoseDBDataSource.getIwsIoThread(deviceId, Iw);
    }

    @Override
    public float[] getIbsIoThread(long deviceId, float Ib) {
        return mGlucoseDBDataSource.getIbsIoThread(deviceId, Ib);
    }


    @Override
    public float[] getTsIoThread(long deviceId, float T) {
        return mGlucoseDBDataSource.getTsIoThread(deviceId, T);
    }

    @Override
    public int[] getYearsIoThread(long deviceId) {
        return mGlucoseDBDataSource.getYearsIoThread(deviceId);
    }

    @Override
    public int[] getMonthsInYearIoThread(long deviceId, int year) {
        return mGlucoseDBDataSource.getMonthsInYearIoThread(deviceId, year);
    }

    @Override
    public int[] getDays(long deviceId, int year, int month) {
        return mGlucoseDBDataSource.getDays(deviceId, year, month);
    }

    @Override
    public List<PocGlucose> getEveryDays(long deviceId, int year, int month, int day) {
        return mGlucoseDBDataSource.getEveryDays(deviceId, year, month, day);
    }

    @Override
    public List<PocGlucose> getAllGlucoseByBetweenTime(long deviceId, Date startTime, Date endTime) {
        return mGlucoseDBDataSource.getAllGlucoseByBetweenTime(deviceId, startTime, endTime);
    }

    @Override
    public List<PocGlucose> getEveryPageData(long deviceId, int page, int pageSize) {
        return mGlucoseDBDataSource.getEveryPageData(deviceId, page, pageSize);
    }

    @Override
    public List<PocGlucose> getLatestPageData(long deviceId, int pageSize) {
        return mGlucoseDBDataSource.getLatestPageData(deviceId, pageSize);
    }

    @Override
    public List<PocGlucose> getAllGlucoseDayData(String name) {
        return mGlucoseDBDataSource.getAllGlucoseDayData(name);
    }

    @Override
    public int getPageCountsIoThread(long deviceId, int pageSize) {
        return mGlucoseDBDataSource.getPageCountsIoThread(deviceId, pageSize);
    }

    @Override
    public List<PocGlucose> getStartDayList(long deviceId, int year, int month, int day, int hour) {
        return mGlucoseDBDataSource.getStartDayList(deviceId, year, month, day, hour);
    }

    @Override
    public List<PocGlucose> getEndDayList(long deviceId, int year, int month, int day, int hour) {
        return mGlucoseDBDataSource.getEndDayList(deviceId, year, month, day, hour);
    }

    @Override
    public List<PocGlucose> getGlucoseByDeviceIdAndTimeMillis(long deviceId, long timeMillisStart) {
        return mGlucoseDBDataSource.getGlucoseByDeviceIdAndTimeMillis(deviceId, timeMillisStart);
    }

    @Override
    public List<PocGlucose> getGlucoseByTimeMillis(long startTime, long endTime) {
        return mGlucoseDBDataSource.getGlucoseByTimeMillis(startTime, endTime);
    }

    @Override
    public void deleteAllGlucoseData() {
        mGlucoseDBDataSource.deleteAllGlucoseData();
    }

}
