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:provider/provider.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:skillcourt/navigation/route_generator.dart';

import 'package:skillcourt/pages/home_new.dart';

import 'package:skillcourt/pages/play_setup/save_game/db_helper.dart';

import 'package:skillcourt/services/auth.dart';
import 'package:skillcourt/services/database.dart';
import 'package:skillcourt/station.dart';

import 'global_data.dart'; // Ensure this import path is correct.

import 'package:skillcourt/game_session_model.dart';
import 'package:sqflite/sqflite.dart';

final GetIt getIt = GetIt.instance;

void main() {
  WidgetsFlutterBinding.ensureInitialized();
  // Register Service container
  getIt.registerSingletonAsync<DatabaseService>(
          () => $FloorDatabaseService.databaseBuilder('skillcourt.db').build());
  getIt.registerSingletonAsync<SharedPreferences>(
          () => SharedPreferences.getInstance());
  getIt.registerSingletonWithDependencies<Auth>(() => Auth(),
      dependsOn: [SharedPreferences]);
  getIt.registerLazySingleton<Station>(() => Station());
  getIt.registerSingleton<DBHelper>(DBHelper());
  getIt.registerSingleton<Future<Database?>>(getIt<DBHelper>().initDatabase());
  runApp(
    MultiProvider(
      providers: [
        ChangeNotifierProvider(
          create: (context) => GlobalData(),
        ),
        ChangeNotifierProvider(
          create: (context) => GameSessionModel(),
        ),
        // Add the DatabaseService provider here
        Provider<DatabaseService>(
          create: (_) => getIt<DatabaseService>(),
        ),
      ],
      child: const SkillcourtApp(),
    ),
  );
}

class SkillcourtApp extends StatelessWidget {
  static const String _title = 'skillcourt';

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

  @override
  Widget build(BuildContext context) {
    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) => const PlatformApp(
                      localizationsDelegates: <LocalizationsDelegate<
                          dynamic>>[
                        DefaultMaterialLocalizations.delegate,
                        DefaultWidgetsLocalizations.delegate,
                        DefaultCupertinoLocalizations.delegate
                      ],
                      title: _title,
                      initialRoute: HomePageNew.routeName,
                      onGenerateRoute: RouteGenerator.generateRoute,
                    )));
          } else {
            return PlatformCircularProgressIndicator();
          }
        });
  }
}
