import 'dart:io';

import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:marking_app/routes/RouterManager.dart';
import 'package:marking_app/utils/const_text.dart';

import 'index.dart';
import 'my_text.dart';

class Protocol extends Dialog {
  final BuildContext context;
  const Protocol(this.context);

  @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: 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: Color.fromRGBO(51, 51, 51, 1)),
                    ),
                    TextSpan(
                      text: '《隐私政策》',
                      style: TextStyle(fontSize: 13.sp, color: Color.fromRGBO(233, 85, 119, 1)),
                      recognizer: TapGestureRecognizer()
                        ..onTap = () async {
                          RouterManager.router.navigateTo(
                            context,
                            '${RouterManager.agreementPath}?type=${AGREEMENT_KEY.PRIVACY_GREEMENT.name}',
                            transition: getTransition(),
                          );
                        },
                    ),
                    TextSpan(
                      text: '和',
                      style: TextStyle(fontSize: 13.sp, color: Color.fromRGBO(51, 51, 51, 1)),
                    ),
                    TextSpan(
                      text: '《用户协议》',
                      style: TextStyle(fontSize: 13.sp, color: Color.fromRGBO(233, 85, 119, 1)),
                      recognizer: TapGestureRecognizer()
                        ..onTap = () async {
                          RouterManager.router.navigateTo(
                            context,
                            '${RouterManager.agreementPath}?type=${AGREEMENT_KEY.USER_AGREEMENT.name}',
                            transition: getTransition(),
                          );
                        },
                    ),
                    TextSpan(
                      text:
                          '内的所有条款，尤其是:1.我们对您的个人信息的收集/保存/使用/对外提供/保护等规则条款，以及您的用户权利等条款;2.约定我们的限制责任、免责条款;3.其他以颜色或加粗进行标识的重要条款。如您对以上协议有任何疑问，可通过人工客服或发邮件至xrydyj@outlook.com与我们联系。您点击"同意并继续”的行为即表示您已阅读完毕并同意以上协议的全部内容。如您同意以上协议内容，请点击"同意并继续”，开始使用我们的产品和服务!',
                      style: TextStyle(fontSize: 13.sp, color: 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: Color.fromRGBO(238, 238, 238, 1)),
                          right: BorderSide(width: 0.5.r, color: Color.fromRGBO(238, 238, 238, 1)),
                        ),
                      ),
                      child: quickText('不同意', size: 14.sp),
                    ),
                  ),
                ),
                Expanded(
                  child: InkWell(
                    onTap: () {
                      FastData.getInstance().setSysProtocol(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: Color.fromRGBO(238, 238, 238, 1)),
                        ),
                      ),
                      child: quickText('同意并继续', color: Color.fromRGBO(206, 97, 126, 1), size: 14.sp),
                    ),
                  ),
                )
              ],
            ),
          ],
        ),
      ),
    );
  }
}

/// 系统协议
Future<void> sysProtocol(BuildContext context) async {
  bool? _sysProtocol = await FastData.getInstance().getSysProtocol();
  if (_sysProtocol == null || !_sysProtocol) {
    print('进来这里....');
    bool? _sysProtocolShow = await FastData.getInstance().getSysProtocolShow();
    if (_sysProtocolShow != true) {
      try {
        await FastData.getInstance().setSysProtocolShow(true);
        return showDialog(
          context: context,
          barrierDismissible: false,
          // useRootNavigator: false,
          builder: (ctx) => Protocol(ctx),
        );
      } catch (e) {
        FastData.getInstance().cleanSysProtocol();
      } finally {
        FastData.getInstance().cleanSysProtocolShow();
      }
    }
  }
}
