import 'package:flutter/material.dart';

import 'package:collection/collection.dart';

import 'package:pshared/models/invitation/invitation.dart';
import 'package:pshared/models/invitation/status.dart';
import 'package:pshared/models/permissions/descriptions/role.dart';

import 'package:pweb/generated/i18n/app_localizations.dart';


class InvitationInfoRow extends StatelessWidget {
  final IconData icon;
  final String label;
  final String value;

  const InvitationInfoRow({
    super.key,
    required this.icon,
    required this.label,
    required this.value,
  });

  @override
  Widget build(BuildContext context) {
    final theme = Theme.of(context);
    return Row(
      mainAxisSize: MainAxisSize.min,
      children: [
        Icon(icon, size: 18, color: theme.hintColor),
        const SizedBox(width: 6),
        if (value.isEmpty)
          Text(label, style: theme.textTheme.bodyMedium)
        else
          RichText(
            text: TextSpan(
              style: theme.textTheme.bodyMedium,
              children: [
                TextSpan(text: '$label: ', style: const TextStyle(fontWeight: FontWeight.w600)),
                TextSpan(text: value),
              ],
            ),
          ),
      ],
    );
  }
}

String invitationStatusLabel(Invitation invitation, AppLocalizations loc) {
  if (invitation.isExpired && invitation.isPending) {
    return loc.invitationStatusExpired;
  }
  switch (invitation.status) {
    case InvitationStatus.created:
    case InvitationStatus.sent:
      return loc.invitationStatusPending;
    case InvitationStatus.accepted:
      return loc.invitationStatusAccepted;
    case InvitationStatus.declined:
      return loc.invitationStatusDeclined;
    case InvitationStatus.revoked:
      return loc.invitationStatusRevoked;
  }
}

Color invitationStatusColor(ThemeData theme, Invitation invitation) {
  if (invitation.isExpired && invitation.isPending) {
    return theme.disabledColor;
  }
  switch (invitation.status) {
    case InvitationStatus.created:
    case InvitationStatus.sent:
      return Colors.amber.shade800;
    case InvitationStatus.accepted:
      return Colors.green.shade700;
    case InvitationStatus.declined:
    case InvitationStatus.revoked:
      return Colors.red.shade700;
  }
}

String invitationRoleLabel(List<RoleDescription> roles, Invitation invitation, AppLocalizations loc) {
  final role = roles.firstWhereOrNull((r) => r.storable.id == invitation.roleRef);
  return role?.describable.name ?? loc.invitationUnknownRole;
}
