import 'package:flutter/material.dart';
import '../halo_tokens.dart';
import 'halo_button.dart';

class HaloPopupAction {
  const HaloPopupAction({required this.label, required this.onPressed});
  final String label;
  final VoidCallback onPressed;
}

class HaloPopup {
  const HaloPopup._();

  static Future<T?> show<T>(
    BuildContext context, {
    required String title,
    String? body,
    Widget? icon,
    HaloPopupAction? primary,
    HaloPopupAction? secondary,
    bool barrierDismissible = true,
  }) {
    return showDialog<T>(
      context: context,
      barrierDismissible: barrierDismissible,
      barrierColor: const Color(0x66000000),
      builder: (ctx) => Dialog(
        backgroundColor: HaloTokens.surface,
        elevation: 0,
        shape: const RoundedRectangleBorder(borderRadius: HaloRadius.xl),
        insetPadding: const EdgeInsets.symmetric(horizontal: HaloSpacing.s24),
        child: Padding(
          padding: const EdgeInsets.all(HaloSpacing.s24),
          child: Column(
            mainAxisSize: MainAxisSize.min,
            crossAxisAlignment: CrossAxisAlignment.stretch,
            children: [
              if (icon != null) ...[
                Center(child: icon),
                const SizedBox(height: HaloSpacing.s16),
              ],
              Text(
                title,
                style: const TextStyle(
                  fontFamily: HaloTokens.fontDisplay,
                  fontSize: 22,
                  height: 28 / 22,
                  fontWeight: FontWeight.w700,
                  color: HaloTokens.ink,
                ),
                textAlign: TextAlign.center,
              ),
              if (body != null) ...[
                const SizedBox(height: HaloSpacing.s12),
                Text(
                  body,
                  style: const TextStyle(
                    fontFamily: HaloTokens.fontBody,
                    fontSize: 15,
                    height: 22 / 15,
                    color: HaloTokens.inkSoft,
                  ),
                  textAlign: TextAlign.center,
                ),
              ],
              const SizedBox(height: HaloSpacing.s24),
              if (primary != null)
                HaloButton(
                  label: primary.label,
                  fullWidth: true,
                  onPressed: () {
                    Navigator.of(ctx).pop();
                    primary.onPressed();
                  },
                ),
              if (secondary != null) ...[
                const SizedBox(height: HaloSpacing.s8),
                HaloButton(
                  label: secondary.label,
                  variant: HaloButtonVariant.ghost,
                  fullWidth: true,
                  onPressed: () {
                    Navigator.of(ctx).pop();
                    secondary.onPressed();
                  },
                ),
              ],
            ],
          ),
        ),
      ),
    );
  }
}
