import 'package:flutter/material.dart';

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


class UsernameField extends StatelessWidget {
  final TextEditingController controller;
  final ValueChanged<bool>? onValid;

  const UsernameField({
    super.key,
    required this.controller,
    this.onValid,
  });

  String? _reportResult(String? msg) {
    onValid?.call(msg == null);
    return msg;
  }

  @override
  Widget build(BuildContext context) => TextFormField(
    controller: controller,
    decoration: InputDecoration(
      labelText: AppLocalizations.of(context)!.username,
      hintText: AppLocalizations.of(context)!.usernameHint,
    ),
    validator: (value) {
      return _reportResult((value?.isNotEmpty ?? false) ? null : AppLocalizations.of(context)!.usernameErrorInvalid);
      // bool isValid = value != null && EmailValidator.validate(value);
      // if (!isValid) {
      //   return _reportResult(AppLocalizations.of(context)!.usernameErrorInvalid);
      // }
      // final tld = value.split('.').last;
      // isValid = tlds.contains(tld);
      // if (!isValid) {
      //   return _reportResult(AppLocalizations.of(context)!.usernameUnknownTLD(tld));
      // }
      // return _reportResult(null);
    },
    onChanged: (value) => onValid?.call(value.isNotEmpty),
  );
}
