import 'package:diameter/components/app_theme.dart';
import 'package:diameter/config.dart';
import 'package:diameter/models/settings.dart';
import 'package:diameter/object_box.dart';
import 'package:diameter/screens/accuracy_detail.dart';
import 'package:diameter/screens/basal/basal_profile_detail.dart';
import 'package:diameter/screens/bolus/bolus_profile_detail.dart';
import 'package:diameter/screens/log/log.dart';
import 'package:diameter/screens/log/log_entry/log_entry.dart';
import 'package:diameter/screens/log/log_event/log_event_detail.dart';
import 'package:diameter/screens/log/log_event/log_event_list.dart';
import 'package:diameter/screens/log/log_event/log_event_type_detail.dart';
import 'package:diameter/screens/log/log_event/log_event_type_list.dart';
import 'package:diameter/screens/meal/meal_category_detail.dart';
import 'package:diameter/screens/meal/meal_category_list.dart';
import 'package:diameter/screens/meal/meal_detail.dart';
import 'package:diameter/screens/meal/meal_list.dart';
import 'package:diameter/screens/meal/meal_portion_type_detail.dart';
import 'package:diameter/screens/meal/meal_portion_type_list.dart';
import 'package:diameter/screens/meal/meal_source_detail.dart';
import 'package:diameter/screens/meal/meal_source_list.dart';
import 'package:diameter/screens/recipe/recipe_detail.dart';
import 'package:diameter/screens/recipe/recipe_list.dart';
import 'package:diameter/settings.dart';
import 'package:flutter/material.dart';
import 'package:diameter/screens/accuracy_list.dart';
import 'package:diameter/screens/basal/basal_profile_list.dart';
import 'package:diameter/screens/bolus/bolus_profile_list.dart';
import 'package:diameter/navigation.dart';
import 'package:objectbox/objectbox.dart';

late ObjectBox objectBox;
Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  objectBox = await ObjectBox.create();
  
  Sync.isAvailable();
  SyncClient syncClient = Sync.client(
    objectBox.store,
    'ws://192.168.1.184:9999',
    SyncCredentials.sharedSecretString(secret)
  );
  syncClient.start();
  syncClient.requestUpdates(subscribeForFuturePushes: false);

  runApp(
    GestureDetector(
      onTap: () => FocusManager.instance.primaryFocus?.unfocus(),
      child: MaterialApp(
        theme: AppTheme.makeTheme(AppTheme.lightTheme),
        darkTheme: AppTheme.makeTheme(AppTheme.darkTheme),
        themeMode: Settings.themeMode,
        initialRoute: '/',
        routes: {
          '/': (context) => const LogScreen(),
          Routes.log: (context) => const LogScreen(),
          Routes.logEntry: (context) => const LogEntryScreen(),
          Routes.logEvent: (context) => const LogEventDetailScreen(),
          Routes.logEventTypes: (context) => const LogEventTypeListScreen(),
          Routes.logEventType: (context) => const EventTypeDetailScreen(),
          Routes.events: (context) => const LogEventListScreen(),
          Routes.accuracies: (context) => const AccuracyListScreen(),
          Routes.accuracy: (context) => const AccuracyDetailScreen(),
          Routes.meals: (context) => const MealListScreen(),
          Routes.meal: (context) => const MealDetailScreen(),
          Routes.recipes: (context) => const RecipeListScreen(),
          Routes.recipe: (context) => const RecipeDetailScreen(),
          Routes.mealCategories: (context) => const MealCategoryListScreen(),
          Routes.mealCategory: (context) => const MealCategoryDetailScreen(),
          Routes.mealPortionTypes: (context) =>
              const MealPortionTypeListScreen(),
          Routes.mealPortionType: (context) =>
              const MealPortionTypeDetailScreen(),
          Routes.mealSources: (context) => const MealSourceListScreen(),
          Routes.mealSource: (context) => const MealSourceDetailScreen(),
          Routes.bolusProfiles: (context) => const BolusProfileListScreen(),
          Routes.bolusProfile: (context) => const BolusProfileDetailScreen(),
          Routes.basalProfiles: (context) => const BasalProfileListScreen(),
          Routes.basalProfile: (context) => const BasalProfileDetailScreen(),
          Routes.settings: (context) => const SettingsScreen(),
        },
      ),
    ),
  );
}
