import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_markdown/flutter_markdown.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:flashlink/app/widgets/ix_image.dart';
import 'package:flashlink/app/widgets/submit_btn.dart';
import 'package:flashlink/config/theme/theme_extensions/theme_extension.dart';

import '../constants/assets.dart';
import '../routes/app_pages.dart';

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

  @override
  State<ProtocolOverlay> createState() => _ProtocolOverlayState();
}

class _ProtocolOverlayState extends State<ProtocolOverlay> {
  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      onWillPop: () async => false, // 禁止返回
      child: Scaffold(
        backgroundColor: Get.reactiveTheme.scaffoldBackgroundColor,
        body: SafeArea(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Container(
                width: 80.w,
                height: 80.w,
                margin: EdgeInsets.only(top: 67.w),
                alignment: Alignment.center,
                decoration: BoxDecoration(
                  color: Get.reactiveTheme.cardColor,
                  borderRadius: BorderRadius.circular(12.r),
                ),
                child: IXImage(
                  source: Assets.flashlinkLogo,
                  width: 43.w,
                  height: 49.w,
                  sourceType: ImageSourceType.asset,
                ),
              ),
              18.verticalSpaceFromWidth,
              IXImage(
                source: Assets.flashlink,
                width: 104.w,
                height: 40.w,
                fit: BoxFit.fitWidth,
                sourceType: ImageSourceType.asset,
              ),
              20.verticalSpaceFromWidth,
              Expanded(
                child: FutureBuilder(
                  future: rootBundle.loadString('assets/md/privacy.md'),
                  builder: (BuildContext context, AsyncSnapshot snapshot) {
                    if (snapshot.connectionState == ConnectionState.done) {
                      if (snapshot.hasData) {
                        return Markdown(
                          physics: const BouncingScrollPhysics(),
                          data: snapshot.data,
                        );
                      }
                    }
                    return Center(
                      child: SizedBox(
                        width: 20.w,
                        height: 20.w,
                        child: CircularProgressIndicator(
                          strokeWidth: 2,
                          valueColor: AlwaysStoppedAnimation<Color>(
                            Get.reactiveTheme.primaryColor,
                          ),
                        ),
                      ),
                    );
                  },
                ),
              ),
              Padding(
                padding: EdgeInsets.symmetric(horizontal: 20, vertical: 6.w),
                child: SubmitButton(
                  text: "Agree and continue",
                  onPressed: () {
                    Get.offAllNamed(Routes.HOME);
                  },
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
