import 'dart:ui';

import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:qr_flutter/qr_flutter.dart';

import '../resource/assets.gen.dart';
import '../resource/colors.gen.dart';
import '../resource/string.gen.dart';

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

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

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

class WechatQrCodeView extends Dialog {
  final String qrCodeUrl;
  VoidCallback? onCloseCallback;

  WechatQrCodeView({super.key, required this.qrCodeUrl, this.onCloseCallback});

  @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: QrImageView(
                      data: qrCodeUrl,
                      version: QrVersions.auto,
                      size: 220.w,
                      backgroundColor: Colors.white,
                    )),
                SizedBox(height: 16.w),
                Text(StringName.wechatPayQrCodeTips,
                    style: TextStyle(fontSize: 14.sp, color: ColorName.black90))
              ],
            ),
          ),
          SizedBox(height: 20.w),
          GestureDetector(
              onTap: () {
                onCloseCallback?.call();
                WechatQrCodeDialog.dismiss();
              },
              child: Assets.images.iconMemberRetainClose
                  .image(width: 32.w, height: 32.w))
        ],
      ),
    );
  }
}
