import 'package:diameter/main.dart';
import 'package:diameter/models/settings.dart';
import 'package:flutter/material.dart';
import 'package:objectbox/objectbox.dart';
import 'package:diameter/objectbox.g.dart' show GlucoseTarget_;

@Entity(uid: 5041265995704044399)
@Sync()
class GlucoseTarget {
  static final Box<GlucoseTarget> box = objectBox.store.box<GlucoseTarget>();

  // properties
  int id;
  bool deleted;
  int fromMgPerDL;
  int toMgPerDl;
  double fromMmolPerL;
  double toMmolPerL;
  int color;

  // constructor
  GlucoseTarget({
    this.id = 0,
    this.deleted = false,
    required this.fromMgPerDL,
    required this.toMgPerDl,
    required this.fromMmolPerL,
    required this.toMmolPerL,
    required this.color,
  });

  // methods
  static GlucoseTarget? get(int id) => box.get(id);

  // methods
  static List<GlucoseTarget> getAll() {
    if (box.getAll().isEmpty) {
      reset();
    }
    return box.getAll();
  }

  static Color getColorForGlucose({int mgPerDl = 0, double mmolPerL = 0}) {
    if (box.getAll().isEmpty) {
      reset();
    }
    
    Condition<GlucoseTarget> condition;
    if (mgPerDl > 0 &&
        (mmolPerL == 0 ||
            Settings.glucoseMeasurement == GlucoseMeasurement.mgPerDl)) {
      condition = GlucoseTarget_.fromMgPerDL.lessOrEqual(mgPerDl).and(GlucoseTarget_.toMgPerDl.greaterOrEqual(mgPerDl));
    } else if (mmolPerL > 0 &&
        (mgPerDl == 0 ||
            Settings.glucoseMeasurement == GlucoseMeasurement.mmolPerL)) {
            condition = GlucoseTarget_.fromMmolPerL.lessOrEqual(mmolPerL).and(GlucoseTarget_.toMmolPerL.greaterOrEqual(mmolPerL));
    } else {
      return Colors.black;
    }

    List<GlucoseTarget> result = box
        .query(GlucoseTarget_.deleted.equals(false) & condition)
        .build()
        .find();

    if (result.length != 1) {
      return Colors.black;
    }
    return Color(result.single.color);
  }

  static void put(GlucoseTarget glucoseTarget) => box.put(glucoseTarget);
  static void remove(int id) {
    final item = box.get(id);
    if (item != null) {
      item.deleted = true;
      box.put(item);
    }
  }

  static void reset() {
    box.removeAll();
    List<GlucoseTarget> defaultTargets = [
      GlucoseTarget(
        fromMgPerDL: 0,
        toMgPerDl: 69,
        fromMmolPerL: 0,
        toMmolPerL: 3.83,
        color: Colors.red.value,
      ),
      GlucoseTarget(
        fromMgPerDL: 70,
        toMgPerDl: 99,
        fromMmolPerL: 3.84,
        toMmolPerL: 5.48,
        color: Colors.orange.value,
      ),
      GlucoseTarget(
        fromMgPerDL: 100,
        toMgPerDl: 140,
        fromMmolPerL: 5.49,
        toMmolPerL: 7.77,
        color: Colors.green.value,
      ),
      GlucoseTarget(
        fromMgPerDL: 141,
        toMgPerDl: 240,
        fromMmolPerL: 7.78,
        toMmolPerL: 13.32,
        color: Colors.orange.value,
      ),
      GlucoseTarget(
        fromMgPerDL: 241,
        toMgPerDl: 999,
        fromMmolPerL: 13.33,
        toMmolPerL: 55.44,
        color: Colors.deepOrange.value,
      ),
    ];
    box.putMany(defaultTargets);
  }
}
