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 AgreementAgainDialog {
  static final _tag = "AgreementAgainDialog";

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

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

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

  const _AgreementAgainDialog(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.bgAgreementAgainDialog.provider(),
            fit: BoxFit.contain,
            alignment: Alignment.topCenter,
          ),
        ),
        child: Column(
          mainAxisSize: MainAxisSize.min,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            SizedBox(height: 29.55.w),
            Text(
              StringName.privacyTitle,
              textAlign: TextAlign.center,
              style: TextStyle(
                color: const Color(0xFF202020),
                fontSize: 16.sp,
                fontWeight: FontWeight.w700,
              ),
            ),
            SizedBox(height: 30.05.w),
            SizedBox(
              width: 240.w,
              child: Opacity(
                opacity: 0.80,
                child: Text.rich(
                  TextSpan(
                    children: [
                      TextSpan(
                        text: '需要同意',
                        style: TextStyle(
                          color: Colors.black.withAlpha(204),
                          fontSize: 14.sp,
                          fontWeight: FontWeight.w400,
                          height: 1.43,
                        ),
                      ),
                      TextSpan(
                        text: '《隐私政策》',
                        style: TextStyle(
                          color: const Color(0xFF374BFF),
                          fontSize: 14.sp,
                          fontWeight: FontWeight.w500,
                          height: 1.43,
                        ),
                        recognizer:
                            TapGestureRecognizer()
                              ..onTap = () {
                                BrowserPage.start(WebUrl.privacyPolicy);
                              },
                      ),
                      TextSpan(
                        text: '和',
                        style: TextStyle(
                          color: Colors.black.withAlpha(204),
                          fontSize: 14.sp,
                          fontWeight: FontWeight.w400,
                          height: 1.43,
                        ),
                      ),
                      TextSpan(
                        text: '《用户协议》',
                        recognizer:
                            TapGestureRecognizer()
                              ..onTap = () {
                                BrowserPage.start(WebUrl.serviceAgreement);
                              },
                        style: TextStyle(
                          color: const Color(0xFF374BFF),
                          fontSize: 14.sp,
                          fontWeight: FontWeight.w500,
                          height: 1.43,
                        ),
                      ),
                      TextSpan(
                        text: '才能继续为您提供服务，如您选择不同意很遗憾我们难以继续为您继续服务。',
                        style: TextStyle(
                          color: Colors.black.withAlpha(204),
                          fontSize: 14.sp,
                          fontWeight: FontWeight.w400,
                          height: 1.43,
                        ),
                      ),
                    ],
                  ),
                ),
              ),
            ),
            SizedBox(height: 30.w),
            Container(
              margin: EdgeInsets.only(left: 25.w, right: 25.w),
              child: Row(
                children: [
                  Expanded(
                    child: sureText(StringName.privacyAgree, () {
                      sureClick();
                    }),
                  ),
                ],
              ),
            ),
            SizedBox(height: 18.w),
            cancelText(StringName.privacyDisagreeAndExit, () => cancel()),
            SizedBox(height: 30.w),
          ],
        ),
      ),
    );
  }

  void cancel() {
    cancelClick();
  }

  Widget cancelText(String txt, VoidCallback onTap) {
    return GestureDetector(
      onTap: onTap,
      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),
        height: 40.w,
        child: Center(
          child: Text(
            txt,
            style: TextStyle(
              fontSize: 16.sp,
              color: Colors.white,
              fontWeight: FontWeight.w500,
            ),
          ),
        ),
      ),
    );
  }
}
