import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_wallet/generated/l10n.dart';
import 'package:flutter_wallet/tools/YSColors.dart';
import 'package:flutter_wallet/tools/YSSqflite.dart';
import 'package:flutter_wallet/tools/YSTools.dart';
import 'package:flutter_wallet/wallet/YSWalletManager.dart';
import 'package:provider/provider.dart';
import 'login/YSLogo.dart';
import 'login/view/YSHelpView.dart';

void main() async{
  WidgetsFlutterBinding.ensureInitialized();
  SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle.light.copyWith(statusBarColor: Colors.transparent));
  runApp(MultiProvider(
    providers: [
      ChangeNotifierProvider(create: (context) => CurrentLocale()),
      ChangeNotifierProvider(create: (context) => ThemeManager()),
      ChangeNotifierProvider(create: (context) => YSWalletNotifier()),
      ChangeNotifierProvider(create: (context) => YSBalanceNotifier()),
      ChangeNotifierProvider(create: (context) => YSTokenNotifier()),
      ChangeNotifierProvider(create: (context) => YSBalanceTotalNotifier()),
      ChangeNotifierProvider(create: (context) => YSPriceTypeNotifier()),
      ChangeNotifierProvider(create: (context) => YSUnitNotifier()),
    ],
    child: const MyApp(),
  ));
}

class MyApp extends StatefulWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  MyAppState createState() => MyAppState();
}

class MyAppState extends State<MyApp> {

  @override
  void initState() {
    _getValue();
    super.initState();
  }
  _getValue() async{
    YSUserRecord().them = await YSSharedPreferences.getStringValue('them');
    if(YSUserRecord().them.isEmpty){
      YSSharedPreferences.setStringValue('them', 'dart');
      YSUserRecord().them = 'light';
    }
    YSUserRecord().language = await YSSharedPreferences.getStringValue('language');
    if(YSUserRecord().language.isEmpty){
      YSSharedPreferences.setStringValue('language', 'zh');
      YSUserRecord().language = 'zh';
    }
    YSUserRecord().price = await YSSharedPreferences.getStringValue('price');
    if(YSUserRecord().price.isEmpty){
      YSSharedPreferences.setStringValue('price', '1');
      YSUserRecord().price = '1';
    }
    YSUserRecord().unit = await YSSharedPreferences.getStringValue('unit');
    if(YSUserRecord().unit.isEmpty){
      YSSharedPreferences.setStringValue('unit', 'dollar');
      YSUserRecord().unit = 'dollar';
    }
    YSUserRecord().lock = await YSSharedPreferences.getStringValue('lock');
    if(YSUserRecord().lock.isEmpty){
      YSSharedPreferences.setStringValue('lock', '0');
      YSUserRecord().lock = '0';
    }
    YSUserRecord().install = await YSSharedPreferences.getStringValue('install');
    YSUserRecord().priceType = await YSSharedPreferences.getStringValue('priceType');
    if(YSUserRecord().priceType.isEmpty){
      YSSharedPreferences.setStringValue('priceType', '1');
      YSUserRecord().priceType = '1';
    }
    if(!mounted)return;
    setThemFunc(context, YSUserRecord().them=='dart'?darkTheme:lightTheme);
    YSLanguage().changeLanguage(context, YSUserRecord().language);
  }
  @override
  Widget build(BuildContext context) {
    return ScreenUtilInit(
      designSize: const Size(375, 812),
      minTextAdapt: true,
      splitScreenMode: true,
      builder: (BuildContext context, Widget? child) {
        return Container(
          color: Colors.white,
          child: Consumer2(
            builder: (context, CurrentLocale cure, ThemeManager them,child) {
              // if('${them.value}'=='ThemeData#541ad'){
              //   SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle.light.copyWith(statusBarColor: Colors.transparent));
              // }else if('${them.value}'=='ThemeData#54398'){
              //   SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle.dark.copyWith(statusBarColor: Colors.transparent));
              // }
              return MaterialApp(
                debugShowCheckedModeBanner: false,
                title: 'Bake Wallet',
                theme: them.value,
                localizationsDelegates: const [
                  S.delegate,
                  GlobalMaterialLocalizations.delegate,
                  GlobalWidgetsLocalizations.delegate,
                  GlobalCupertinoLocalizations.delegate,
                ],
                routes: {
                  "YSWalletManager": (context) => const YSWalletManager(),
                },
                supportedLocales: S.delegate.supportedLocales,
                locale: cure.value,
                home: const YSLogo(),
              );
            },
          ),
        );
      },
    );
  }
}


// class MyApp extends StatelessWidget {
//   const MyApp({Key? key}) : super(key: key);
//
//   @override
//   Widget build(BuildContext context) {
//     return ScreenUtilInit(
//       designSize: const Size(375, 812),
//       minTextAdapt: true,
//       splitScreenMode: true,
//       builder: (BuildContext context, Widget? child) {
//         return Consumer2(
//           builder: (context, CurrentLocale cure, ThemeManager them,child) {
//             // if('${them.value}'=='ThemeData#541ad'){
//             //   SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle.light.copyWith(statusBarColor: Colors.transparent));
//             // }else if('${them.value}'=='ThemeData#54398'){
//             //   SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle.dark.copyWith(statusBarColor: Colors.transparent));
//             // }
//             return MaterialApp(
//               debugShowCheckedModeBanner: false,
//               title: 'Bake Wallet',
//               theme: them.value,
//               localizationsDelegates: const [
//                 S.delegate,
//                 GlobalMaterialLocalizations.delegate,
//                 GlobalWidgetsLocalizations.delegate,
//                 GlobalCupertinoLocalizations.delegate,
//               ],
//               routes: {
//                 "YSWalletManager": (context) => const YSWalletManager(),
//               },
//               supportedLocales: S.delegate.supportedLocales,
//               locale: cure.value,
//               home: const YSLogo(),
//             );
//           },
//         );
//       },
//     );
//   }
// }

