import 'package:food_health/providers/app_store.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:food_health/providers/user_store.dart';
import 'package:go_router/go_router.dart';
import 'package:provider/provider.dart';

import '../../../config/app_context.dart';
import '../../../router/config/route_paths.dart';
import '../../../router/routes.dart';

class SplashPage extends StatefulWidget {
  const SplashPage({super.key});

  @override
  State<SplashPage> createState() => _SplashPageState();
}

class _SplashPageState extends State<SplashPage> {
  @override
  void initState() {
    super.initState();
    init();
  }

  void init() {
    WidgetsBinding.instance.addPostFrameCallback((_) async {
      AppContext.setContent(navigatorKey.currentState!.context);
      //效验
      AppStore appStore = context.read<AppStore>();
      await appStore.init();
      if (!mounted) return;
      if (appStore.token.isEmpty) {
        context.go(RoutePaths.login);
      } else {
        context.go(RoutePaths.layout);
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SizedBox(
        width: double.infinity,
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            Image.asset(
              "assets/image/logo.png",
              width: 68.w,
            ),
            Container(
              margin: EdgeInsets.only(top: 16),
              child: Text(
                "Demacare",
                style: Theme.of(context).textTheme.titleMedium,
              ),
            ),
          ],
        ),
      ),
    );
  }
}
