import 'dart:ui';

import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_crashlytics/firebase_crashlytics.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:nut_player_example/src/common/repository/settings_repository.dart';
import 'src/features/main_screen/presentation/home.dart';
import 'package:screen_brightness/screen_brightness.dart';

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp();
  final double systemBrightness = await ScreenBrightness().system;

  FlutterError.onError = (errorDetails) {
    FirebaseCrashlytics.instance.recordFlutterFatalError(errorDetails);
  };
  // Pass all uncaught asynchronous errors that aren't handled by the Flutter framework to Crashlytics
  PlatformDispatcher.instance.onError = (error, stack) {
    FirebaseCrashlytics.instance.recordError(error, stack, fatal: true);
    return true;
  };

  runApp(MyApp(brightness: systemBrightness));
}

class MyApp extends StatelessWidget {
  final double brightness;

  const MyApp({required this.brightness, super.key});

  @override
  Widget build(BuildContext context) {
    return RepositoryProvider(
        create: (context) => SettingsRepository.createDefaults(brightness),
        child: const CupertinoApp( //Для теста убрать const
          theme: CupertinoThemeData(
              brightness: Brightness.light,
              primaryColor: CupertinoColors.link
          ),
          home: Home()//Bместо Home() подставить PlayerView()
        )
    );
  }
}