import 'package:atmob_logging/atmob_logging.dart';
import 'package:flutter/material.dart';
import 'package:flutter_inappwebview/flutter_inappwebview.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/resource/assets.gen.dart';
import 'package:location/resource/colors.gen.dart';
import 'package:location/resource/string.gen.dart';

class AlipayQrCodeDialog {
  static const String _tag = 'AlipayQrCodeDialog';

  static void show(
      {required String qrCodeHtml,
      VoidCallback? onDismiss,
      VoidCallback? onCloseCallback,
      VoidCallback? loadSuccessCallback}) {
    SmartDialog.show(
        tag: _tag,
        onDismiss: onDismiss,
        backDismiss: false,
        builder: (_) {
          return AlipayQrCodeView(
              qrCodeHtml: qrCodeHtml,
              onCloseCallback: onCloseCallback,
              loadSuccessCallback: loadSuccessCallback);
        },
        clickMaskDismiss: false);
  }

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

class AlipayQrCodeView extends Dialog {
  final String qrCodeHtml;
  VoidCallback? loadSuccessCallback;
  VoidCallback? onCloseCallback;

  InAppWebViewController? webViewController;

  AlipayQrCodeView(
      {super.key,
      required this.qrCodeHtml,
      this.onCloseCallback,
      this.loadSuccessCallback});

  @override
  Widget build(BuildContext context) {
    return IntrinsicHeight(
      child: Column(
        children: [
          Container(
            padding: EdgeInsets.all(26.w),
            decoration: BoxDecoration(
              color: Colors.white,
              borderRadius: BorderRadius.circular(10.w),
            ),
            child: Column(
              children: [
                SizedBox(
                  width: 220.w,
                  height: 220.w,
                  child: GestureDetector(
                    behavior: HitTestBehavior.translucent,
                    child: InAppWebView(
                      initialData:
                          InAppWebViewInitialData(data: getHtmlTemplate()),
                      onPageCommitVisible: (controller, url) {
                        ALog.d("lzplzp", "onPageCommitVisible ${url?.host}");
                        if (url?.host.contains("alipay.com") ?? false) {
                          loadSuccessCallback?.call();
                          final scaleFactor = Get.width / 45.w;
                          controller.evaluateJavascript(source: '''
                          document.body.style.transform = "scale($scaleFactor)";
                        document.body.style.transformOrigin = "0 0";  
                        document.body.style.touchAction = 'none';
                        document.documentElement.style.overscrollBehavior = 'none';           
                          ''');
                        }
                      },
                    ),
                  ),
                ),
                SizedBox(height: 16.w),
                Text(StringName.alipayQrCodeTips,
                    style: TextStyle(fontSize: 14.sp, color: ColorName.black90))
              ],
            ),
          ),
          SizedBox(height: 20.w),
          GestureDetector(
              onTap: () {
                onCloseCallback?.call();
                AlipayQrCodeDialog.dismiss();
              },
              child: Assets.images.iconMemberRetainClose
                  .image(width: 32.w, height: 32.w))
        ],
      ),
    );
  }

  String getHtmlTemplate() {
    return '''
    <!DOCTYPE html><html lang=\"en\">
      <head><meta charset=\"UTF-8\"/>
        <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\"/>
        <title>Alipay QR Code</title>
      </head>
      <body>$qrCodeHtml</body>
     </html>
 
     ''';
  }
}
