import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:gap/gap.dart';
import 'package:tanami_app/core/styles/app_text.dart';
import 'package:tanami_app/core/utils/constant/country_flag_data.dart';

import '../../../../shared/components/form_label_textfield.dart';
import '../../../countrySelection/presentation/bloc/choose_country_bloc.dart';
import '../../../countrySelection/presentation/bloc/choose_country_state.dart';
import '../bloc/register_bloc.dart';

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

  @override
  Widget build(BuildContext context) {
    final loginBloc = context.read<RegisterBloc>();
    int selectedCountry = -1;
    return BlocConsumer<RadioBloc, RadioState>(listener: (context, state) {
      if (state is RadioSelectionChanged) {
        selectedCountry = state.selectedIndex;
        loginBloc.countrySelectionTextField.text = countryName[selectedCountry];
      }
    }, builder: (context, state) {
      if (state is RadioSelectionChanged) {
        selectedCountry = state.selectedIndex;
      } else {
        selectedCountry = -1;
      }
      return Form(
        key: loginBloc.formKey,
        child: Padding(
          padding: const EdgeInsets.symmetric(
            horizontal: 14,
          ),
          child: Align(
            alignment: Alignment.topLeft,
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                const Gap(50),
                FormLabelTextField(
                  prefixWidget: selectedCountry == -1
                      ? null
                      : Image.asset(
                          countryFlag[selectedCountry],
                          width: 20,
                          height: 20,
                        ),
                  hintText: AppText.chooseCountry,
                  title: AppText.countryOfResidence,
                  type: "country selection",
                  textEditingController: loginBloc.countrySelectionTextField,
                ),
                const Gap(20),
                FormLabelTextField(
                  prefixWidget: selectedCountry == -1
                      ? null
                      : Image.asset(
                          countryFlag[selectedCountry],
                          width: 20,
                          height: 20,
                        ),
                  hintText: "+0 (000) 000 00 00",
                  title: AppText.phoneNumber,
                  type: "phone number",
                  textEditingController: loginBloc.phoneNumberTextField,
                ),
              ],
            ),
          ),
        ),
      );
    });
  }
}
