import 'dart:io';
import 'package:get/get.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:making_school_asignment_app/common/const_text.dart';
import 'package:making_school_asignment_app/common/store/app_storage_key.dart';
import 'package:making_school_asignment_app/common/utils/storage.dart';
import 'package:making_school_asignment_app/common/utils/utils.dart';
import 'package:making_school_asignment_app/page/global_widget/my_text.dart';
import 'package:making_school_asignment_app/routes/app_pages.dart';

class Protocol extends Dialog {
  final BuildContext context;
  const Protocol(this.context, {super.key});

  @override
  Widget build(BuildContext context) {
    return Center(
      child: Container(
        width: isPad() ? 200.w : 260.w,
        height: 400.h,
        padding: EdgeInsets.symmetric(vertical: 16.h),
        decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.circular(12.r),
        ),
        child: Column(
          children: <Widget>[
            quickText('用户协议与隐私政策', size: 15.sp, color: const Color.fromRGBO(37, 37, 37, 1), fontWeight: FontWeight.bold),
            Expanded(
              child: ListView(
                physics: const BouncingScrollPhysics(),
                padding: EdgeInsets.fromLTRB(16.w, 14.h, 16.w, 10.h),
                children: [
                  Text.rich(TextSpan(children: [
                    TextSpan(
                      text: '感谢您选择点智学APP ! 我们非常重视您的个人信息和隐私保护。为了更好地保障您的个人权益，在您使用我们的产品前，请务必审慎阅读',
                      style: TextStyle(fontSize: 13.sp, color: const Color.fromRGBO(51, 51, 51, 1)),
                    ),
                    TextSpan(
                      text: '《隐私政策》',
                      style: TextStyle(fontSize: 13.sp, color: Theme.of(context).primaryColor),
                      recognizer: TapGestureRecognizer()
                        ..onTap = () async {
                          Get.toNamed(Routes.agreementPage, arguments: {"type": AGREEMENT_KEY.PRIVACY_GREEMENT.name});
                          // RouterManager.router.navigateTo(
                          //   context,
                          //   '${RouterManager.agreementPath}?type=${AGREEMENT_KEY.PRIVACY_GREEMENT.name}',
                          //   transition: getTransition(),
                          // );
                        },
                    ),
                    TextSpan(
                      text: '和',
                      style: TextStyle(fontSize: 13.sp, color: const Color.fromRGBO(51, 51, 51, 1)),
                    ),
                    TextSpan(
                      text: '《用户协议》',
                      style: TextStyle(fontSize: 13.sp, color: Theme.of(context).primaryColor),
                      recognizer: TapGestureRecognizer()
                        ..onTap = () async {
                          Get.toNamed(Routes.agreementPage, arguments: {"type": AGREEMENT_KEY.USER_AGREEMENT.name});
                          // RouterManager.router.navigateTo(
                          //   context,
                          //   '${RouterManager.agreementPath}?type=${AGREEMENT_KEY.USER_AGREEMENT.name}',
                          //   transition: getTransition(),
                          // );
                        },
                    ),
                    TextSpan(
                      text: '内的所有条款，尤其是:1.我们对您的个人信息的收集/保存/使用/对外提供/保护等规则条款，以及您的用户权利等条款;2.约定我们的限制责任、免责条款;3.其他以颜色或加粗进行标识的重要条款。如您对以上协议有任何疑问，可通过发邮件至yuanxuanjiaoyu@gmail.com与我们联系。您点击"同意并继续”的行为即表示您已阅读完毕并同意以上协议的全部内容。如您同意以上协议内容，请点击"同意并继续”，开始使用我们的产品和服务!',
                      style: TextStyle(fontSize: 13.sp, color: const Color.fromRGBO(51, 51, 51, 1)),
                    ),
                  ])),
                ],
              ),
            ),
            SizedBox(height: 10.h),
            Row(
              children: [
                Expanded(
                  child: InkWell(
                    onTap: () {
                      exit(0);
                    },
                    child: Container(
                      alignment: Alignment.center,
                      padding: EdgeInsets.only(top: 15.h),
                      decoration: BoxDecoration(
                        border: Border(
                          top: BorderSide(width: 0.5.r, color: const Color.fromRGBO(238, 238, 238, 1)),
                          right: BorderSide(width: 0.5.r, color: const Color.fromRGBO(238, 238, 238, 1)),
                        ),
                      ),
                      child: quickText('退出APP', size: 14.sp),
                    ),
                  ),
                ),
                Expanded(
                  child: InkWell(
                    onTap: () {
                      StorageService.to.write(AppStorageKey.privacyAgreement.value, true);
                      Navigator.of(context).pop(true);
                    },
                    child: Container(
                      alignment: Alignment.center,
                      padding: EdgeInsets.only(top: 15.h),
                      decoration: BoxDecoration(
                        border: Border(
                          top: BorderSide(width: 0.5.r, color: const Color.fromRGBO(238, 238, 238, 1)),
                        ),
                      ),
                      child: quickText('同意并继续', color: Theme.of(context).primaryColor, size: 14.sp),
                    ),
                  ),
                )
              ],
            ),
          ],
        ),
      ),
    );
  }
}

/// 系统协议
Future<void> sysProtocol(BuildContext context) async {
  bool? sysProtocol = StorageService.to.hasData(AppStorageKey.privacyAgreement.value);

  if (!sysProtocol) {
    return showDialog(
      context: context,
      barrierDismissible: false,
      // useRootNavigator: false,
      builder: (ctx) => Protocol(ctx),
    );
  }
}

/// 辅助阅读协议
Future<bool> assistedReadingProtocol(BuildContext context) async {
  var flag = await showDialog<bool>(
    context: context,
    barrierDismissible: false,
    // useRootNavigator: false,
    builder: (ctx) => const ProtocolAssistedGuidance(),
  );
  print("点击返回值 ：$flag");
  return flag ?? false;
}

/// 协议辅助引导
class ProtocolAssistedGuidance extends Dialog {
  const ProtocolAssistedGuidance({super.key});

  @override
  Widget build(BuildContext context) {
    return Center(
      child: Container(
        width: isPad() ? 216.w : 260.w,
        height: 200.h,
        padding: EdgeInsets.symmetric(horizontal: 4.w,vertical: 16.h),
        decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.circular(12.r),
        ),
        child: Column(
          children: <Widget>[
            quickText('协议及政策阅读指引', size: 16.sp, color: const Color.fromRGBO(37, 37, 37, 1), fontWeight: FontWeight.bold),
            Expanded(
              child: ListView(
                physics: const BouncingScrollPhysics(),
                padding: EdgeInsets.fromLTRB(10.w, 14.h, 10.w, 10.h),
                children: [
                  Text.rich(TextSpan(children: [
                    TextSpan(
                      text: '感谢您选择点智学，在使用当前APP服务之前，请仔细阅读',
                      style: TextStyle(fontSize: 13.sp, color: const Color.fromRGBO(51, 51, 51, 1)),
                    ),
                    TextSpan(
                      text: '《隐私政策》',
                      style: TextStyle(fontSize: 13.sp, color: Theme.of(context).primaryColor),
                      recognizer: TapGestureRecognizer()
                        ..onTap = () async {
                          Get.toNamed(Routes.agreementPage, arguments: {"type": AGREEMENT_KEY.PRIVACY_GREEMENT.name});
                        },
                    ),
                    TextSpan(
                      text: '和',
                      style: TextStyle(fontSize: 13.sp, color: const Color.fromRGBO(51, 51, 51, 1)),
                    ),
                    TextSpan(
                      text: '《用户协议》',
                      style: TextStyle(fontSize: 13.sp, color: Theme.of(context).primaryColor),
                      recognizer: TapGestureRecognizer()
                        ..onTap = () async {
                          Get.toNamed(Routes.agreementPage, arguments: {"type": AGREEMENT_KEY.USER_AGREEMENT.name});
                        },
                    ),
                    TextSpan(
                      text: '如果你已经阅读并同意此协议及政策，请点击“同意并继续”开始使用。',
                      style: TextStyle(fontSize: 13.sp, color: const Color.fromRGBO(51, 51, 51, 1)),
                    ),
                  ])),
                ],
              ),
            ),
            SizedBox(height: 10.h),
            Row(
              children: [
                Expanded(
                  child: InkWell(
                    onTap: () => Navigator.of(context).pop(false),
                    child: Container(
                      alignment: Alignment.center,
                      padding: EdgeInsets.only(top: 15.h),
                      decoration: BoxDecoration(
                        border: Border(
                          top: BorderSide(width: 0.5.r, color: const Color.fromRGBO(238, 238, 238, 1)),
                          right: BorderSide(width: 0.5.r, color: const Color.fromRGBO(238, 238, 238, 1)),
                        ),
                      ),
                      child: quickText('拒绝', size: 14.sp),
                    ),
                  ),
                ),
                Expanded(
                  child: InkWell(
                    onTap: () => Navigator.of(context).pop(true),
                    child: Container(
                      alignment: Alignment.center,
                      padding: EdgeInsets.only(top: 15.h),
                      decoration: BoxDecoration(
                        border: Border(
                          top: BorderSide(width: 0.5.r, color: const Color.fromRGBO(238, 238, 238, 1)),
                        ),
                      ),
                      child: quickText('同意并继续', color: Theme.of(context).primaryColor, size: 15.sp),
                    ),
                  ),
                )
              ],
            ),
          ],
        ),
      ),
    );
  }
}
