import 'package:clean/data/consts/constants.dart';
import 'package:clean/module/browser/browser_view.dart';
import 'package:clean/resource/assets.gen.dart';
import 'package:clean/resource/colors.gen.dart';
import 'package:flutter/Material.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:lottie/lottie.dart';

void privacyDialog({required Function onAgree, required Function onDisagree}) {
  const tag = 'privacyDialog';
  SmartDialog.show(
      tag: tag,
      backType: SmartBackType.normal,
      clickMaskDismiss: false,
      maskColor: ColorName.black,
      builder: (_) {
        return Container(
          height: double.infinity,
          width: double.infinity,
          child: Stack(
            children: [
              SafeArea(
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    GestureDetector(
                        onTap: () {
                          onDisagree();
                          SmartDialog.dismiss(tag: tag);
                        },
                        child: Container(
                          margin: EdgeInsets.only(left: 16.w),
                          width: 28.w,
                          height: 28.w,
                          decoration: ShapeDecoration(
                            color: Color(0xFF2A3E55),
                            shape: RoundedRectangleBorder(
                              borderRadius: BorderRadius.circular(86.r),
                            ),
                            image: DecorationImage(
                              image: Assets.images.iconPrivateClose.provider(),
                            ),
                          ),
                        )),
                    SizedBox(
                      height: 45.h,
                    ),
                    Expanded(
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.center,
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: [
                          SizedBox(
                            width: 245.w,
                            child: Text(
                              'Clean up your iphone and free up storage',
                              textAlign: TextAlign.center,
                              style: TextStyle(
                                color: Colors.white,
                                fontSize: 24.sp,
                                fontWeight: FontWeight.w500,
                                height: 1.25.h,
                              ),
                            ),
                          ),
                          SizedBox(
                            child: Lottie.asset(
                              Assets.anim.animPrivacy,
                              height: 412.h,
                              repeat: true,
                            ),
                          ),
                          SizedBox(
                            height: 8,
                          ),
                          Text(
                            'CleanPro values your privacy. By starting, you agree to ',
                            style: TextStyle(
                              color: Colors.white.withValues(alpha: 0.8),
                              fontSize: 12.sp,
                              fontWeight: FontWeight.w400,
                              height: 1.50.h,
                            ),
                          ),
                          Text.rich(
                            TextSpan(
                              children: [
                                TextSpan(
                                  text: 'our ',
                                  style: TextStyle(
                                    color: Colors.white.withValues(alpha: 0.8),
                                    fontSize: 12.sp,
                                    fontWeight: FontWeight.w400,
                                    height: 2.50.h,
                                  ),
                                ),
                                buildLinkText(
                                    'Privacy Policy', Constants.privacyPolicy),
                                TextSpan(
                                  text: ' and ',
                                  style: TextStyle(
                                    color: Colors.white.withValues(alpha: 0.8),
                                    fontSize: 12.sp,
                                    fontWeight: FontWeight.w400,
                                    height: 2.50.h,
                                  ),
                                ),
                                buildLinkText('Terms of Service',
                                    Constants.userAgreement),
                                TextSpan(
                                  text: '.',
                                  style: TextStyle(
                                    color: Colors.white,
                                    fontSize: 12.sp,
                                    fontWeight: FontWeight.w400,
                                    height: 2.50.h,
                                  ),
                                ),
                              ],
                            ),
                          ),
                          Spacer(),
                          GestureDetector(
                            onTap: () {
                              onAgree();
                              SmartDialog.dismiss(tag: tag);
                            },
                            child: Container(
                              width: 316.w,
                              height: 48.h,
                              decoration: ShapeDecoration(
                                color: Color(0xFF0279FB),
                                shape: RoundedRectangleBorder(
                                  borderRadius: BorderRadius.circular(24.r),
                                ),
                              ),
                              child: Center(
                                child: Text(
                                  'Start',
                                  style: TextStyle(
                                    color: Colors.white,
                                    fontSize: 16.sp,
                                    fontWeight: FontWeight.w500,
                                  ),
                                ),
                              ),
                            ),
                          ),
                        ],
                      ),
                    ),
                  ],
                ),
              ),
              IgnorePointer(
                child: Assets.images.bgPhotoSelectedPreviewFinish.image(
                  width: 360.w,
                ),
              ),
            ],
          ),
        );
      });
}

TextSpan buildLinkText(String text, String url) {
  return TextSpan(
    text: text,

    style: TextStyle(
      color: Colors.white,
      decoration: TextDecoration.underline,
      decorationColor: Colors.white,
      fontSize: 12.sp,
      fontWeight: FontWeight.w400,
      height: 2.50.h,
    ),
    // 链接文字样式
    recognizer: TapGestureRecognizer()
      ..onTap = () {
        BrowserPage.start(url);
      },
  );
}
