import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:gap/gap.dart';
import 'package:tanami_app/core/styles/app_color.dart';
import 'package:tanami_app/core/utils/constant/country_flag_data.dart';
import 'package:tanami_app/shared/components/text_widget.dart';

import '../bloc/choose_country_bloc.dart';
import '../bloc/choose_country_event.dart';
import '../bloc/choose_country_state.dart';

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

  @override
  Widget build(BuildContext context) {
    final radioBloc = context.read<RadioBloc>();
    return BlocBuilder<RadioBloc, RadioState>(
      builder: (context, state) {
        int selectedIndex = -1;
        if (state is RadioSelectionChanged) {
          selectedIndex = state.selectedIndex;
        }

        return Column(
          children: List<Widget>.generate(countryFlag.length, (int index) {
            return ListTile(
              title: Row(
                crossAxisAlignment: CrossAxisAlignment.center,
                children: [
                  Image.asset(
                    countryFlag[index],
                    width: 24,
                    height: 24,
                  ),
                  const Gap(10),
                  TextWidget().text14W500(countryName[index],
                      clr: AppColor.charcoalColor),
                ],
              ),
              leading: Radio<int>(
                activeColor: AppColor.radioActiveColor,
                value: index,
                groupValue: selectedIndex,
                onChanged: (int? value) {
                  if (value != null) {
                    radioBloc.add(RadioSelected(value));
                  }
                },
              ),
            );
          }),
        );
      },
    );
  }
}
