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/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_address_shimmer_loader.dart';

class PreregAddressPage extends StatelessWidget {
  const PreregAddressPage({super.key});

  @override
  Widget build(BuildContext context) {
    return BlocBuilder<PreregistrationCubit, PreregistrationState>(
      builder: (context, state) {
        final theme = Theme.of(context);
        final cubit = context.read<PreregistrationCubit>();

        if (state.isLoading) {
          return const Center(child: PreregAddressShimmerLoader());
        }

        final addressErrorMessage = !cubit.isAddressValid && state.missingInformation
            ? context.strings.generic_information_required
            : null;
        final postalCodeErrorMessage = !cubit.isPostalCodeValid && state.missingInformation
            ? context.strings.generic_information_required
            : null;
        final cityErrorMessage = !cubit.isCityValid && state.missingInformation
            ? context.strings.generic_information_required
            : null;

        return ListView(
          padding: const EdgeInsets.symmetric(horizontal: 12.0),
          key: const PageStorageKey("information_form"),
          children: [
            const SizedBox(height: 40),
            Text(
              context.strings.preregistration_address_title,
              style: Theme.of(context).textTheme.headlineLarge,
            ),
            const SizedBox(height: 18),
            Text(
              context.strings.preregistration_address_subtitle,
              style: Theme.of(context).textTheme.bodySmall,
            ),
            const SizedBox(height: 40),
            ComwellTextField(
              key: const Key("address"),
              fieldName: context.strings.preregistration_address_label_address,
              initialValue: "",
              readOnly: false,
              errorMessage: addressErrorMessage,
              controller: cubit.addressTextController,
            ),
            const SizedBox(height: 12),
            ComwellTextField(
              fieldName: context.strings.preregistration_address_label_postal_code,
              initialValue: "",
              textInputType: TextInputType.number,
              errorMessage: postalCodeErrorMessage,
              readOnly: false,
              controller: cubit.postalCodeTextController,
            ),
            const SizedBox(height: 12),
            ComwellTextField(
              fieldName: context.strings.preregistration_address_label_city,
              initialValue: "",
              readOnly: false,
              errorMessage: cityErrorMessage,
              controller: cubit.cityTextController,
            ),
            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.selectedCountry,
                showFlag: false,
                favorite: cubit.favoriteCountries,
                showDropDownButton: true,
                textStyle: theme.textTheme.headlineSmall,
                showCountryOnly: true,
                showOnlyCountryWhenClosed: true,
                onChanged: (CountryCode countryCode) {
                  cubit.onSelectedCountrySelected(countryCode.code ?? '');
                },
              ),
            ),
          ],
        );
      },
    );
  }
}