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/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';

void main() async {
  await Hive.initFlutter();
  await setting.userLocalDb.openBox();
  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() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  late AuthState state;
  String language = setting.userLocalDb.getUser().language;
  @override
  void initState() {
    Future.delayed(Duration.zero, () async {
      state = Provider.of(context, listen: false);
      setState(() {
        language = state.language;
      });
    });
    super.initState();
  }

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return Consumer<AuthState>(
      builder: (context, value, child) {
        return 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, // Add this line
            GlobalMaterialLocalizations.delegate,
            GlobalWidgetsLocalizations.delegate,
            GlobalCupertinoLocalizations.delegate,
          ],
          locale: Locale(value.language),
          supportedLocales: const [
            Locale('en'), // English
            Locale('fa'), // Persian
          ],
        );
      },
    );
  }
}
