import 'package:flutter/material.dart';
import 'package:hmg_patient_app_new/core/app_state.dart';
import 'package:hmg_patient_app_new/core/dependencies.dart';
import 'package:hmg_patient_app_new/core/location_util.dart';
import 'package:hmg_patient_app_new/core/utils/penguin_method_channel.dart';
import 'package:hmg_patient_app_new/core/utils/utils.dart';
import 'package:hmg_patient_app_new/features/hospital/AppPermission.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/hospital_model.dart';
import 'package:permission_handler/permission_handler.dart';

class HospitalSelectionBottomSheetViewModel extends ChangeNotifier {
  List<HospitalsModel> displayList = [];
  List<HospitalsModel> listOfData = [];
  List<HospitalsModel> hmgHospitalList = [];
  List<HospitalsModel> hmcHospitalList = [];
  FacilitySelection selectedFacility = FacilitySelection.ALL;
  int hmcCount = 0;
  int hmgCount = 0;
  TextEditingController searchController = TextEditingController();
  final AppState appState;
  LocationUtils locationUtils = getIt.get<LocationUtils>();

  HospitalSelectionBottomSheetViewModel(this.appState) {
    locationUtils.getCurrentLocation(onSuccess: (value) {
      Utils.navigationProjectsList.forEach((element) {
        HospitalsModel model = HospitalsModel.fromJson(element);

        double dist = Utils.distance(value.latitude, value.longitude, double.parse(model.latitude!), double.parse(model.longitude!)).ceilToDouble();
        print(dist);
        model.distanceInKilometers = dist;

        if (model.isHMC == true) {
          hmcHospitalList.add(model);
        } else {
          hmgHospitalList.add(model);
        }
        listOfData.add(model);
      });
      hmgCount = hmgHospitalList.length;
      hmcCount = hmcHospitalList.length;

      listOfData.sort((a, b) => a.distanceInKilometers.compareTo(b.distanceInKilometers));
      hmgHospitalList.sort((a, b) => a.distanceInKilometers.compareTo(b.distanceInKilometers));

      getDisplayList();
    });
  }

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

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

  void clearSearchText() {
    searchController.clear();
  }

  void setSelectedFacility(FacilitySelection value) {
    selectedFacility = value;
    getDisplayList();

  }

  void openPenguin(HospitalsModel hospital, {String clinicID = ""}) {
    initPenguinSDK(hospital.iD, clinicID: clinicID);
  }

  initPenguinSDK(int projectID, {String clinicID = ""}) async {
    NavigationClinicDetails data = NavigationClinicDetails();
    data.projectId = projectID.toString();
    data.clinicId = clinicID;
    // data.clinicId = "1";
    final bool permited = await AppPermission.askPenguinPermissions();
    if (!permited) {
      Map<Permission, PermissionStatus> statuses = await [
        Permission.location,
        Permission.bluetooth,
        Permission.bluetoothConnect,
        Permission.bluetoothScan,
        Permission.activityRecognition,
      ].request().whenComplete(() {
        PenguinMethodChannel().launch("penguin", appState.isArabic() ? "ar" : "en", appState.getAuthenticatedUser()?.patientId?.toString() ?? "", false, details: data);
      });
    }
  }


}
