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/category/categories.dart';
import 'package:diameter/screens/category/accuracy_detail.dart';
import 'package:diameter/screens/category/accuracy_list.dart';
import 'package:diameter/screens/category/event_type_detail.dart';
import 'package:diameter/screens/category/event_type_list.dart';
import 'package:diameter/screens/category/meal_category_detail.dart';
import 'package:diameter/screens/category/meal_category_list.dart';
import 'package:diameter/screens/category/meal_portion_type_detail.dart';
import 'package:diameter/screens/category/meal_portion_type_list.dart';
import 'package:diameter/screens/category/meal_source_detail.dart';
import 'package:diameter/screens/category/meal_source_list.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_overview.dart';
import 'package:diameter/screens/reports/daily_chart.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/meal/meal_detail.dart';
import 'package:diameter/screens/meal/meal_list.dart';
import 'package:diameter/screens/reports/export.dart';
import 'package:diameter/screens/reports/reports.dart';
import 'package:diameter/settings.dart';
import 'package:diameter/data_export.dart';
import 'package:flutter/material.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';
import 'package:flutter_translate/flutter_translate.dart';

late ObjectBox objectBox;

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();

  DataExport.exportToGoogleDrive();

  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);

  DataExport.exportToGoogleDrive();

  var delegate = await LocalizationDelegate.create(
    fallbackLocale: 'en_US',
    supportedLocales: ['en_US', 'de'],
  );

  runApp(
    LocalizedApp(delegate, const App()),
  );
}

class App extends StatelessWidget {
  const App({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    var localizationDelegate = LocalizedApp.of(context).delegate;

    return LocalizationProvider(
      state: LocalizationProvider.of(context).state,
      child: GestureDetector(
        onTap: () => FocusManager.instance.primaryFocus?.unfocus(),
        child: MaterialApp(
          localizationsDelegates: [
            localizationDelegate,
          ],
          supportedLocales: const [
            Locale('en', 'US'),
            Locale('de', 'DE'),
          ],
          // localizationDelegate.supportedLocales,
          locale: localizationDelegate.currentLocale,
          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.eventTypes: (context) => const EventTypeListScreen(),
            Routes.eventType: (context) => const EventTypeDetailScreen(),
            Routes.logEvents: (context) => const LogEventListScreen(),
            Routes.reports: (context) => const ReportsOverviewScreen(),
            Routes.export: (context) => const ExportDialog(),
            Routes.dailyChart: (context) => const DailyChart(),
            Routes.meals: (context) => const MealListScreen(),
            Routes.meal: (context) => const MealDetailScreen(),
            Routes.category: (context) => const CategoryOverviewScreen(),
            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.accuracies: (context) => const AccuracyListScreen(),
            Routes.accuracy: (context) => const AccuracyDetailScreen(),
            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(),
          },
        ),
      ),
    );
  }
}
