import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart';
import 'package:get_it/get_it.dart';
import 'package:hmg_patient_app_new/core/app_assets.dart';
import 'package:hmg_patient_app_new/core/utils/size_utils.dart';
import 'package:hmg_patient_app_new/core/utils/utils.dart';
import 'package:hmg_patient_app_new/extensions/string_extensions.dart';
import 'package:hmg_patient_app_new/extensions/widget_extensions.dart';
import 'package:hmg_patient_app_new/features/ask_doctor/models/ask_doctor_appointments_list.dart';
import 'package:hmg_patient_app_new/features/my_appointments/models/resp_models/ask_doctor_request_type_response_model.dart';
import 'package:hmg_patient_app_new/features/my_appointments/models/resp_models/patient_appointment_history_response_model.dart';
import 'package:hmg_patient_app_new/features/my_appointments/my_appointments_view_model.dart';
import 'package:hmg_patient_app_new/generated/locale_keys.g.dart';
import 'package:hmg_patient_app_new/services/navigation_service.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/loader/bottomsheet_loader.dart';

class AskDoctorRequestTypeSelect extends StatelessWidget {
  AskDoctorRequestTypeSelect({super.key, required this.askDoctorRequestTypeList, required this.myAppointmentsViewModel, required this.patientAppointmentHistoryResponseModel});

  final MyAppointmentsViewModel myAppointmentsViewModel;
  final PatientAppointmentHistoryResponseModel patientAppointmentHistoryResponseModel;
  List<AskDocRequestType> askDoctorRequestTypeList = [];
  int selectedParameterCodeValue = 2;
  int selectedParameterCode = 0;

  final ValueNotifier<int> requestTypeSelectNotifier = ValueNotifier<int>(0);

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        Container(
          width: double.infinity,
          decoration: RoundedRectangleBorder().toSmoothCornerDecoration(color: AppColors.whiteColor, borderRadius: 24),
          child: ListView.builder(
            itemCount: askDoctorRequestTypeList.length,
            physics: NeverScrollableScrollPhysics(),
            padding: EdgeInsets.only(top: 8, bottom: 8),
            shrinkWrap: true,
            itemBuilder: (context, index) {
              return ValueListenableBuilder<int>(
                valueListenable: requestTypeSelectNotifier,
                builder: (context, duration, child) {
                  return Theme(
                    data: Theme.of(context).copyWith(
                      listTileTheme: ListTileThemeData(horizontalTitleGap: 4),
                    ),
                    child: RadioListTile<int>(
                      title: (askDoctorRequestTypeList[index].description ?? '').toText14(isBold: true),
                      value: index,
                      fillColor: WidgetStateProperty.resolveWith((states) {
                        if (states.contains(WidgetState.selected)) {
                          return AppColors.primaryRedColor;
                        }
                        return Color(0xffEEEEEE);
                      }),
                      contentPadding: EdgeInsets.only(left: 12.h, right: 12.h),
                      groupValue: selectedParameterCode,
                      onChanged: (int? newValue) {
                        selectedParameterCode = newValue!;
                        selectedParameterCodeValue = askDoctorRequestTypeList[index].parameterCode!;
                        requestTypeSelectNotifier.value = selectedParameterCode;
                        debugPrint(selectedParameterCodeValue.toString());
                      },
                    ),
                  );
                },
              );
            },
          ),
        ),
        SizedBox(height: 16.h),
        Row(
          children: [
            Expanded(
              child: CustomButton(
                text: LocaleKeys.cancel.tr(),
                onPressed: () {
                  Navigator.pop(context);
                },
                backgroundColor: AppColors.secondaryLightRedColor,
                borderColor: AppColors.secondaryLightRedColor,
                textColor: AppColors.primaryRedColor,
                icon: AppAssets.cancel,
                iconColor: AppColors.primaryRedColor,
                borderRadius: 12.r,
                iconSize: 16.5.h,
                fontSize: 16.f,
                height: 56.h,
              ),
            ),
            SizedBox(width: 8.h),
            Expanded(
              child: CustomButton(
                text: LocaleKeys.confirm.tr(),
                onPressed: () async {
                  Navigator.of(context).pop();
                  LoaderBottomSheet.showLoader(loadingText: LocaleKeys.sendingAskDrRequest.tr(context: context));
                  await myAppointmentsViewModel.sendAskDocCallRequest(
                      patientAppointmentHistoryResponseModel: patientAppointmentHistoryResponseModel,
                      requestType: selectedParameterCodeValue.toString(),
                      remarks: "",
                      userMobileNumber: myAppointmentsViewModel.appState.getAuthenticatedUser()!.mobileNumber!,
                      onSuccess: (val) {
                        LoaderBottomSheet.hideLoader();
                        showCommonBottomSheetWithoutHeight(GetIt.instance<NavigationService>().navigatorKey.currentContext!,
                            child: Padding(padding: EdgeInsets.symmetric(horizontal: 24.h), child: Utils.getSuccessWidget(loadingText: LocaleKeys.askDrRequestSentSuccess.tr())), callBackFunc: () {
                          Navigator.of(context).pop();
                        }, title: "", isCloseButtonVisible: false, isDismissible: false, isFullScreen: false, isAutoDismiss: true);
                      },
                      onError: (errMessage) {
                        LoaderBottomSheet.hideLoader();
                      });
                },
                backgroundColor: AppColors.bgGreenColor.withValues(alpha: 0.20),
                borderColor: AppColors.bgGreenColor.withValues(alpha: 0.0),
                textColor: AppColors.bgGreenColor,
                iconColor: AppColors.bgGreenColor,
                icon: AppAssets.confirm,
                iconSize: 16.5.h,
                borderRadius: 12.r,
                fontSize: 16.f,
                height: 56.h,
              ),
            ),
          ],
        )
      ],
    );
  }
}
