import 'package:flutter/material.dart';
import 'package:responsive_framework/responsive_framework.dart';
import 'package:states_rebuilder/states_rebuilder.dart';
import 'package:demo_package_ui_semantic/data_source/hive_storage.dart';
import 'route_generator.dart';
import 'ui/pages/pages.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await RM.storageInitializer(HiveStorage());
  FlutterError.onError = (FlutterErrorDetails details) {
    print("Error :  ${details.exception}");
    print("StackTrace :  ${details.stack}");
  };
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return TopAppWidget(
      ensureInitialization: () => [
        RM.storageInitializer(HiveStorage()),
      ],
      onWaiting: () => MaterialApp(
        theme: ThemeData.dark(),
        home: Scaffold(
          body: Center(child: CircularProgressIndicator())
        ),
      ),
      onError: (error, refresh) {
        return MaterialApp(
          theme: ThemeData.dark(),
          home: Scaffold(
            body: Text('$error'),
          ),
        );
      },
      injectedAuth: user,
      builder: (context) {
        return MaterialApp(
          builder: (context, widget) => ResponsiveWrapper.builder(
            ClampingScrollWrapper.builder(context, widget!),
            maxWidth: 1000,
            minWidth: 450,
            defaultScale: true,
            defaultName: MOBILE,
            breakpoints: [
              ResponsiveBreakpoint.resize(450, name: MOBILE),
              ResponsiveBreakpoint.autoScale(800, name: TABLET),
              ResponsiveBreakpoint.autoScale(1000, name: TABLET)
            ],
          ),
          title: 'Demo Package UI Semantic',
          theme: ThemeData(
            primarySwatch: Colors.blue,
          ),
          home: On.auth(
            onInitialWaiting: () => Center(child: CircularProgressIndicator()),
            onUnsigned: () => LoginPage(),
            onSigned: () => MainPage(),
          ).listenTo(
            user,
            useRouteNavigation: true
          ),
          navigatorKey: RM.navigate.navigatorKey,
          onGenerateRoute: RM.navigate.onGenerateRoute(
            {
              MainPage.routeName: (_) => MainPage(),
              LoginPage.routeName: (_) => LoginPage(),
              ButtonsPage.routeName: (_) => ButtonsPage(),
              StandardsButtonPage.routeName: (_) => StandardsButtonPage(),
              LabelPage.routeName: (_) => LabelPage(),
              TilesPage.routeName: (_) => TilesPage(),
              RoundedButtonPage.routeName: (_) => RoundedButtonPage(),
              SquareButtonPage.routeName: (_) => SquareButtonPage(),
              ButtonsPageDynamic.routeName: (_) => ButtonsPageDynamic(),
              AccordionPage.routeName: (_) => AccordionPage(),
              AlertPage.routeName: (_) => AlertPage(),
              AppbarHomePage.routeName: (_) => AppbarHomePage(),
              AvatarPage.routeName: (_) => AvatarPage(),
              BadgesPage.routeName: (_) => BadgesPage(),
              IconsButtonsPage.routeName: (_) => IconsButtonsPage(),
              CardPage.routeName: (_) => CardPage(),
              TabsPage.routeName: (_) => TabsPage(),
              SegmentedTabPage.routeName: (_) => SegmentedTabPage(),
              IconTabPage.routeName: (_) => IconTabPage(),
              LabeledTabPage.routeName: (_) => LabeledTabPage(),
              BottomIconTabPage.routeName: (_) => BottomIconTabPage(),
              BottomLabeledTabPage.routeName: (_) => BottomLabeledTabPage(),
              CarouselPage.routeName: (_) => CarouselPage(),
              ToastPage.routeName: (_) => ToastPage(),
              TogglePage.routeName: (_) => TogglePage()
            },
          ),
          debugShowCheckedModeBanner: false,
        );
      }
    );
  }
}