package cgmblelib.database.entity;

import androidx.annotation.NonNull;
import androidx.room.ColumnInfo;
import androidx.room.Entity;
import androidx.room.ForeignKey;
import androidx.room.Index;
import androidx.room.TypeConverters;

import java.io.Serializable;
import java.util.Arrays;

import cgmblelib.Enum.enumError;
import cgmblelib.Enum.enumGlucoseStatus;
import cgmblelib.Enum.enumTrend;
import cgmblelib.database.ByteConverter;
import cgmblelib.database.DateConverter;
import cgmblelib.database.EnumConverter;
import cgmblelib.utils.Tool;


/**
 * Created by minyuchun on 2019/1/11.
 * 血糖类
 */
@Entity(
        tableName = "glucose",
        primaryKeys = {"deviceId", "glucoseId"},
        foreignKeys = @ForeignKey(entity = PocDevice.class, parentColumns = "id", childColumns = "deviceId"), indices = @Index(value = {"deviceId"}))
@TypeConverters(value = {DateConverter.class, ByteConverter.class, EnumConverter.class})
public class PocGlucose implements Serializable {
    /**
     * device Id
     */
    @ColumnInfo(name = "deviceId")
    private long deviceId;
    /**
     * glucose bytes
     */
    private byte[] bytes;
    /**
     * glucoseId
     */
    private int glucoseId;

    /**
     * 设备名称 读取自数据解析
     */
    private String name;
    /**
     * glucoseData year
     */
    private int year;
    /**
     * glucoseData month
     */
    private int month;
    /**
     * glucoseData day
     */
    private int day;
    /**
     * glucoseData hour
     */
    private int hour;
    /**
     * glucoseData minute
     */
    private int minute;
    /**
     * Iw
     */
    private float Iw;
    /**
     * Ib
     */
    private float Ib;
    /**
     * temperature
     */
    private float T;
    /**
     * first input bg :1 , input newBg after first add 1
     */
    private int BGCount;
    /**
     * input newBg 1 , next glucose add 1
     */
    private int BGICount;
    /**
     * trend
     */
    private enumTrend trend = enumTrend.NONE;
    /**
     * error Code
     */
    private enumError errorCode = enumError.NONE;
    /**
     * 血糖数据显示 -- 2020-11-16
     */
    private float showGlucose = 0;
    /**
     * 血糖数据显示(mg/dL) -- 2020-11-16
     */
    private int showGlucoseMG = 0;
    /**
     * 血糖值的状态 -- 2020-12-17
     */
    private enumGlucoseStatus glucoseStatus;
    /**
     * 低血糖预警
     */
    private int hypoglycemiaEarlyWarnMinutes;

    private int hyperglycemiaEarlyWarnMinutes;

    private int countdownDays;

    private int countdownHours;

    private int countdownMinutes;
    /**数据库版本10的时候新增一个时间戳，目的是为了兼容海外奥地利使用时间戳获取数据的处理*/
    private long timeInMillis;


    public PocGlucose() {

    }

    public long getDeviceId() {
        return deviceId;
    }

    public void setDeviceId(long deviceId) {
        this.deviceId = deviceId;
    }

    public byte[] getBytes() {
        return bytes;
    }

    public void setBytes(byte[] bytes) {
        this.bytes = bytes;
    }

    public int getGlucoseId() {
        return glucoseId;
    }

    public void setGlucoseId(int glucoseId) {
        this.glucoseId = glucoseId;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getYear() {
        return year;
    }

    public void setYear(int year) {
        this.year = year;
    }

    public int getMonth() {
        return month;
    }

    public void setMonth(int month) {
        this.month = month;
    }

    public int getDay() {
        return day;
    }

    public void setDay(int day) {
        this.day = day;
    }

    public int getHour() {
        return hour;
    }

    public void setHour(int hour) {
        this.hour = hour;
    }

    public int getMinute() {
        return minute;
    }

    public void setMinute(int minute) {
        this.minute = minute;
    }

    public float getIw() {
        return Iw;
    }

    public void setIw(float iw) {
        Iw = iw;
    }

    public float getIb() {
        return Ib;
    }

    public void setIb(float ib) {
        Ib = ib;
    }

    public float getT() {
        return T;
    }

    public void setT(float t) {
        T = t;
    }

    public int getBGCount() {
        return BGCount;
    }

    public void setBGCount(int BGCount) {
        this.BGCount = BGCount;
    }

    public int getBGICount() {
        return BGICount;
    }

    public void setBGICount(int BGICount) {
        this.BGICount = BGICount;
    }

    public void setErrorCode(enumError errorCode) {
        this.errorCode = errorCode;
    }

    public void setTrend(enumTrend trend) {
        this.trend = trend;
    }

    public enumError getErrorCode() {
        return errorCode;
    }

    public enumTrend getTrend() {
        return trend;
    }

    public long getTimeInMillis() {
        return timeInMillis;
    }

    public void setTimeInMillis(long timeInMillis) {
        this.timeInMillis = timeInMillis;
    }


    public String getDateOfDay() {
        return (hour < 10 ? "0" + hour : hour) + ":" +
                (minute < 10 ? "0" + minute : minute);
    }

    public String getDate() {
        return year + "/" +
                (month < 10 ? "0" + month : month) + "/" +
                (day < 10 ? "0" + day : day) + " " +
                (hour < 10 ? "0" + hour : hour) + ":" +
                (minute < 10 ? "0" + minute : minute);
    }

    public String getDateWithLine() {
        return year + "-" +
                (month < 10 ? "0" + month : month) + "-" +
                (day < 10 ? "0" + day : day) + " " +
                (hour < 10 ? "0" + hour : hour) + ":" +
                (minute < 10 ? "0" + minute : minute);
    }

    public String getFullDate() {
        return year + "-" +
                (month < 10 ? "0" + month : month) + "-" +
                (day < 10 ? "0" + day : day) + " " +
                (hour < 10 ? "0" + hour : hour) + ":" +
                (minute < 10 ? "0" + minute : minute) +
                ":00";
    }

    public float getShowGlucose() {
        return Tool.getDecimalOneNumber(showGlucose);
    }

    public void setShowGlucose(float showGlucose) {
        this.showGlucose = showGlucose;
    }


    public int getShowGlucoseMG() {
        return showGlucoseMG;
    }

    public void setShowGlucoseMG(int showGlucoseMG) {
        this.showGlucoseMG = showGlucoseMG;
    }

    public enumGlucoseStatus getGlucoseStatus() {
        return glucoseStatus;
    }

    public void setGlucoseStatus(enumGlucoseStatus glucoseStatus) {
        this.glucoseStatus = glucoseStatus;
    }

    public int getHypoglycemiaEarlyWarnMinutes() {
        return hypoglycemiaEarlyWarnMinutes;
    }

    public void setHypoglycemiaEarlyWarnMinutes(int hypoglycemiaEarlyWarnMinutes) {
        this.hypoglycemiaEarlyWarnMinutes = hypoglycemiaEarlyWarnMinutes;
    }

    public int getHyperglycemiaEarlyWarnMinutes() {
        return hyperglycemiaEarlyWarnMinutes;
    }

    public void setHyperglycemiaEarlyWarnMinutes(int hyperglycemiaEarlyWarnMinutes) {
        this.hyperglycemiaEarlyWarnMinutes = hyperglycemiaEarlyWarnMinutes;
    }

    public int getCountdownDays() {
        return countdownDays;
    }

    public void setCountdownDays(int countdownDays) {
        this.countdownDays = countdownDays;
    }

    public int getCountdownHours() {
        return countdownHours;
    }

    public void setCountdownHours(int countdownHours) {
        this.countdownHours = countdownHours;
    }

    public int getCountdownMinutes() {
        return countdownMinutes;
    }

    public void setCountdownMinutes(int countdownMinutes) {
        this.countdownMinutes = countdownMinutes;
    }

    @NonNull
    @Override
    public String toString() {
        return "{" +
                "deviceId=" + deviceId +
                ", bytes=" + Arrays.toString(bytes) +
                ", glucoseId=" + glucoseId +
                ", name=" + name +
                ", year=" + year +
                ", month=" + month +
                ", day=" + day +
                ", hour=" + hour +
                ", minute=" + minute +
                ", Iw=" + Iw +
                ", Ib=" + Ib +
                ", T=" + T +
                ", BGCount=" + BGCount +
                ", BGICount=" + BGICount +
                ", trend=" + trend +
                ", errorCode=" + errorCode +
                ", showGlucose=" + showGlucose +
                ", showGlucoseMG=" + showGlucoseMG +
                ", glucoseStatus=" + glucoseStatus +
                ", hypoglycemiaEarlyWarnMinutes=" + hypoglycemiaEarlyWarnMinutes +
                ", countdownDays=" + countdownDays +
                ", countdownHours=" + countdownHours +
                ", countdownMinutes=" + countdownMinutes +
                ", timeInMillis=" + timeInMillis +
                '}';
    }

}
