import 'package:flutter/material.dart';

import 'package:provider/provider.dart';

import 'package:pweb/providers/account_name.dart';


class AccountNameText extends StatelessWidget {
  const AccountNameText({
    super.key,
    required this.hintText,
    required this.lastNameHint,
    required this.inputWidth,
    required this.borderWidth,
  });

  final String hintText;
  final String lastNameHint;
  final double inputWidth;
  final double borderWidth;

  @override
  Widget build(BuildContext context) {
    final state = context.watch<AccountNameState>();
    final theme = Theme.of(context);

    if (state.isEditing) {
      return SizedBox(
        width: inputWidth,
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            TextFormField(
              controller: state.firstNameController,
              style: theme.textTheme.headlineMedium?.copyWith(
                fontWeight: FontWeight.bold,
              ),
              autofocus: true,
              enabled: !state.isBusy,
              decoration: InputDecoration(
                hintText: hintText,
                labelText: hintText,
                isDense: true,
                border: UnderlineInputBorder(
                  borderSide: BorderSide(
                    color: theme.colorScheme.primary,
                    width: borderWidth,
                  ),
                ),
              ),
            ),
            const SizedBox(height: 8),
            TextFormField(
              controller: state.lastNameController,
              style: theme.textTheme.headlineSmall?.copyWith(
                fontWeight: FontWeight.w600,
              ),
              enabled: !state.isBusy,
              decoration: InputDecoration(
                hintText: lastNameHint,
                labelText: lastNameHint,
                isDense: true,
                border: UnderlineInputBorder(
                  borderSide: BorderSide(
                    color: theme.colorScheme.primary,
                    width: borderWidth,
                  ),
                ),
              ),
            ),
          ],
        ),
      );
    }

    final displayName = state.currentFullName.isNotEmpty ? state.currentFullName : hintText;
    return Text(
      displayName,
      style: theme.textTheme.headlineMedium?.copyWith(
        fontWeight: FontWeight.bold,
      ),
    );
  }
}
