import 'dart:convert';
import 'dart:io';

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';
import 'package:zhiying_base_widget/utils/mob_push_util.dart';
import 'package:zhiying_business_college/register.dart';
import 'package:zhiying_new_user_free/register.dart';
import 'package:zhiying_wechat_teacher/register.dart';
import 'package:zhiying_moments/register.dart';
import 'package:zhiying_member_upgrade/register.dart';
import 'package:zhiying_equity_card/zhiying_equity_card.dart';
import 'package:zhiying_official/register.dart';
import 'package:zhiying_base_widget/zhiying_base_widget.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) {
    var erroStr = '';
    const bool inProduction = const bool.fromEnvironment("dart.vm.product");
    if (!inProduction) {
      erroStr = error.exceptionAsString();
    }
    return Center(
      child: Text(
        "" + erroStr,
        style: Theme.of(context).textTheme.title.copyWith(color: Colors.grey[200]),
      ),
    );
  }

  @override
  void initState() {
    super.initState();
    print('初始化~~~~');
    BaseWidgetRegister.init();
    BusinessCollageRegister.init();
    NewUserFreeRegister.init();
    WeChatTeachRegister.init();
    FriendCircleRegister.init();
    MemberUpgradeRegister.init();
    EquityCardRegister.init();
    OfficialRegister.init();
    print('初始化百川');
    FlutterAlibc.initAlibc(version: "", appName: "").then((result) {
      print("白川" + '${result.errorCode}  ${result.errorMessage}');
    });
    MobPushUtil.setCanPush();

    EventUtil.instance.on().listen((event) {});

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

  @override
  Widget build(BuildContext context) {
    ErrorWidget.builder = (FlutterErrorDetails errorDetails) {
      return getErrorWidget(context, errorDetails);
    };
    return MultiProvider(
      providers: [
        ChangeNotifierProvider.value(value: UserInfoNotifier()),
      ],
      child: GestureDetector(
        onTap: () {
          FocusScopeNode currentFocus = FocusScope.of(context);
          if (!currentFocus.hasPrimaryFocus && currentFocus.focusedChild != null) {
            FocusManager.instance.primaryFocus.unfocus();
          }
        },
        child: MaterialApp(
          builder: (context, child) {
            return MediaQuery(
              //设置文字大小不随系统设置改变
              data: MediaQuery.of(context).copyWith(textScaleFactor: 1.0),
              child: child,
            );
          },
          theme: ThemeData(
            fontFamily: 'PingFang',
            primaryColor: HexColor.fromHex('#FF4242'),
            accentColor: HexColor.fromHex('#FF4242'),
          ),
          routes: <String, WidgetBuilder>{
            '/homePage': (BuildContext context) => HomeCenterPage(),
          },
          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: LaunchPage(
              // builder: (context) => HomePage(),
              ),
        ),
      ),
    );
  }
}

// FocusScopeNode currentFocus = FocusScope.of(context);
// if (!currentFocus.hasPrimaryFocus && currentFocus.focusedChild != null) {
// FocusManager.instance.primaryFocus.unfocus();
// }
