// ignore_for_file: must_be_immutable

import 'package:flutter/material.dart';
import 'package:flyinsono/lab/color/lab_colors.dart';
import 'package:get/get.dart';

class LabAlertDialog<T> extends StatelessWidget {
  LabAlertDialog({
    Key? key,
    required this.title,
    required this.content,
    this.confirmText = "确定",
    this.cancelText = "取消",
    this.maxWidth = 500,
    this.showLoading = false,
    this.showCloseIcon = true,
    this.showCancelButton = true,
    this.contentVerticalPadding = 30,
    this.contentHorizontalPadding = 0,
    this.onConfirm,
    this.onCancel,
  }) : super(key: key);

  final String title;

  /// 内容
  final Widget content;
  final String confirmText;
  final String cancelText;
  final double maxWidth;
  final bool showLoading;
  final bool showCloseIcon;
  final bool showCancelButton;
  final double contentVerticalPadding;
  final double contentHorizontalPadding;

  /// 确定回调，不为空则显示确定按钮
  final VoidCallback? onConfirm;
  final VoidCallback? onCancel;

  static ButtonStyle buttonStyle = ElevatedButton.styleFrom(
    elevation: 0.0,
    backgroundColor: LabColors.dialogHeaderColor,
    padding: EdgeInsets.symmetric(
      horizontal: 12,
      vertical: 5,
    ),
  );

  static const TextStyle buttonTextStyle = TextStyle(
    fontSize: 15,
    height: 1,
    color: LabColors.text200,
  );

  static Future<T?> showDialog<T>(
    Widget child, [
    bool barrierDismissible = false,
  ]) async {
    return await Get.dialog<T>(
      child,
      barrierDismissible: barrierDismissible,
      barrierColor: LabColors.base100.withOpacity(0.1),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Dialog(
      backgroundColor: Colors.transparent,
      insetPadding: EdgeInsets.all(0),
      child: Container(
        clipBehavior: Clip.antiAlias,
        constraints: BoxConstraints(maxWidth: maxWidth),
        decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(5),
          // border: Border.all(
          //   color: LabColors.mainPageHeaderColor,
          //   width: 2,
          //   strokeAlign: 0.0,
          // ),
          color: LabColors.base200,
        ),
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            Container(
              color: LabColors.dialogHeaderColor,
              padding: EdgeInsets.symmetric(vertical: 5),
              child: SizedBox(
                height: 40,
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    SizedBox(width: 15),
                    Text(
                      title,
                      style: TextStyle(
                        fontSize: 18,
                        color: LabColors.text200,
                      ),
                    ),
                    Expanded(child: Container()),
                    if (showCloseIcon)
                      IconButton(
                        icon: Icon(
                          Icons.close_rounded,
                          color: LabColors.text200,
                          size: 20,
                        ),
                        onPressed: () {
                          Get.back(result: null);
                        },
                      ),
                  ],
                ),
              ),
            ),
            // SizedBox(height: contentVerticalPadding),
            Padding(
              padding: EdgeInsets.symmetric(
                  horizontal: contentHorizontalPadding,
                  vertical: contentVerticalPadding),
              child: content,
            ),
            // SizedBox(height: contentVerticalPadding),
            if (onConfirm != null)
              Container(
                height: 40,
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  crossAxisAlignment: CrossAxisAlignment.stretch,
                  children: [
                    Container(
                      width: 100,
                      child: ElevatedButton(
                        style: buttonStyle,
                        child: Text(confirmText, style: buttonTextStyle),
                        onPressed: () => onConfirm?.call(),
                      ),
                    ),
                    if (showCancelButton) SizedBox(width: 20),
                    if (showCancelButton)
                      Container(
                        width: 100,
                        child: ElevatedButton(
                          style: buttonStyle,
                          child: Text(cancelText, style: buttonTextStyle),
                          onPressed: () {
                            if (onCancel != null)
                              onCancel?.call();
                            else
                              Get.back(result: null);
                          },
                        ),
                      ),
                  ],
                ),
              ),
            if (onConfirm != null) SizedBox(height: 15),
          ],
        ),
      ),
    );
  }
}
