import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:bloc/bloc.dart';
import 'package:provider/provider.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
// import 'package:firebase_crashlytics/firebase_crashlytics.dart';
import 'package:flutter_localizations/flutter_localizations.dart';

import 'package:{{ name }}/repository/TokenRepository.dart';
import 'package:{{ name }}/repository/AuthenticationRepository.dart';

import 'package:{{ name }}/bloc/blocs.dart';
import 'package:{{ name }}/router/Router.dart';

import 'package:i18n_translate/i18n.dart';
import 'package:i18n_translate/AppLanguage.dart';

import 'views/Home.dart';
import 'views/Auth/AuthWalktrought.dart';

class SimpleBlocDelegate extends BlocDelegate {
  @override
  void onEvent(Bloc bloc, Object event) {
    super.onEvent(bloc, event);
    print(event);
  }

  @override
  void onTransition(Bloc bloc, Transition transition) {
    super.onTransition(bloc, transition);
    print(transition);
  }

  @override
  void onError(Bloc bloc, Object error, StackTrace stacktrace) {
    super.onError(bloc, error, stacktrace);
    print(error);
  }
}

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  AppLanguage appLanguage = AppLanguage();
  BlocSupervisor.delegate = SimpleBlocDelegate();
  final tokenRepository = TokenRepository();
  final authenticationRepository = AuthenticationRepository();
  // Crashlytics.instance.enableInDevMode = true;
  // Pass all uncaught errors from the framework to Crashlytics.
  // FlutterError.onError = Crashlytics.instance.recordFlutterError;
  await appLanguage.fetchLocale();
  runApp(
    BlocProvider<AuthenticationBloc>(
      create: (context) {
        return AuthenticationBloc(tokenRepository: tokenRepository, authenticationRepository: authenticationRepository)
          ..add(AppLoaded());
      },
      child: MyApp(appLanguage: appLanguage, authenticationRepository: authenticationRepository),
    ),
  );
}

class MyApp extends StatelessWidget {
  final AppLanguage appLanguage;
  final AuthenticationRepository authenticationRepository;

  MyApp({Key key,this.appLanguage, @required this.authenticationRepository}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider<AppLanguage>(
        create: (_) => appLanguage,
        child: StreamBuilder<Object>(
          stream: null,
          builder: (context, snapshot) {
            return Consumer<AppLanguage>(
              builder: (context, model, child) {
              return MaterialApp(
                debugShowCheckedModeBanner: false,
                theme: ThemeData(
                  primaryColor: Color(0xff0251ff),
                ),
                locale: model.appLocal,
                supportedLocales: [
                  Locale('es', 'MX'),
                  Locale('en', 'US'),
                ],
                localizationsDelegates: [
                  I18n.delegate,
                  GlobalMaterialLocalizations.delegate,
                  GlobalWidgetsLocalizations.delegate,
                ],
                onGenerateRoute: (settings) => Router.generateRoute(settings, authenticationRepository),
                home: AnnotatedRegion<SystemUiOverlayStyle>(
                  value: SystemUiOverlayStyle(
                    statusBarColor: Colors.transparent, // transparent status bar
                    // systemNavigationBarColor: Colors.black, // navigation bar color
                    statusBarIconBrightness: Brightness.dark, // status bar icons' color
                    // systemNavigationBarIconBrightness: Brightness.dark, //navigation bar icons' color
                  ),
                  child: BlocBuilder<AuthenticationBloc, AuthenticationState>(
                    builder: (context, state) {
                      if (state is AuthenticationUninitialized) {
                       return Container();
                      }
                      if (state is AuthenticationLoading) {
                       return Container();
                      }
                      if (state is AuthenticationAuthenticated) {
                        return HomePage(currentUser: state.user);
                      }
                      return AuthWalktrought();
                    },
                  ),
                ),
                );
              }
            );
          }
        ),
    );
  }
}
