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 String? selectedRoleRef;
  final ValueChanged<String?> onRoleChanged;
  static const double _fieldWidth = 250.0;
  static const double _rowSpacing = 40.0;
  static const double _rowGap = 12.0;

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

  @override
  Widget build(BuildContext context) {
    final loc = AppLocalizations.of(context)!;
    return Column(
      children: [
        Row(
          spacing: _rowSpacing,
          children: [
            SizedBox(
              width: _fieldWidth,
              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: _fieldWidth,
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  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),
                    ),
                  ),
                ],
              ),
            ),
          ],
        ),
        SizedBox(height: _rowGap),
        Row(
          spacing: _rowSpacing,
          children: [
            SizedBox(
              width: _fieldWidth,
              child: TextFormField(
                controller: lastNameController,
                decoration: InputDecoration(
                  labelText: loc.lastName,
                  prefixIcon: const Icon(Icons.person_outline),
                ),
              ),
            ),
            SizedBox(
              width: _fieldWidth,
              child: TextFormField(
                controller: firstNameController,
                decoration: InputDecoration(
                  labelText: loc.firstName,
                  prefixIcon: const Icon(Icons.person_outline),
                ),
              ),
            ),
          ],
        ),
        const SizedBox(height: _rowGap),
        TextFormField(
          controller: messageController,
          minLines: 2,
          maxLines: 3,
          decoration: InputDecoration(
            labelText: loc.invitationMessageLabel,
            prefixIcon: const Icon(Icons.chat_bubble_outline),
          ),
        ),
      ],
    );
  }
}
