import 'package:flutter/material.dart';
import 'package:cs_resources/generated/l10n.dart';
import 'package:widgets/ext/ex_widget.dart';
import 'package:cs_resources/theme/app_colors_theme.dart';
import 'package:widgets/my_text_view.dart';
import 'package:widgets/no_shadow_scroll_behavior.dart';
import 'package:widgets/widget_export.dart';

class CommentsDialog extends StatelessWidget {
  String? title;
  Widget? Function(BuildContext)? messageBuilder;
  VoidCallback confirmAction;
  VoidCallback? cancelAction;
  bool isShowCancelBtn;
  bool isShowConfirmBtn;
  String? confirmTxt;
  String? cancelTxt;
  double? height = 0.0;
  double? minHeight = 0.0;
  double? maxHeight;
  double? maxWidth = 300;

  CommentsDialog({
    this.title,
    Widget Function(BuildContext)? this.messageBuilder,
    required this.confirmAction,
    this.cancelAction,
    this.isShowCancelBtn = true,
    this.isShowConfirmBtn = true,
    this.confirmTxt,
    this.cancelTxt,
    height,
    minHeight,
    maxHeight,
    maxWidth,
    Key? key,
  }): height = height ?? 0.0,
        minHeight = minHeight ?? 0.0,
        maxHeight = maxHeight ?? 5000,
        maxWidth = maxWidth ?? 500,
        super(key: key);

  @override
  Widget build(BuildContext context) {
    final keyboardHeight = MediaQuery.of(context).viewInsets.bottom;

    return Container(
        width: double.infinity,
        constraints: BoxConstraints(
          minHeight: minHeight!,
          maxHeight: maxHeight!,
          maxWidth: maxWidth!,
          minWidth: 0.0,
        ),
        decoration: BoxDecoration(
          color: context.appColors.whiteSecondBG,
          borderRadius: const BorderRadius.only(
            topLeft: Radius.circular(0),
            topRight: Radius.circular(0),
          ),
        ),
        child: SingleChildScrollView(
            reverse: true, // 反向滚动，确保输入框在键盘上方
            child: Column(
                mainAxisSize: MainAxisSize.min,
                crossAxisAlignment: CrossAxisAlignment.center,
                children: [
                    messageBuilder!(context) ?? Container(), // 处理 null 情况
                    Visibility(
                      visible: isShowConfirmBtn && isShowCancelBtn,
                      child: Row(
                        children: [
                          const SizedBox(width: 18),
                          Visibility(
                            visible: isShowCancelBtn,
                            child: Expanded(
                              flex: 1,
                              child: InkWell(
                                onTap: () {
                                  onCancel();
                                  cancelAction?.call();
                                },
                                child: MyTextView(
                                  cancelTxt ?? S.current.no,
                                  fontSize: 16,
                                  isFontMedium: true,
                                  paddingTop: 13,
                                  marginRight: 15,
                                  paddingBottom: 13,
                                  textAlign: TextAlign.center,
                                  textColor: Colors.white,
                                  backgroundColor: context.appColors.orangeBG,
                                  cornerRadius: 7,
                                ),
                              ),
                            ),
                          ),
                          Expanded(
                            flex: 1,
                            child: Visibility(
                              visible: isShowConfirmBtn,
                              child: InkWell(
                                onTap: () async {
                                  onCancel();
                                  confirmAction();
                                },
                                child: MyTextView(
                                  confirmTxt ?? S.current.yes,
                                  fontSize: 16,
                                  paddingTop: 13,
                                  paddingBottom: 13,
                                  isFontMedium: true,
                                  textAlign: TextAlign.center,
                                  textColor: Colors.white,
                                  backgroundColor: context.appColors.btnBgDefault,
                                  cornerRadius: 7,
                                ),
                              ),
                            ),
                          ),
                          const SizedBox(width: 18),
                        ],
                      ).marginOnly(bottom: 30, top: 28),
                    ),
                ],
            ),
        ).marginOnly(
          bottom: keyboardHeight + 10,
          top: 5,
        ),
      );
  }

  // 取消弹框
  void onCancel() async {
    SmartDialog.dismiss();
  }
}
   