import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart';
import 'package:hmg_patient_app_new/core/utils/size_utils.dart';
import 'package:hmg_patient_app_new/extensions/widget_extensions.dart';
import 'package:hmg_patient_app_new/generated/locale_keys.g.dart';
import 'package:hmg_patient_app_new/theme/colors.dart';
import 'package:hmg_patient_app_new/widgets/buttons/custom_button.dart';
import 'package:hmg_patient_app_new/widgets/common_bottom_sheet.dart';
import 'package:hmg_patient_app_new/widgets/radio_list_tile_widget.dart';

void showRefundReasonBottomSheet(
  BuildContext context, {
  required String? selectedReason,
  required ValueChanged<String> onDone,
}) {
  showCommonBottomSheetWithoutHeight(
    context,
    title: LocaleKeys.selectRefundReason.tr(context: context),
    isDismissible: true,
    child: _RefundReasonSheetContent(
      selectedReason: selectedReason,
      onDone: onDone,
    ),
    callBackFunc: () {},
  );
}

class _RefundReasonSheetContent extends StatefulWidget {
  final String? selectedReason;
  final ValueChanged<String> onDone;

  const _RefundReasonSheetContent({
    required this.selectedReason,
    required this.onDone,
  });

  @override
  State<_RefundReasonSheetContent> createState() => _RefundReasonSheetContentState();
}

class _RefundReasonSheetContentState extends State<_RefundReasonSheetContent> {
  String? _selected;

  static List<String> _reasons(BuildContext context) => [
        LocaleKeys.refundReason1.tr(context: context),
        LocaleKeys.refundReason2.tr(context: context),
        LocaleKeys.refundReason3.tr(context: context),
        LocaleKeys.refundReason4.tr(context: context),
        LocaleKeys.refundReason5.tr(context: context),
        LocaleKeys.refundReason6.tr(context: context),
        LocaleKeys.refundReason7.tr(context: context),
      ];

  @override
  void initState() {
    super.initState();
    _selected = widget.selectedReason;
  }

  @override
  Widget build(BuildContext context) {
    final reasons = _reasons(context);

    return Column(
      mainAxisSize: MainAxisSize.min,
      children: [
        Container(
          decoration: RoundedRectangleBorder().toSmoothCornerDecoration(
            color: AppColors.whiteColor,
            borderRadius: 24.r,
            hasShadow: false,
          ),
          child: ListView.separated(
            padding: EdgeInsets.zero,
            shrinkWrap: true,
            physics: const NeverScrollableScrollPhysics(),
            itemCount: reasons.length,
            separatorBuilder: (_, __) => Divider(
              color: AppColors.bottomNAVBorder,
              height: 1,
              thickness: 1,
            ).paddingSymmetrical(16.w, 0.w),
            itemBuilder: (context, index) {
              final reason = reasons[index];
              return RadioListTileWidget<String>(
                value: reason,
                groupValue: _selected,
                title: reason,
                onChanged: (value) {
                  if (value != null) setState(() => _selected = value);
                },
              );
            },
          ),
        ),
        SizedBox(height: 16.h),
        CustomButton(
          text: LocaleKeys.done.tr(context: context),
          isDisabled: _selected == null,
          onPressed: () {
            if (_selected != null) {
              widget.onDone(_selected!);
              Navigator.of(context).pop();
            }
          },
        ).paddingSymmetrical(0, 0),
        SizedBox(height: 8.h),
      ],
    );
  }
}
