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:location/module/browser/browser_view.dart';
import 'package:location/resource/assets.gen.dart';
import 'package:location/resource/colors.gen.dart';
import 'package:location/resource/string.gen.dart';
import 'package:location/utils/common_expand.dart';

import '../data/consts/web_url.dart';

class AgreementDialog {
  static final _tag = "AgreementDialog";

  static void show(
      {required VoidCallback cancelClick, required VoidCallback sureClick}) {
    SmartDialog.show(
      tag: _tag,
      backDismiss: false,
      builder: (_) {
        return _AgreementDialog(cancelClick, sureClick);
      },
      clickMaskDismiss: false,
    );
  }

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

class _AgreementDialog extends Dialog {
  final RxBool isFirstStep = true.obs;

  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(8.w),
      ),
      child: IntrinsicHeight(
        child: Stack(
          children: [
            Column(children: [
              SizedBox(height: 24.w),
              Text(StringName.privacyTitle,
                  style: TextStyle(
                      fontSize: 17.sp,
                      color: '#202020'.color,
                      fontWeight: FontWeight.bold)),
              SizedBox(height: 16.w),
              Container(
                  margin: EdgeInsets.symmetric(horizontal: 24.w),
                  child: Obx(() {
                    if (isFirstStep.value) {
                      return oneStepContentRichText();
                    } else {
                      return twoStepContentRichText();
                    }
                  })),
              SizedBox(height: 28.w),
              Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Obx(() {
                    return cancelText(
                        isFirstStep.value
                            ? StringName.privacyDisagree
                            : StringName.privacyDisagreeAndExit,
                        () => cancel());
                  }),
                  SizedBox(width: 16.w),
                  sureText(StringName.privacyAgree, () {
                    AgreementDialog.dismiss();
                    sureClick();
                  })
                ],
              ),
              SizedBox(height: 24.w)
            ]),
            Positioned(
                top: 12.w,
                right: 12.w,
                child: GestureDetector(
                  onTap: () => cancel(),
                  child: Assets.images.iconAgreementClose
                      .image(width: 14.w, height: 14.w),
                )),
          ],
        ),
      ),
    );
  }

  Widget twoStepContentRichText() {
    return RichText(
      text: TextSpan(
          style: TextStyle(fontSize: 14.sp, color: '#404040'.color),
          children: [
            const TextSpan(
              text: "需要同意",
            ),
            TextSpan(
              text: StringName.privacyPolicy,
              style: TextStyle(color: '#2F79FF'.color),
              recognizer: TapGestureRecognizer()
                ..onTap = () {
                  BrowserPage.start(WebUrl.privacyPolicy);
                },
            ),
            const TextSpan(
              text: "和",
            ),
            TextSpan(
              text: StringName.termOfService,
              style: TextStyle(color: '#2F79FF'.color),
              recognizer: TapGestureRecognizer()
                ..onTap = () {
                  BrowserPage.start(WebUrl.userAgreement);
                },
            ),
            const TextSpan(
              text: "才能继续为您提供服务，如您选择不同意很遗憾我们难以继续为您继续服务。",
            ),
          ]),
    );
  }

  Widget oneStepContentRichText() {
    return RichText(
      text: TextSpan(
          style: TextStyle(fontSize: 14.sp, color: '#404040'.color),
          children: [
            const TextSpan(
              text:
                  "亲爱的用户，感谢您使用我们产品，为了更好的为您服务，我们可能向系统申请以下必要权限：定位信息权限、存储空间、电话权限等，用于应用的基本服务和功能），你有权拒绝或者撤回权限。\n本软件非常重视您的隐私和个人信息，在您使用之前请仔细阅读",
            ),
            TextSpan(
              text: StringName.privacyPolicy,
              style: TextStyle(color: '#2F79FF'.color),
              recognizer: TapGestureRecognizer()
                ..onTap = () {
                  BrowserPage.start(WebUrl.privacyPolicy);
                },
            ),
            const TextSpan(
              text: "和",
            ),
            TextSpan(
              text: StringName.termOfService,
              style: TextStyle(color: '#2F79FF'.color),
              recognizer: TapGestureRecognizer()
                ..onTap = () {
                  BrowserPage.start(WebUrl.userAgreement);
                },
            ),
            const TextSpan(
              text: "全文，如您同意，请点击点击下方的“同意”按钮。",
            ),
          ]),
    );
  }

  void privacyClick() {
    BrowserPage.start(WebUrl.privacyPolicy);
  }

  void userAgreementClick() {
    BrowserPage.start(WebUrl.userAgreement);
  }

  void cancel() {
    if (isFirstStep.value) {
      isFirstStep.value = !isFirstStep.value;
    } else {
      cancelClick();
    }
  }

  Widget cancelText(String txt, VoidCallback onTap) {
    return GestureDetector(
      onTap: onTap,
      child: Container(
        decoration: BoxDecoration(
          color: '#F8F8F8'.color,
          borderRadius: BorderRadius.circular(26.w),
        ),
        width: 118.w,
        height: 40.w,
        child: Center(
          child: Text(txt,
              style: TextStyle(
                  fontSize: 16.sp,
                  color: '#A7A7A7'.color,
                  fontWeight: FontWeight.bold)),
        ),
      ),
    );
  }

  Widget sureText(String txt, VoidCallback onTap) {
    return GestureDetector(
      onTap: onTap,
      child: Container(
        decoration: BoxDecoration(
          color: ColorName.colorPrimary,
          borderRadius: BorderRadius.circular(26.w),
        ),
        width: 118.w,
        height: 40.w,
        child: Center(
          child: Text(txt,
              style: TextStyle(
                  fontSize: 16.sp,
                  color: Colors.white,
                  fontWeight: FontWeight.bold)),
        ),
      ),
    );
  }
}
