import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:get/get.dart';
import 'package:keyboard/utils/common_expand.dart';

import '../data/consts/web_url.dart';
import '../module/browser/browser_page.dart';
import '../resource/assets.gen.dart';
import '../resource/colors.gen.dart';
import '../resource/string.gen.dart';
import '../utils/styles.dart';
import '../widget/click_text_span.dart';

class AgreementDialog {
  static final _tag = "AgreementDialog";

  static void show({
    required VoidCallback cancelClick,
    required VoidCallback sureClick,
  }) {
    SmartDialog.show(
      tag: _tag,
      backType: SmartBackType.block,
      builder: (_) {
        return _AgreementDialog(cancelClick, sureClick);
      },
      alignment: Alignment.center,
      clickMaskDismiss: false,
    );
  }

  static void dismiss() {
    SmartDialog.dismiss(tag: _tag);
  }
}

class _AgreementDialog extends Dialog {
  final VoidCallback cancelClick;
  final VoidCallback sureClick;

  _AgreementDialog(this.cancelClick, this.sureClick);

  @override
  Widget build(BuildContext context) {
    return Container(
      width: 300.w,
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(20.w),
      ),
      child: Container(
        width: 300.w,
        decoration: BoxDecoration(
          image: DecorationImage(
            image: Assets.images.bgAgreementDialog.provider(),
            fit: BoxFit.contain,
            alignment: Alignment.topCenter,
          ),
        ),
        child: Column(
          mainAxisSize: MainAxisSize.min,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            SizedBox(height: 114.w),
            SizedBox(
              width: 241.w,
              child: Opacity(
                opacity: 0.80,
                child: Text.rich(
                  textAlign: TextAlign.start,
                  TextSpan(
                    children: [
                      TextSpan(
                        text:
                            '亲爱的用户，为您提供更好的服务，我们需要您同意我们的用户协议和隐私政策。这些文件包含了关于您如何使用我们的服务、我们如何处理您的个人信息以及您的权利和责任的重要信息。请花些时间仔细阅读它们 ',
                        style: TextStyle(
                          color: Colors.black.withAlpha(204),
                          fontSize: 14,
                          fontWeight: FontWeight.w400,
                          height: 1.43,
                        ),
                      ),
                      TextSpan(
                        text: '隐私政策',
                        style: TextStyle(
                          color: const Color(0xFF374BFF),
                          fontSize: 14,
                          fontWeight: FontWeight.w500,
                          decoration: TextDecoration.underline,
                          decorationColor: const Color(0xFF374BFF),
                          height: 1.43,
                        ),
                        recognizer:
                            TapGestureRecognizer()
                              ..onTap = () {
                                BrowserPage.start(WebUrl.privacyPolicy);
                              },
                      ),
                      TextSpan(
                        text: ' 和 ',
                        style: TextStyle(
                          color: Colors.black.withAlpha(204),
                          fontSize: 14,
                          fontWeight: FontWeight.w400,
                          height: 1.43,
                        ),
                      ),

                      TextSpan(
                        text: '用户协议',
                        style: TextStyle(
                          color: const Color(0xFF374BFF),
                          fontSize: 14,
                          fontWeight: FontWeight.w500,
                          decoration: TextDecoration.underline,
                          decorationColor: const Color(0xFF374BFF),
                          height: 1.43,
                        ),
                        recognizer:
                            TapGestureRecognizer()
                              ..onTap = () {
                                BrowserPage.start(WebUrl.serviceAgreement);
                              },
                      ),
                      TextSpan(
                        text: '全文，如您同意，请点击点击下方的“同意”按钮。',
                        style: TextStyle(
                          color: Colors.black.withAlpha(204),
                          fontSize: 14,
                          fontWeight: FontWeight.w400,
                          height: 1.43,
                        ),
                      ),
                    ],
                  ),
                ),
              ),
            ),
            SizedBox(height: 30.w),
            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                cancelText(StringName.privacyDisagree, () => cancel()),

                SizedBox(width: 16.w),
                sureText(StringName.privacyAgree, () {
                  sureClick();
                }),
              ],
            ),
            SizedBox(height: 30.w),
          ],
        ),
      ),
    );
  }

  void cancel() {
    cancelClick();
  }

  Widget cancelText(String txt, VoidCallback onTap) {
    return GestureDetector(
      onTap: onTap,
      child: Container(
        decoration: ShapeDecoration(
          color: const Color(0xFFF5F4F9),
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(50.r),
          ),
        ),
        width: 128.w,
        height: 40.w,
        child: Center(
          child: Text(
            txt,
            style: TextStyle(
              fontSize: 16.sp,
              color: Colors.black.withAlpha(102),
              fontWeight: FontWeight.w500,
            ),
          ),
        ),
      ),
    );
  }

  Widget sureText(String txt, VoidCallback onTap) {
    return GestureDetector(
      onTap: onTap,
      child: Container(
        decoration: Styles.getActivateButtonDecoration(50.r),
        width: 128.w,
        height: 40.w,
        child: Center(
          child: Text(
            txt,
            style: TextStyle(
              fontSize: 16.sp,
              color: Colors.white,
              fontWeight: FontWeight.w500,
            ),
          ),
        ),
      ),
    );
  }
}
