import 'package:diameter/main.dart';
import 'package:flutter/material.dart';
import 'package:objectbox/objectbox.dart';

enum GlucoseDisplayMode { activeOnly, bothForList, bothForDetail, both }
List<String> glucoseDisplayModeLabels = [
  'activeOnly',
  'bothForList',
  'bothForDetail',
  'both',
];

enum GlucoseMeasurement {
  mgPerDl,
  mmolPerL,
}
List<String> glucoseMeasurementSuffixes = [
  'mg/dl',
  'mmol/l',
];
List<String> glucoseMeasurementLabels = [
  'mgPerDl',
  'mmolPerL',
];

enum NutritionMeasurement {
  grams,
  ounces,
  lbs,
}
List<String> nutritionMeasurementSuffixes = [
  'g',
  'oz',
  'lbs',
];
List<String> nutritionMeasurementLabels = [
  'grams',
  'ounces',
  'lbs',
];

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

  // properties
  int id;

  int nutritionMeasurementIndex;
  int glucoseDisplayModeIndex;
  int glucoseMeasurementIndex;
  int targetGlucoseMgPerDl;
  double targetGlucoseMmolPerL;

  double insulinIncrements;
  double nutritionIncrements;
  double mmolPerLIncrements;
  double amountIncrements;

  String dateFormat;
  String? longDateFormat;
  String timeFormat;
  String? longTimeFormat;

  bool showConfirmationDialogOnCancel;
  bool showConfirmationDialogOnDelete;
  bool showConfirmationDialogOnStopEvent;

  bool useDarkTheme;

  // constructor
  Settings({
    this.id = 0,
    this.nutritionMeasurementIndex = 0,
    this.glucoseDisplayModeIndex = 0,
    this.glucoseMeasurementIndex = 0,
    this.insulinIncrements = 0.05,
    this.nutritionIncrements = 0.01,
    this.mmolPerLIncrements = 0.1,
    this.amountIncrements = 0.05,
    this.dateFormat = 'MM/dd/yy',
    this.longDateFormat = 'MMMM dd, yyyy',
    this.timeFormat = 'HH:mm',
    this.longTimeFormat = 'HH:mm:ss',
    this.showConfirmationDialogOnCancel = true,
    this.showConfirmationDialogOnDelete = true,
    this.showConfirmationDialogOnStopEvent = true,
    this.targetGlucoseMgPerDl = 100,
    this.targetGlucoseMmolPerL = 5.5,
    this.useDarkTheme = false,
  });

  // methods
  static Settings get() {
    if (box.getAll().length != 1) {
      reset();
    }
    return box.getAll().single;
  }

  static NutritionMeasurement get nutritionMeasurement =>
      NutritionMeasurement.values[get().nutritionMeasurementIndex];
  static GlucoseMeasurement get glucoseMeasurement =>
      GlucoseMeasurement.values[get().glucoseMeasurementIndex];
  static GlucoseDisplayMode get glucoseDisplayMode =>
      GlucoseDisplayMode.values[get().glucoseDisplayModeIndex];

  static String get nutritionMeasurementSuffix =>
      nutritionMeasurementSuffixes[get().nutritionMeasurementIndex];
  static String get glucoseMeasurementSuffix =>
      glucoseMeasurementSuffixes[get().glucoseMeasurementIndex];

  static int get targetMgPerDl => get().targetGlucoseMgPerDl;
  static double get targetMmolPerL => get().targetGlucoseMmolPerL;

  static double get insulinSteps => get().insulinIncrements;
  static double get nutritionSteps => get().nutritionIncrements;
  static double get mmolPerLSteps => get().mmolPerLIncrements;

  static ThemeMode get themeMode =>
      get().useDarkTheme ? ThemeMode.dark : ThemeMode.light;

  static void put(Settings settings) => box.put(settings);

  static void reset() {
    box.removeAll();
    box.put(Settings(useDarkTheme: ThemeMode.system == ThemeMode.dark));
  }
}
