import 'dart:async';

import 'package:flutter/material.dart';
import 'package:hmg_patient_app_new/core/utils/size_utils.dart';
import 'package:hmg_patient_app_new/core/utils/utils.dart' show Utils;
import 'package:hmg_patient_app_new/extensions/widget_extensions.dart';
import 'package:hmg_patient_app_new/features/book_appointments/book_appointments_view_model.dart';
import 'package:hmg_patient_app_new/features/my_appointments/appointment_via_region_viewmodel.dart';
import 'package:hmg_patient_app_new/features/my_appointments/my_appointments_view_model.dart' show MyAppointmentsViewModel;
import 'package:hmg_patient_app_new/presentation/appointments/widgets/region_bottomsheet/region_list_item.dart' show RegionListItem;
import 'package:provider/provider.dart';

class RegionBottomSheetBody extends StatefulWidget {
  const RegionBottomSheetBody({super.key});

  @override
  State<RegionBottomSheetBody> createState() => _RegionBottomSheetBodyState();
}

class _RegionBottomSheetBodyState extends State<RegionBottomSheetBody> {
  late BookAppointmentsViewModel myAppointmentsViewModel;
  late AppointmentViaRegionViewmodel regionalViewModel;

  @override
  void initState() {
    scheduleMicrotask(() {
      if (regionalViewModel.regionBottomSheetType == RegionBottomSheetType.FOR_REGION) {
        myAppointmentsViewModel.getRegionMappedProjectList();
      } else if (regionalViewModel.regionBottomSheetType == RegionBottomSheetType.FOR_CLINIIC) {
        myAppointmentsViewModel.getMappedDoctors();
      }
    });
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    myAppointmentsViewModel = Provider.of<BookAppointmentsViewModel>(context);
    regionalViewModel = Provider.of<AppointmentViaRegionViewmodel>(context);
    return Consumer<BookAppointmentsViewModel>(
      builder: (context, myAppointmentsVM, child) {
        if (myAppointmentsVM.isRegionListLoading) {
          return Container(
            height: MediaQuery.of(context).size.height * 0.3,
            decoration: const BoxDecoration(
              color: Colors.white,
              borderRadius: BorderRadius.vertical(top: Radius.circular(16)),
            ),
            child: Center(
              child: Utils.getLoadingWidget(),
            ),
          );
        } else {
          return SizedBox(
            height: MediaQuery.of(context).size.height * 0.5,
            child: ListView.separated(
              itemCount: myAppointmentsVM.hospitalList?.registeredDoctorMap?.length ?? 0,
              separatorBuilder: (_, __) {
                return SizedBox(
                  height: 16.h,
                );
              },
              itemBuilder: (_, index) {
                String key = myAppointmentsVM.hospitalList?.registeredDoctorMap?.keys.toList()[index] ?? '';
                return RegionListItem(
                  title: key,
                  subTitle: "",
                  hmcCount: "${myAppointmentsVM.hospitalList?.registeredDoctorMap?[key]?.hmcSize ?? 0}",
                  hmgCount: "${myAppointmentsVM.hospitalList?.registeredDoctorMap?[key]?.hmgSize ?? 0}",
                ).onPress(() {
                  regionalViewModel.setSelectedRegionId(key);
                  regionalViewModel.setBottomSheetState(AppointmentViaRegionState.TYPE_SELECTION);
                });
              },
            ),
          );
        }
      },
    );
  }
}
