import 'package:farm_tpf/custom_model/action_form/CommonData.dart';
import 'package:farm_tpf/data/repository/repository.dart';
import 'package:farm_tpf/presentation/screens/actions/state_management_helper/change_dropdown_controller.dart';
import 'package:farm_tpf/presentation/screens/resources/bloc/bloc/common_data_bloc.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:get/get.dart';
import 'package:group_radio_button/group_radio_button.dart';

class RadioButtonWidget extends StatefulWidget {
  final String condition;
  final String supply;
  final String tag;
  final Function(CommonData) onPressed;
  RadioButtonWidget({
    required this.condition,
    required this.supply,
    required this.onPressed,
    required this.tag,
  });

  @override
  _RadioButtonWidgetState createState() => _RadioButtonWidgetState();
}

class _RadioButtonWidgetState extends State<RadioButtonWidget> {
  var selectedItem = CommonData();
  CommonDataBloc? _commonDataBloc;
  ChangeDropdownController? controller;

  @override
  void initState() {
    super.initState();
    controller = Get.put(ChangeDropdownController(), tag: widget.tag);
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder<ChangeDropdownController>(
        init: controller,
        tag: widget.tag,
        builder: (data) {
          return BlocProvider(
            create: (context) => CommonDataBloc(repository: Repository())..add(DataFetched(tbSupply: widget.supply, condition: widget.condition)),
            child: BlocBuilder<CommonDataBloc, CommonDataState>(
              bloc: _commonDataBloc,
              builder: (contextb, state) {
                if (state is CommonDataFailure) {
                  return Center(child: Text(''));
                }
                if (state is CommonDataSuccess) {
                  if ((state.items ?? []).isEmpty) {
                    return Center(child: Text(''));
                  }
                  List<CommonData> listItem = state.items!.cast<CommonData>();

                  if (data?.currentData?.id != null) {
                    listItem.forEach((element) {
                      if (element.id == (data.currentData?.id ?? -1)) {
                        selectedItem = element;
                      }
                    });
                  }
                  return RadioGroup<CommonData>.builder(
                    groupValue: selectedItem,
                    onChanged: (value) {
                      setState(() {
                        selectedItem = value ?? CommonData();
                        widget.onPressed(value ?? CommonData());
                        controller?.change(value ?? CommonData());
                      });
                    },
                    items: state.items!.cast<CommonData>(),
                    itemBuilder: (item) => RadioButtonBuilder(item.name ?? ''),
                  );
                }
                return Center(child: CircularProgressIndicator());
              },
            ),
          );
        });
  }
}
