import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:syncrow_web/utils/color_manager.dart';

class DeleteUserDialog extends StatefulWidget {
  final Function()? onTapDelete;
  DeleteUserDialog({super.key, this.onTapDelete});

  @override
  _DeleteUserDialogState createState() => _DeleteUserDialogState();
}

class _DeleteUserDialogState extends State<DeleteUserDialog> {
  bool isLoading = false;
  bool _isDisposed = false;

  @override
  void dispose() {
    _isDisposed = true;
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Dialog(
        child: Container(
      height: 160,
      width: 200,
      decoration: const BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.all(Radius.circular(20))),
      child: Column(
        children: [
          const Padding(
            padding: EdgeInsets.all(8.0),
            child: SizedBox(
              child: Text(
                "Delete User",
                style: TextStyle(
                    color: ColorsManager.red,
                    fontSize: 18,
                    fontWeight: FontWeight.bold),
              ),
            ),
          ),
          const Padding(
            padding: EdgeInsets.only(
              left: 25,
              right: 25,
            ),
            child: Divider(),
          ),
          const Expanded(
              child: Padding(
            padding: EdgeInsets.only(left: 25, right: 25, top: 10, bottom: 10),
            child: Text(
              "Are you sure you want to delete this user?",
              textAlign: TextAlign.center,
            ),
          )),
          Row(
            children: [
              Expanded(
                  child: InkWell(
                onTap: () {
                  Navigator.of(context).pop(false); // Return false if canceled
                },
                child: Container(
                    padding: const EdgeInsets.all(10),
                    decoration: const BoxDecoration(
                      border: Border(
                        right: BorderSide(
                          color: ColorsManager.grayBorder,
                          width: 0.5,
                        ),
                        top: BorderSide(
                          color: ColorsManager.grayBorder,
                          width: 1,
                        ),
                      ),
                    ),
                    child: const Center(child: Text('Cancel'))),
              )),
              Expanded(
                  child: InkWell(
                onTap: isLoading
                    ? null
                    : () async {
                        setState(() {
                          isLoading = true;
                        });

                        try {
                          if (widget.onTapDelete != null) {
                            await widget.onTapDelete!();
                          }
                        } finally {
                          if (!_isDisposed) {
                            setState(() {
                              isLoading = false;
                            });
                          }
                        }
                        Navigator.of(context).pop(true);
                      },
                child: Container(
                    padding: const EdgeInsets.all(10),
                    decoration: const BoxDecoration(
                      border: Border(
                        left: BorderSide(
                          color: ColorsManager.grayBorder,
                          width: 0.5,
                        ),
                        top: BorderSide(
                          color: ColorsManager.grayBorder,
                          width: 1,
                        ),
                      ),
                    ),
                    child: Center(
                        child: isLoading
                            ? const SizedBox(
                                height: 20,
                                width: 20,
                                child: CircularProgressIndicator(
                                  color: ColorsManager.red,
                                  strokeWidth: 2.0,
                                ),
                              )
                            : const Text(
                                'Delete',
                                style: TextStyle(
                                  color: ColorsManager.red,
                                ),
                              ))),
              )),
            ],
          )
        ],
      ),
    ));
  }
}
