import 'package:flutter/material.dart';

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

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


class InvitationFormFields extends StatelessWidget {
  final List<RoleDescription> roles;
  final TextEditingController emailController;
  final TextEditingController firstNameController;
  final TextEditingController lastNameController;
  final TextEditingController messageController;
  final bool canCreateRoles;
  final VoidCallback onCreateRole;
  final String? selectedRoleRef;
  final ValueChanged<String?> onRoleChanged;

  const InvitationFormFields({
    super.key,
    required this.roles,
    required this.emailController,
    required this.firstNameController,
    required this.lastNameController,
    required this.messageController,
    required this.canCreateRoles,
    required this.onCreateRole,
    required this.selectedRoleRef,
    required this.onRoleChanged,
  });

  @override
  Widget build(BuildContext context) {
    final loc = AppLocalizations.of(context)!;
    return Column(
      children: [
        Wrap(
          spacing: 12,
          runSpacing: 12,
          children: [
            SizedBox(
              width: 320,
              child: TextFormField(
                controller: emailController,
                decoration: InputDecoration(
                  labelText: loc.invitationEmailLabel,
                  prefixIcon: const Icon(Icons.alternate_email_outlined),
                ),
                keyboardType: TextInputType.emailAddress,
                validator: (value) => (value == null || value.trim().isEmpty)
                  ? loc.errorEmailMissing
                  : null,
              ),
            ),
            SizedBox(
              width: 200,
              child: TextFormField(
                controller: firstNameController,
                decoration: InputDecoration(
                  labelText: loc.firstName,
                  prefixIcon: const Icon(Icons.person_outline),
                ),
              ),
            ),
            SizedBox(
              width: 200,
              child: TextFormField(
                controller: lastNameController,
                decoration: InputDecoration(
                  labelText: loc.lastName,
                  prefixIcon: const Icon(Icons.person_outline),
                ),
              ),
            ),
            SizedBox(
              width: 260,
              child: DropdownButtonFormField<String>(
                initialValue: selectedRoleRef,
                items: roles.map((role) => DropdownMenuItem(
                  value: role.storable.id,
                  child: Text(role.describable.name),
                )).toList(),
                onChanged: roles.isEmpty ? null : onRoleChanged,
                decoration: InputDecoration(
                  labelText: loc.invitationRoleLabel,
                  prefixIcon: const Icon(Icons.security_outlined),
                  suffixIcon: IconButton(
                    onPressed: canCreateRoles ? onCreateRole : null,
                    icon: const Icon(Icons.add_circle_outline),
                    tooltip: loc.invitationAddRoleButton,
                  ),
                ),
              ),
            ),
          ],
        ),
        const SizedBox(height: 12),
        TextFormField(
          controller: messageController,
          minLines: 2,
          maxLines: 3,
          decoration: InputDecoration(
            labelText: loc.invitationMessageLabel,
            prefixIcon: const Icon(Icons.chat_bubble_outline),
          ),
        ),
      ],
    );
  }
}
