import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:hive_flutter/hive_flutter.dart';
import 'package:provider/provider.dart';
import 'package:qadirneyriz/config/config.dart';
import 'package:qadirneyriz/firebase_options.dart';
import 'package:qadirneyriz/global/global_state/global_state.dart';
import 'package:qadirneyriz/router/router.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:qadirneyriz/screens/auth/state/state.dart';
import 'package:qadirneyriz/screens/home/state.dart';
import 'package:qadirneyriz/screens/meeting/state.dart';
import 'package:qadirneyriz/screens/private_meeting/state.dart';
import 'package:qadirneyriz/screens/report/state.dart';
import 'package:qadirneyriz/setting/setting.dart';

final FirebaseMessaging messaging = FirebaseMessaging.instance;

Future<void> initializeApp() async {
  await Hive.initFlutter();
  await Firebase.initializeApp(
    options: DefaultFirebaseOptions.currentPlatform,
  );
  await setting.userLocalDb.openBox();
}

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await initializeApp(); // Initialize Firebase and other services
  runApp(
    MultiProvider(
      providers: [
        ChangeNotifierProvider(create: (_) => GlobalState()),
        ChangeNotifierProvider(create: (_) => AuthState()),
        ChangeNotifierProvider(create: (_) => HomeState()),
        ChangeNotifierProvider(create: (_) => MeetingsState()),
        ChangeNotifierProvider(create: (_) => PrivateMeetingsState()),
        ChangeNotifierProvider(create: (_) => ReportState()),
      ],
      child: const MyApp(),
    ),
  );
}

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => FiltersItemInPrivateMeetingsReport();
}

class FiltersItemInPrivateMeetingsReport extends State<MyApp> {
  late AuthState state;
  String language = setting.userLocalDb.getUser().language;

  @override
  void initState() {
    super.initState();
    Future.delayed(Duration.zero, () async {
      state = Provider.of<AuthState>(context, listen: false);
      setState(() {
        language = state.language;
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    return Consumer<AuthState>(
      builder: (context, value, child) {
        return GestureDetector(
          behavior: HitTestBehavior.opaque,
          onTap: () {
            FocusScopeNode currentFocus = FocusScope.of(context);

            if (!currentFocus.hasPrimaryFocus &&
                currentFocus.focusedChild != null) {
              FocusManager.instance.primaryFocus?.unfocus();
            }
          },
          child: MaterialApp.router(
            theme: ThemeData(
              colorScheme: ColorScheme.light(primary: config.ui.mainGreen),
              buttonTheme: ButtonThemeData(
                colorScheme: ColorScheme.light(primary: Colors.green),
              ),
              useMaterial3: true,
              fontFamily: 'Font',
              scaffoldBackgroundColor: Colors.white,
            ),
            debugShowCheckedModeBanner: false,
            routerDelegate: router.routerDelegate,
            routeInformationParser: router.routeInformationParser,
            routeInformationProvider: router.routeInformationProvider,
            localizationsDelegates: const [
              AppLocalizations.delegate,
              GlobalMaterialLocalizations.delegate,
              GlobalWidgetsLocalizations.delegate,
              GlobalCupertinoLocalizations.delegate,
            ],
            locale: Locale(value.language),
            supportedLocales: const [
              Locale('en'),
              Locale('fa'),
            ],
          ),
        );
      },
    );
  }
}


