import 'package:flutter/material.dart';
import 'package:syncrow_web/pages/common/buttons/cancel_button.dart';
import 'package:syncrow_web/utils/color_manager.dart';

void showDeleteConfirmationDialog(BuildContext context, VoidCallback onConfirm, bool isSpace) {
  final String title = isSpace ? 'Delete Space' : 'Delete Community';
  final String subtitle = isSpace
      ? 'All the data in the space will be lost'
      : 'All the data in the community will be lost';

  showDialog(
    context: context,
    barrierDismissible: false,
    builder: (BuildContext context) {
      return Dialog(
        shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(16.0)),
        child: SizedBox(
          width: 500,
          child: Container(
            color: ColorsManager.whiteColors,
            padding: const EdgeInsets.all(20.0),
            child: Column(
              mainAxisSize: MainAxisSize.min,
              children: [
                _buildWarningIcon(),
                const SizedBox(height: 20),
                _buildDialogTitle(context, title),
                const SizedBox(height: 10),
                _buildDialogSubtitle(context, subtitle),
                const SizedBox(height: 20),
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                  children: [
                    CancelButton(
                      label: 'Cancel',
                      onPressed: () => Navigator.of(context).pop(),
                    ),
                    ElevatedButton(
                      onPressed: () {
                        Navigator.of(context).pop(); // Close the first dialog
                        showProcessingPopup(context, isSpace, onConfirm);
                      },
                      style: _dialogButtonStyle(ColorsManager.spaceColor),
                      child: const Text('Continue', style: TextStyle(color: ColorsManager.whiteColors)),
                    ),
                  ],
                ),
              ],
            ),
          ),
        ),
      );
    },
  );
}

void showProcessingPopup(BuildContext context, bool isSpace, VoidCallback onDelete) {
  final String title = isSpace ? 'Delete Space' : 'Delete Community';

  showDialog(
    context: context,
    barrierDismissible: false,
    builder: (BuildContext context) {
      return Dialog(
        shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(16.0)),
        child: SizedBox(
          width: 500,
          child: Container(
            color: ColorsManager.whiteColors,
            padding: const EdgeInsets.all(20.0),
            child: Column(
              mainAxisSize: MainAxisSize.min,
              children: [
                _buildWarningIcon(),
                const SizedBox(height: 20),
                _buildDialogTitle(context, title),
                const SizedBox(height: 10),
                _buildDialogSubtitle(context, 'Are you sure you want to delete?'),
                const SizedBox(height: 20),
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                  children: [
                    ElevatedButton(
                      onPressed: onDelete,
                      style: _dialogButtonStyle(ColorsManager.warningRed),
                      child: const Text('Delete', style: TextStyle(color: ColorsManager.whiteColors)),
                    ),
                    CancelButton(
                      label: 'Cancel',
                      onPressed: () => Navigator.of(context).pop(),
                    ),
                  ],
                ),
              ],
            ),
          ),
        ),
      );
    },
  );
}

Widget _buildWarningIcon() {
  return Container(
    width: 50,
    height: 50,
    decoration: BoxDecoration(
      color: ColorsManager.warningRed,
      shape: BoxShape.circle,
    ),
    child: const Icon(Icons.close, color: ColorsManager.whiteColors, size: 40),
  );
}

Widget _buildDialogTitle(BuildContext context, String title) {
  return Text(
    title,
    style: Theme.of(context).textTheme.headlineMedium,
  );
}

Widget _buildDialogSubtitle(BuildContext context, String subtitle) {
  return Text(
    subtitle,
    textAlign: TextAlign.center,
    style: Theme.of(context).textTheme.bodyMedium?.copyWith(color: ColorsManager.grayColor),
  );
}

ButtonStyle _dialogButtonStyle(Color color) {
  return ElevatedButton.styleFrom(
    backgroundColor: color,
    shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(8.0)),
    fixedSize: const Size(140, 40),
  );
}
