import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_alibc/flutter_alibc.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:provider/provider.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:zhiying_base_widget/pages/home_page/home_page.dart';
import 'package:zhiying_base_widget/pages/launch_page/launch_page.dart';
import 'package:zhiying_base_widget/register.dart';
import 'package:zhiying_comm/util/update/app_update_util.dart';
import 'package:zhiying_comm/zhiying_comm.dart';

import 'util/localizations_delegate.dart';

void main() {
  FlutterError.onError = (FlutterErrorDetails details) {
    FlutterError.dumpErrorToConsole(details);
    print(details.exceptionAsString());
//    if (kReleaseMode) exit(1);
  };
  runApp(MyApp());
}

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  Widget getErrorWidget(BuildContext context, FlutterErrorDetails error) {
    return Center(
      child: Text(
        "走神了~\n${error.exceptionAsString()}",
        style:
            Theme.of(context).textTheme.title.copyWith(color: Colors.redAccent),
      ),
    );
  }

  @override
  void initState() {
    super.initState();
    print('初始化~~~~');
    BaseWidgetRegister.init();
    print('初始化百川');
    FlutterAlibc.initAlibc(version: "", appName: "").then((result) {
      print("白川" + '${result.errorCode}  ${result.errorMessage}');
    });

    // app更新插件
    AppUpdateUtil.initXUpdate();
  }

  @override
  Widget build(BuildContext context) {
    ErrorWidget.builder = (FlutterErrorDetails errorDetails) {
      return getErrorWidget(context, errorDetails);
    };
    return MultiProvider(
      providers: [
        ChangeNotifierProvider.value(value: UserInfoNotifier()),
      ],
      child: MaterialApp(
        theme: ThemeData(
          primaryColor: HexColor.fromHex('#FF4242'),
          accentColor: HexColor.fromHex('#FF4242'),
        ),
        routes: <String, WidgetBuilder>{
          '/homePage': (BuildContext context) => HomePage() ,
        },
        navigatorKey: navigatorKey,
        localizationsDelegates: [
          RefreshLocalizations.delegate,
          GlobalMaterialLocalizations.delegate,
          GlobalWidgetsLocalizations.delegate,
          CommonLocalizationsDelegate(),
        ],
        supportedLocales: [
          const Locale('en'),
          const Locale('zh'),
        ],
        localeResolutionCallback:
            (Locale locale, Iterable<Locale> supportedLocales) {
          print("change language ${locale.toString()}");
          return locale;
        },
        home: GestureDetector(
          onTap: () {
            FocusScopeNode currentFocus = FocusScope.of(context);
            if (!currentFocus.hasPrimaryFocus &&
                currentFocus.focusedChild != null) {
              FocusManager.instance.primaryFocus.unfocus();
            }
          },
          child: LaunchPage(
            // builder: (context) => HomePage(),
          ),
        ),
      ),
    );
  }
}