import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:location/resource/assets.gen.dart';
import 'package:location/utils/common_style.dart';

class CommonConfirmDialog {
  static const String _tag = "CommonConfirmDialog";

  static void show(
      {required Widget titleWidget,
      required Widget descWidget,
      required String confirmText,
      String? cancelText,
      VoidCallback? cancelOnTap,
      VoidCallback? onDismiss,
      bool? clickMaskDismiss,
      bool? backDismiss,
      required VoidCallback confirmOnTap,
      String tag = _tag}) {
    SmartDialog.show(
        tag: _tag,
        backDismiss: backDismiss,
        clickMaskDismiss: clickMaskDismiss,
        onDismiss: onDismiss,
        builder: (_) {
          return CommonConfirmView(
              titleWidget: titleWidget,
              descWidget: descWidget,
              confirmText: confirmText,
              cancelText: cancelText,
              cancelOnTap: () {
                CommonConfirmDialog.dismiss(tag: tag);
                cancelOnTap?.call();
              },
              confirmOnTap: () {
                CommonConfirmDialog.dismiss(tag: tag);
                confirmOnTap();
              });
        });
  }

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

class CommonConfirmView extends Dialog {
  final Widget titleWidget;
  final Widget descWidget;
  final String confirmText;
  final String? cancelText;
  final VoidCallback? cancelOnTap;
  final VoidCallback confirmOnTap;

  const CommonConfirmView({
    super.key,
    required this.titleWidget,
    required this.descWidget,
    required this.confirmText,
    this.cancelOnTap,
    this.cancelText,
    required this.confirmOnTap,
  });

  @override
  Widget build(BuildContext context) {
    return Container(
      width: 300.w,
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(20.w),
      ),
      child: IntrinsicHeight(
        child: Stack(
          children: [
            Center(
              child: Column(
                children: [
                  SizedBox(height: 27.w),
                  titleWidget,
                  SizedBox(height: 15.w),
                  Container(
                    margin: EdgeInsets.symmetric(horizontal: 28.w),
                    child: descWidget,
                  ),
                  SizedBox(height: 31.w),
                  GestureDetector(
                    onTap: () {
                      confirmOnTap();
                    },
                    child: Container(
                        width: 229.w,
                        height: 43.w,
                        decoration: getPrimaryBtnDecoration(46.w),
                        child: Center(
                            child: Text(confirmText,
                                style: TextStyle(
                                    fontSize: 14.sp, color: Colors.white)))),
                  ),
                  SizedBox(height: 20.w),
                ],
              ),
            ),
            Positioned(
                top: 12.w,
                right: 12.w,
                child: GestureDetector(
                    onTap: () {
                      cancelOnTap?.call();
                    },
                    child: Assets.images.iconDialogClose
                        .image(width: 20.w, height: 20.w)))
          ],
        ),
      ),
    );
  }
}
