import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:sk_base_mobile/config.dart';
import 'package:sk_base_mobile/services/storage.service.dart';
import 'package:sk_base_mobile/app_theme.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:sk_base_mobile/widgets/refresh_footer.dart';
import 'package:sk_base_mobile/widgets/refresh_header.dart';

import 'constants/constants.dart';
import 'services/app_info.service.dart';

// The index screen shows login if no user identity was found, otherwise the landing screen
class IndexPage extends StatelessWidget {
  const IndexPage({super.key});
  @override
  Widget build(BuildContext context) {
    final isLandscape =
        MediaQuery.of(context).orientation == Orientation.landscape;
    return ScreenUtilInit(
        minTextAdapt: true,
        designSize: isLandscape
            ? const Size(1490, 932)
            : const Size(932, 1490), // 2800 * 1800p 309ppi
        builder: (buildContext, child) => RefreshConfiguration(
            headerBuilder: () => RefreshHeader(),
            footerBuilder: () => const RefreshFooter(),
            hideFooterWhenNotFull: true,
            headerTriggerDistance: 30,
            maxOverScrollExtent: 80,
            footerTriggerDistance: 150,
            child: GetMaterialApp(
                theme: theme,
                title: GloablConfig.DOMAIN_NAME,
                getPages: RouteConfig.getPages,
                initialRoute: getInitialRoute(),
                // initialRoute: RouteConfig.onboarding,
                debugShowCheckedModeBanner: false,
                builder: (_, widget) {
                  return Obx(() => MediaQuery(
                      //设置文字大小不随系统设置改变
                      data: MediaQuery.of(context).copyWith(
                          textScaleFactor: AppInfoService.to.scale.value),
                      child: widget!));
                })));
  }

  String getInitialRoute() {
    if (AppInfoService.to.checkIsFirstInstall()) {
      // 如果是第一次打开app打开的页面
    }
    final token =
        StorageService.to.getString(CacheKeys.token, isWithUser: false);
    bool isLogined = token != null;
    return isLogined ? RouteConfig.home : RouteConfig.login;
  }
}
