// ignore_for_file: unused_field

import 'package:animated_splash_screen/animated_splash_screen.dart';
import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:gozap_package/components/DB/db.dart';
import 'package:gozap_package/components/common.dart';
import 'package:gozap_package/components/localization/app_translations_delegate.dart';
import 'package:gozap_package/components/localization/application.dart';
import 'package:gozap_package/components/themes/themes.dart';
import 'package:gozap_package/controllers/extentions/trycatch.dart';
import 'package:gozap_package/gozap_package.dart';
import 'package:gozap_package/models/common.dart';
import 'package:gozap_package/models/user/user_model.dart';
import 'package:gozap_package/views/log_in/log_in_view.dart';
import 'package:provider/provider.dart';
import 'package:toast/toast.dart';

import 'views/dashboard/dashboard_view.dart';

void main() {
  runApp(MultiProvider(providers: [
    ChangeNotifierProvider(create: (_) => LocalDB()),
    ChangeNotifierProvider(create: (_) => userChangeNotifier()),
    ChangeNotifierProvider(create: (_) => commonChangeNotifier()),
  ], child: const MyApp()));
}

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

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

class _MyAppState extends State<MyApp> {
  late AppTranslationsDelegate _newLocaleDelegate;
  UserModule? user;
  @override
  void initState() {
    _newLocaleDelegate = const AppTranslationsDelegate(newLocale: null);
    application.onLocaleChanged = onLocaleChange;
    super.initState();
    checkUser.callAndAbsorbError();
  }

  checkUser() async {
    await Provider.of<userChangeNotifier>(context, listen: false).get(context);
    setState(() {
      user = Provider.of<userChangeNotifier>(context, listen: false).user;
    });
    // ignore: use_build_context_synchronously
    Provider.of<commonChangeNotifier>(context,listen: false).set(context, "https://api.gofuel.in/");
  }

  void onLocaleChange(Locale locale) {
    setState(() {
      _newLocaleDelegate = AppTranslationsDelegate(newLocale: locale);
    });
  }

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    ToastContext().init(context);
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'GoZAP Rider',
      theme: ThemeData(
          fontFamily: 'Eras',
          primarySwatch: buildMaterialColor(AppTheme.primaryColor)
      ),
      localizationsDelegates: const [
        AppTranslationsDelegate(),
        //provides localised strings
        GlobalMaterialLocalizations.delegate,
        //provides RTL support
        GlobalWidgetsLocalizations.delegate,
      ],
      supportedLocales: application.supportedLocales(),
      home: AnimatedSplashScreen(
        duration: 3000,
        splashIconSize: 300.0,
        splash: Container(
          child: Common.goFuelLogoWidget(context),
        ),
        nextScreen: user != null
            ? const DashboardView()
            : LogInView(
                nextScreen: const DashboardView(),
              ),
        backgroundColor: AppTheme.secondaryColor,
        splashTransition: SplashTransition.fadeTransition,
      ),
    );
  }
}
