import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:momo/material/router.dart';
import 'package:momo/provider/token.dart';
import 'package:shared_preferences/shared_preferences.dart';

class MyMaterialApp extends ConsumerWidget {
  const MyMaterialApp({Key? key}) : super(key: key);

  Future<String?> loadToken() async {
    final prefs = await SharedPreferences.getInstance();
    String? tk = prefs.getString("token");
    return tk;
  }

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    String? token = ref.watch(tokenProvider);
    // UniqueKey uniqueKey = ref.watch(uniqueIdProvider);
    MyMaterialRouterConfig myMaterialRouterConfig =
        MyMaterialRouterConfig(token);

    return MaterialApp.router(
      routerConfig: myMaterialRouterConfig.router,
      debugShowCheckedModeBanner: false,
      theme:
          ThemeData(useMaterial3: true, scaffoldBackgroundColor: Colors.white),
    );
    // return FutureBuilder(
    //     future: loadToken(),
    //     builder: (BuildContext context, AsyncSnapshot<String?> snapshot) {
    //       if (snapshot.connectionState == ConnectionState.done) {
    //         String? token = snapshot.data;
    //         if (token != null) {
    //           //   ref
    //           //       .watch(
    //           //         tokenProvider.notifier,
    //           //       )
    //           //       .setToken(token);
    //         }
    //         MyMaterialRouterConfig myMaterialRouterConfig =
    //             MyMaterialRouterConfig(token);
    //
    //         return MaterialApp.router(
    //           routerConfig: myMaterialRouterConfig.router,
    //           theme: ThemeData(
    //               useMaterial3: true, scaffoldBackgroundColor: Colors.white),
    //         );
    //       } else {
    //         return MaterialApp(
    //           theme: ThemeData(
    //               useMaterial3: true, scaffoldBackgroundColor: Colors.white),
    //           home: const Center(
    //             child: CircularProgressIndicator(),
    //           ),
    //         );
    //       }
    //     });
  }
}
