import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

import '../l10n/l10n.dart';
import 'api_exception.dart';

const _safeForbiddenMessages = {
  'Du kan inte omkategorisera, ta bort eller ändra produkter som är globala.',
  'Du kan bara omkategorisera dina egna produkter',
};

String mapErrorToUserMessage(Object error, BuildContext context) {
  final l10n = context.l10n;
  if (error is ApiException) {
    switch (error.type) {
      case ApiErrorType.unauthorized:
        return l10n.sessionExpiredError;
      case ApiErrorType.forbidden:
        return _safeForbiddenMessages.contains(error.message)
            ? error.message
            : l10n.forbiddenError;
      case ApiErrorType.server:
        return l10n.serverError;
      case ApiErrorType.network:
        return l10n.networkError;
      case ApiErrorType.unknown:
        return error.message.isNotEmpty ? error.message : l10n.unexpectedError;
    }
  }
  return l10n.unexpectedError;
}

SnackBar buildCopyableErrorSnackBar(BuildContext context, String message) {
  return SnackBar(
    content: Text(message),
    action: SnackBarAction(
      label: context.l10n.errorDialogCopy,
      onPressed: () {
        Clipboard.setData(ClipboardData(text: message));
        ScaffoldMessenger.of(context).showSnackBar(
          SnackBar(content: Text(context.l10n.errorDialogCopied)),
        );
      },
    ),
  );
}

Widget buildCopyableErrorPanel({
  required BuildContext context,
  required String message,
  required VoidCallback onRetry,
  String title = 'Ett fel uppstod',
}) {
  return Center(
    child: ConstrainedBox(
      constraints: const BoxConstraints(maxWidth: 720),
      child: Card(
        margin: const EdgeInsets.all(16),
        child: Padding(
          padding: const EdgeInsets.all(16),
          child: Column(
            mainAxisSize: MainAxisSize.min,
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Text(
                title,
                style: const TextStyle(fontSize: 18, fontWeight: FontWeight.w600),
              ),
              const SizedBox(height: 8),
              SelectableText(message),
              const SizedBox(height: 12),
              Wrap(
                spacing: 8,
                runSpacing: 8,
                children: [
                  FilledButton.icon(
                    onPressed: onRetry,
                    icon: const Icon(Icons.refresh),
                    label: Text(context.l10n.retryAction),
                  ),
                  OutlinedButton.icon(
                    onPressed: () {
                      Clipboard.setData(ClipboardData(text: message));
                      ScaffoldMessenger.of(context).showSnackBar(
                        SnackBar(content: Text(context.l10n.errorDialogCopied)),
                      );
                    },
                    icon: const Icon(Icons.copy_all),
                    label: Text(context.l10n.errorDialogCopy),
                  ),
                ],
              ),
            ],
          ),
        ),
      ),
    ),
  );
}
