import 'package:flutter/foundation.dart' show ChangeNotifier;
import 'package:flutter/material.dart';
import 'package:hmg_patient_app_new/core/app_state.dart' show AppState;
import 'package:hmg_patient_app_new/core/utils/utils.dart';
import 'package:hmg_patient_app_new/features/my_appointments/models/facility_selection.dart';
import 'package:hmg_patient_app_new/features/my_appointments/models/resp_models/doctor_list_api_response.dart';
import 'package:hmg_patient_app_new/features/my_appointments/models/resp_models/hospital_model.dart';
import 'package:hmg_patient_app_new/presentation/book_appointment/dental_chief_complaints_page.dart';
import 'package:hmg_patient_app_new/presentation/book_appointment/laser/laser_appointment.dart';
import 'package:hmg_patient_app_new/presentation/book_appointment/select_clinic_page.dart';
import 'package:hmg_patient_app_new/presentation/book_appointment/select_doctor_page.dart';
import 'package:hmg_patient_app_new/services/navigation_service.dart';
import 'package:hmg_patient_app_new/widgets/routes/custom_page_route.dart';

enum AppointmentViaRegionState {
  REGION_SELECTION,
  TYPE_SELECTION,
  HOSPITAL_SELECTION,
  CLINIC_SELECTION,
  DOCTOR_SELECTION
}

enum RegionBottomSheetType{
  FOR_REGION,
  REGION_FOR_DENTAL_AND_LASER,
  FOR_CLINIIC,
}

class AppointmentViaRegionViewmodel extends ChangeNotifier {
  String? selectedRegionId;
  String? selectedFacilityType;
  PatientDoctorAppointmentList? selectedHospital;
  final NavigationService navigationService;
  AppointmentViaRegionState bottomSheetState =
      AppointmentViaRegionState.REGION_SELECTION;
  final AppState appState;
  TextEditingController searchController = TextEditingController();
  List<PatientDoctorAppointmentList>? hospitalList;
  List<PatientDoctorAppointmentList>? hmgHospitalList;
  List<PatientDoctorAppointmentList>? hmcHospitalList;
  List<PatientDoctorAppointmentList>? displayList;
  FacilitySelection selectedFacility = FacilitySelection.ALL;
  int hmgCount = 0;
  int hmcCount = 0;
  RegionBottomSheetType regionBottomSheetType = RegionBottomSheetType.FOR_REGION;
  bool sortByLocation = false;

  AppointmentViaRegionViewmodel({required this.navigationService,required this.appState});

  void initSortByLocation() {
    sortByLocation = (appState.userLat != 0.0) && (appState.userLong != 0.0);
    notifyListeners();
  }

  void setSortByLocation(bool value) {
    sortByLocation = value;
    notifyListeners();
  }

  void setSelectedRegionId(String? regionId) {
    selectedRegionId = regionId;
    notifyListeners();
  }

  void setDisplayListAndRegionHospitalList(PatientDoctorAppointmentListByRegion? registeredDoctorMap){
    if(registeredDoctorMap == null) {
      return;
    }
    selectedFacility = FacilitySelection.ALL;
    hmcHospitalList = [];
    hmgHospitalList = [];
    hospitalList = [];
    displayList = [];
    for(var data in registeredDoctorMap.hmgDoctorList!){
      hmgHospitalList?.add(data);

    }

    for(var data in registeredDoctorMap.hmcDoctorList!){
      hmcHospitalList?.add(data);

    }

    hospitalList!.addAll(hmgHospitalList!);
    hospitalList!.addAll(hmcHospitalList!);

    hmcCount = registeredDoctorMap.hmcSize;
    hmgCount = registeredDoctorMap.hmgSize;

    hospitalList!.sort((a, b) => num.parse(a.distanceInKMs!).compareTo(num.parse(b.distanceInKMs!)));

    getDisplayList();

  }

  void setFacility(String? facility) {
    selectedFacilityType = facility;
    notifyListeners();
  }

  void setBottomSheetType(RegionBottomSheetType type) {
    regionBottomSheetType = type;
    notifyListeners();
  }

  void setBottomSheetState(AppointmentViaRegionState state) {
    bottomSheetState = state;
    notifyListeners();
  }

  void handleLastStepForRegion(){
    navigationService.pop();
    navigationService.push(CustomPageRoute(
      page: SelectClinicPage(isFromRegionFlow: true,),
    ),);
  }

  void handleBackPress() {
    switch (bottomSheetState) {
      case AppointmentViaRegionState.REGION_SELECTION:
        break;
      case AppointmentViaRegionState.TYPE_SELECTION:
        setBottomSheetState(AppointmentViaRegionState.REGION_SELECTION);
        setSelectedRegionId(null);
        break;
      case AppointmentViaRegionState.HOSPITAL_SELECTION:
        setBottomSheetState(AppointmentViaRegionState.REGION_SELECTION);
        break;
      default:
    }
  }

  void flush() {
    setSelectedRegionId(null);
    setFacility(null);
    setBottomSheetType(RegionBottomSheetType.FOR_REGION);
    setBottomSheetState(AppointmentViaRegionState.REGION_SELECTION);
    sortByLocation = false;
  }

  void setHospitalModel(PatientDoctorAppointmentList? hospital) {
    selectedHospital = hospital;
  }

  bool get isArabic => appState.isArabic();

  void handleLastStepForClinic() {
    navigationService.pop();
    navigationService.push(CustomPageRoute(
      page: SelectDoctorPage(),
    ),);
  }

  void handleLastStepForClinicForDentalAndLaser(int clinicId) {
    navigationService.pop();

    if (clinicId == 17) {
      if (!appState.isAuthenticated) {
        navigationService.push(
          CustomPageRoute(
            page: DentalChiefComplaintsPage(),
          ),
        );
      }
    }
    if (clinicId == 253) {
      {
        navigationService.push(
          CustomPageRoute(
            page: LaserAppointment(),
          ),
        );
      }
    }
  }

  void handleLastStepForDentalAndLaser() {
    //todo_section handle the routing here
    navigationService.pop();
    navigationService.push(
      CustomPageRoute(
        page: SelectDoctorPage(),
      ),
    );
  }

  searchHospitals(String query) {
    if (query.isEmpty) {
      getDisplayList();
      return;
    }
    List<PatientDoctorAppointmentList>? sourceList;
    switch (selectedFacility) {
      case FacilitySelection.ALL:
        sourceList = hospitalList;
        break;
      case FacilitySelection.HMG:
        sourceList = hmgHospitalList;
        break;
      case FacilitySelection.HMC:
        sourceList = hmcHospitalList;
        break;
    }
    displayList = sourceList?.where((hospital) => hospital.filterName != null && hospital.filterName!.toLowerCase().contains(query.toLowerCase())).toList();
    notifyListeners();
  }

  getDisplayList() {
    switch (selectedFacility) {
      case FacilitySelection.ALL:
        displayList = hospitalList;
        break;
      case FacilitySelection.HMG:
        displayList = hmgHospitalList;
        break;
      case FacilitySelection.HMC:
        displayList = hmcHospitalList;
        break;
    }
    notifyListeners();
  }


  setSelectedFacility(FacilitySelection selection) {
    selectedFacility = selection;
    notifyListeners();
  }


}
