
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:provider/provider.dart';
import 'package:bot_toast/bot_toast.dart';

import 'package:twong/pages/tabs.dart';
import 'package:twong/utils/index.dart';
import 'package:twong/utils/sentry.dart';
import 'package:twong/router/index.dart';
import 'package:twong/config/style.dart';
import 'package:twong/config/theme.dart';
import 'package:twong/providers/index.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  // await SentryUtils.initPlatformState();
  // runZoned((){
  //   ErrorWidget.builder = (FlutterErrorDetails details){
  //     Zone.current.handleUncaughtError(details.exception, details.stack);
  //     ///出现异常时会进入下方页面（flutter原有的红屏），
  //     // return ExceptionPageState(details.exception.toString(),details.stack.toString()).generateWidget();
  //     return Container();
  //   };
  // }, onError: (Object error,StackTrace trace){
  //   ///你可以将下面日志上传到服务器，用于release后的错误处理
  //   Log.e("$error ${trace.toString()}");
  //   SentryUtils.reportError(error, trace);
  // });
  try {
    Log.init();
    initRouterList();
    Cache.initial().then((e) => runApp(MainApp()));
    SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle.light);
  } catch (error, trace) {
    Log.e("$error ${trace.toString()}");
    SentryUtils.reportError(error, trace);
  }
}

class MainApp extends StatelessWidget {
  final GlobalKey<NavigatorState> navigatorKey = new GlobalKey<
      NavigatorState>();
  final SystemUiOverlayStyle _style = SystemUiOverlayStyle(
      statusBarColor: Colors.transparent);

  @override
  Widget build(BuildContext context) {
    Cache.setKey(navigatorKey);
    SystemChrome.setSystemUIOverlayStyle(_style);

    return MultiProvider(
        providers: providers,
        child: MaterialApp(
            debugShowCheckedModeBanner: false,
            home: Tabs(),
            theme: ThemeData(
                fontFamily: "HuaWen",
                cursorColor: Colors.white,
                backgroundColor: DColors.Main,
                splashFactory: const NoSplashFactory()
            ),
            builder: BotToastInit(),
            navigatorKey: navigatorKey,
            onGenerateRoute: onGenerateRoute,
            navigatorObservers: [BotToastNavigatorObserver()]
        ));
  }
}
