import 'dart:async';

import 'package:flutter/material.dart';
import 'package:fis_common/logger/logger.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';
import 'package:vitalapp/components/floating_window/index.dart';
import 'package:vitalapp/store/store.dart';

import 'global.dart';
import 'routes/routes.dart';

void main() async {
  runZonedGuarded(
    () async {
      try {
        WidgetsFlutterBinding.ensureInitialized();
        await Global.init();
      } catch (e) {
        logger.e('Global init Error', e);
      }
      runApp(const _App());
    },
    (error, stack) {
      // GlobalErrorHandler.handle(error, stack);
    },
  );
}

class _App extends StatelessWidget {
  const _App();

  @override
  Widget build(BuildContext context) {
    return GetMaterialApp(
      title: "家医一体机",
      theme: ThemeData(
        // primaryColor: const Color.fromRGBO(0, 178, 237, 1),
        // TODO:
        primaryColor: Colors.blue,
        colorScheme:
            ColorScheme.fromSeed(seedColor: Color(Colors.blue.value - 40)),
        // colorScheme: ColorScheme.fromSeed(
        //   // seedColor: const Color.fromRGBO(44, 119, 229, 1),
        //   // seedColor: Colors.lightBlue,
        //   seedColor: const Color.fromRGBO(0, 178, 237, 1),
        // ),
        // colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
        useMaterial3: true,
        fontFamily: "NotoSansSC-R-fixed",
        fontFamilyFallback: const ["NotoSansSC-R-fixed"],
      ),
      debugShowCheckedModeBanner: false,
      getPages: Routes.routes,
      initialRoute: "splash",
      // initialRoute: "/patient/create",
      // initialRoute: "/contract/signature",
      // initialRoute: "/",
      builder: EasyLoading.init(
        builder: (context, widget) {
          const designWidth = 1280.0; // 设计尺寸宽度：1280
          final size = MediaQuery.of(context).size;
          final scale = size.width / designWidth; // 计算缩放比例
          return Obx(() {
            if (!Store.user.isShowUserCard) {
              return FittedBox(
                fit: BoxFit.fitWidth,
                child: SizedBox(
                  width: designWidth,
                  height: size.height / scale,
                  child: Center(child: widget ?? const SizedBox()),
                ),
              );
            }
            return HoveringPatientCard(
              bgChild: FittedBox(
                fit: BoxFit.fitWidth,
                child: SizedBox(
                  width: designWidth,
                  height: size.height / scale,
                  child: Center(child: widget ?? const SizedBox()),
                ),
              ),
              child: Container(
                width: 80,
                height: 80,
                decoration: BoxDecoration(
                  color: Colors.blue.withOpacity(0.5),
                  border: Border.all(color: Colors.blue),
                  borderRadius: const BorderRadius.all(
                    Radius.circular(50),
                  ),
                ),
                child: Center(
                  child: _buildPatient(),
                ),
              ),
            );
          });

          // return widget ?? const SizedBox();
        },
      ),
    );
  }

  Widget _buildPatient() {
    if (Store.user.currentSelectPatientInfo?.patientName?.isNotEmpty ?? false) {
      return Text(
        Store.user.currentSelectPatientInfo?.patientName ?? '',
        style: const TextStyle(
          color: Colors.white,
          fontSize: 20,
        ),
        overflow: TextOverflow.ellipsis,
      );
    } else {
      return const Icon(
        Icons.add,
        size: 40,
        color: Colors.white,
      );
    }
  }
}
