import 'package:comwell_key_app/common/components/comwell_error_widget.dart';
import 'package:comwell_key_app/presentation/screens/pregistration/prereg_request_model.dart' show IdType;
import 'package:comwell_key_app/common/components/comwell_text_field.dart';
import 'package:comwell_key_app/presentation/screens/pregistration/cubit/preregistration_cubit.dart';
import 'package:comwell_key_app/presentation/screens/pregistration/cubit/preregistration_state.dart';
import 'package:comwell_key_app/presentation/screens/profile_settings/components/date_time_picker.dart';
import 'package:comwell_key_app/themes/light_theme.dart';
import 'package:comwell_key_app/utils/l10n_utils.dart';
import 'package:country_code_picker/country_code_picker.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:comwell_key_app/common/components/shimmer_loader/prereg_profile_shimmer_loader.dart';
import 'package:comwell_key_app/presentation/screens/profile_settings/components/intl_phone_field.dart';
class PreregProfilePage extends StatelessWidget {
const PreregProfilePage({super.key});
@override
Widget build(BuildContext context) {
final theme = Theme.of(context);
return BlocBuilder<PreregistrationCubit, PreregistrationState>(
builder: (context, state) {
if (state.isLoading) {
return const Center(child: PreregProfileShimmerLoader());
}
if (state.user != null) {
return _buildProfilePage(theme, state, context);
}
return Center(
child: ComwellErrorWidget(
title: context.strings.generic_error_title,
subtitle: context.strings.generic_error,
border: true,
),
);
},
);
}
Widget _buildProfilePage(ThemeData theme, PreregistrationState state, BuildContext context) {
final cubit = context.read<PreregistrationCubit>();
final firstNameErrorMessage = !cubit.isFirstNameValid && state.missingInformation
? context.strings.generic_information_required
: null;
final lastNameErrorMessage = !cubit.isLastNameValid && state.missingInformation
? context.strings.generic_information_required
: null;
final phoneNumberErrorMessage = !cubit.isPhoneNumberValid && state.missingInformation
? context.strings.generic_information_required
: null;
final birthDateErrorMessage = !cubit.isBirthDateValid
? context.strings.preregistration_birthdate_error_message
: null;
final documentNumberErrorMessage = !cubit.isDocumentNumberValid && state.missingInformation
? context.strings.generic_information_required
: null;
return ListView(
children: [
Padding(
padding: const EdgeInsets.symmetric(horizontal: 12.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const SizedBox(height: 36),
Text(
context.strings.preregistration_profile_title,
style: theme.textTheme.headlineLarge,
),
const SizedBox(height: 18),
Text(
context.strings.preregistration_profile_subtitle,
style: theme.textTheme.bodySmall,
),
const SizedBox(height: 40),
ComwellTextField(
key: const Key("firstName"),
fieldName: context.strings.profile_settings_firstname,
initialValue: state.user!.firstName,
readOnly: false,
controller: cubit.firstNameTextController,
errorMessage: firstNameErrorMessage,
onChanged: (value) {
cubit.firstNameTextController.text = value;
},
),
const SizedBox(height: 8),
ComwellTextField(
key: const Key("lastName"),
fieldName: context.strings.profile_settings_lastname,
initialValue: state.user!.lastName,
readOnly: false,
controller: cubit.lastNameTextController,
errorMessage: lastNameErrorMessage,
onChanged: (value) {
cubit.lastNameTextController.text = value;
},
),
const SizedBox(height: 8),
DateTimePicker(
title: context.strings.profile_settings_birthday,
initialValue: state.user!.birthDate!,
onDateSelected: (date) async {
await cubit.onBirthDateSelected(date);
},
errorMessage: birthDateErrorMessage,
),
const SizedBox(height: 8),
ComwellTextField(
key: const Key("email"),
fieldName: context.strings.profile_settings_email,
initialValue: state.user!.email,
readOnly: true,
controller: cubit.emailTextController,
),
const SizedBox(height: 8),
IntlPhoneField(
key: const Key("phone"),
title: context.strings.profile_settings_phone,
phoneNumber: cubit.phoneNumber!,
countryCode: cubit.countryCode!,
controller: cubit.phoneNumberTextController,
readOnly: false,
errorMessage: phoneNumberErrorMessage,
onCountryCodeSelected: (CountryCode countryCode) {
cubit.onCountryCodeSelected(countryCode);
},
onPhoneNumberChanged: (String phoneNumber) {
cubit.onPhoneNumberChanged(phoneNumber);
},
onSubmitted: (_) {
// NO OP
},
),
const SizedBox(height: 12),
Container(
height: 62,
width: double.infinity,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(8),
border: Border.all(color: colorDivider),
),
child: CountryCodePicker(
alignLeft: true,
textOverflow: TextOverflow.visible,
initialSelection: cubit.selectedNationality,
showFlag: false,
favorite: cubit.favoriteCountries,
showDropDownButton: true,
textStyle: theme.textTheme.headlineSmall,
showCountryOnly: true,
showOnlyCountryWhenClosed: true,
onChanged: (CountryCode countryCode) {
cubit.onNationalitySelected(countryCode.code ?? '');
},
),
),
if (!cubit.isFavoriteCountry) ...[
const SizedBox(height: 12),
Container(
height: 62,
width: double.infinity,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(8),
border: Border.all(color: colorDivider),
),
child: Padding(
padding: const EdgeInsets.symmetric(vertical: 10, horizontal: 12),
child: DropdownButtonFormField<IdType>(
hint: Text(context.strings.document_type),
decoration: const InputDecoration(border: InputBorder.none),
initialValue: state.selectedDocumentType,
style: theme.textTheme.headlineSmall?.copyWith(color: Colors.black),
items: IdType.values
.map(
(idType) => DropdownMenuItem<IdType>(
value: idType,
child: Text(idType.displayLabel(context.strings)),
),
)
.toList(),
onChanged: (value) {
if (value != null) {
cubit.onDocumentTypeSelected(value);
}
},
),
),
),
const SizedBox(height: 12),
ComwellTextField(
key: const Key("document_number"),
fieldName: context.strings.document_number,
textInputType: TextInputType.number,
initialValue: "",
readOnly: false,
errorMessage: documentNumberErrorMessage,
controller: cubit.documentNumberTextController,
),
],
const SizedBox(height: 50),
],
),
),
],
);
}
}