import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_platform_widgets/flutter_platform_widgets.dart';
import 'package:get_it/get_it.dart';
import 'package:shared_preferences/shared_preferences.dart';

import './pages/club_list.dart';
import './pages/club_registration.dart';
import './pages/configuration.dart';
import './pages/group_list.dart';
import './pages/group_registration.dart';
import './pages/home.dart';
import './pages/login_screen.dart';
import './pages/pad_config.dart';
import './pages/player_list.dart';
import './pages/player_registration.dart';
import './pages/station_creation_form.dart';
import './pages/statistics.dart';
import './services/auth.dart';
import './services/database.dart';

final GetIt getIt = GetIt.instance;

void main() {
  WidgetsFlutterBinding.ensureInitialized();

  // Register Service container
  getIt.registerSingletonAsync<DatabaseService>(
      () => $FloorDatabaseService.databaseBuilder('agineek.db').build());
  getIt.registerSingletonAsync<SharedPreferences>(
      () => SharedPreferences.getInstance());
  getIt.registerSingletonWithDependencies<Auth>(() => Auth(),
      dependsOn: [SharedPreferences]);

  runApp(const AgineekApp());
}

class AgineekApp extends StatelessWidget {
  static const String _title = 'Agineek';

  const AgineekApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final appRoutes = <String, WidgetBuilder>{
      LoginScreen.routeName: (context) => const LoginScreen(),
      ClubRegistration.routeName: (context) => const ClubRegistration(),
      PlayerRegistration.routeName: (context) => const PlayerRegistration(),
      GroupRegistration.routeName: (context) => const GroupRegistration(),
      ConfigPage.routeName: (context) => const ConfigPage(),
      StationCreationForm.routeName: (context) => const StationCreationForm(),
      PadConfigPage.routeName: (context) => const PadConfigPage(),
      PlayerList.routeName: (context) => const PlayerList(),
      GroupList.routeName: (context) => const GroupList(),
      ClubList.routeName: (context) => const ClubList(),
      Statistics.routeName: (context) => const Statistics(),
    };

    final themeData = ThemeData(primarySwatch: Colors.green);

    return FutureBuilder(
        future: getIt.allReady(),
        // ignore: body_might_complete_normally
        builder: (BuildContext context, AsyncSnapshot snapshot) {
          if (snapshot.hasData) {
            return Theme(
                data: themeData,
                child: PlatformProvider(
                    settings:
                        PlatformSettingsData(iosUsesMaterialWidgets: false),
                    builder: (context) => PlatformApp(
                            localizationsDelegates: const <
                                LocalizationsDelegate<dynamic>>[
                              DefaultMaterialLocalizations.delegate,
                              DefaultWidgetsLocalizations.delegate,
                              DefaultCupertinoLocalizations.delegate
                            ],
                            title: _title,
                            home: const HomePage(),
                            routes: appRoutes)));
          } else {
            return PlatformCircularProgressIndicator();
          }
        });
  }
}
