import 'package:doctor_app_flutter/core/enum/view_state.dart';
import 'package:doctor_app_flutter/core/model/dashboard/radiology_critical_finding_model.dart';
import 'package:doctor_app_flutter/core/service/home/dasboard_service.dart';
import 'package:doctor_app_flutter/core/service/home/doctor_reply_service.dart';
import 'package:doctor_app_flutter/core/service/special_clinics/special_clinic_service.dart';
import 'package:doctor_app_flutter/core/viewModel/project_view_model.dart';
import 'package:doctor_app_flutter/screens/pharmacy_intervention/viewmodel/pharmacy_intervention_service.dart';
import 'package:firebase_messaging/firebase_messaging.dart';

import '../../locator.dart';
import '../model/dashboard/dashboard_model.dart';
import '../model/dashboard/get_special_clinical_care_List_Respose_Model.dart';
import '../model/doctor/clinic_model.dart';
import 'authentication_view_model.dart';
import 'base_view_model.dart';

class DashboardViewModel extends BaseViewModel {
  final FirebaseMessaging _firebaseMessaging = FirebaseMessaging.instance;
  DashboardService _dashboardService = locator<DashboardService>();
  PharmacyInterventionService _service = locator<PharmacyInterventionService>();
  SpecialClinicsService _specialClinicsService =
      locator<SpecialClinicsService>();
  DoctorReplyService _doctorReplyService = locator<DoctorReplyService>();

  List<DashboardModel> get dashboardItemsList =>
      _dashboardService.dashboardItemsList;

  RadiologyCriticalFindingModel? get radiologyCriticalFindingModel =>
      _dashboardService.radiologyCriticalFindingModel;

  bool get hasVirtualClinic => _dashboardService.hasVirtualClinic;

  String? get sServiceID => _dashboardService.sServiceID;

  int get notRepliedCount => _doctorReplyService.notRepliedCount;

  bool isInfectiousDiseaseConsultant = false;

  bool isInfectiousDiseasePending = false;

  List<GetSpecialClinicalCareListResponseModel> get specialClinicalCareList =>
      _specialClinicsService.specialClinicalCareList;

  Future getInfectiousDiseaseConsultantStatus() async {
    bool result = await _service.getInfectiousDiseaseConsultantStatus();
    if (_service.hasError) {
      error = _service.error;
      return;
    }
    isInfectiousDiseaseConsultant = result;
    print('the result is ${result}');
  }

  Future getPendingInfectiousDiseaseStatus() async {
    bool result = await _service.getPendingInterventionDiseaseStatus();
    if (_service.hasError) {
      error = _service.error;
      return;
    }
    isInfectiousDiseasePending = result;
    print('the result is ${result}');
  }

  Future startHomeScreenServices(ProjectViewModel projectsProvider,
      AuthenticationViewModel authProvider) async {
    setState(ViewState.Busy);
    await getDoctorProfile(isGetProfile: true);

    final results = await Future.wait([
      getInfectiousDiseaseConsultantStatus(),
      getPendingInfectiousDiseaseStatus(),
      projectsProvider.getDoctorClinicsList(),
      _dashboardService.getDashboard(),
      _dashboardService.checkDoctorHasLiveCare(),
      _specialClinicsService.getSpecialClinicalCareList(),
      _dashboardService.checkDoctorHasRadiologyFindings(),
    ]);

    if (_dashboardService.hasError) {
      error = _dashboardService.error;
      setState(ViewState.ErrorLocal);
    } else
      setState(ViewState.Idle);

    setFirebaseNotification(authProvider);
  }

  Future setFirebaseNotification(AuthenticationViewModel authProvider) async {
    _firebaseMessaging.requestPermission(
        sound: true, badge: true, alert: true, provisional: true);

    _firebaseMessaging.getToken().then((String? token) async {
      if (token != '') {
        // DEVICE_TOKEN = token;
        ///TODO Elham* return it back
        authProvider.insertDeviceImei(token);
      }
    });
  }

  Future getDashboard() async {
    setState(ViewState.Busy);
    await _dashboardService.getDashboard();
  }

  Future checkDoctorHasLiveCare() async {
    setState(ViewState.Busy);
    await _dashboardService.checkDoctorHasLiveCare();
  }

  Future acknowledgeRadiologyCriticalFinding(
      String invoiceNo, String invoiceLineItemNo) async {
    await _dashboardService.acknowledgeDoctorHasRadiologyFindings(
        invoiceNo, invoiceLineItemNo);
  }

  Future getSpecialClinicalCareList() async {
    setState(ViewState.Busy);
    await _specialClinicsService.getSpecialClinicalCareList();
    // if (_specialClinicsService.hasError) {
    //   error = _specialClinicsService.error;
    //   setState(ViewState.Error);
    // } else
    //   setState(ViewState.Idle);
  }

  Future changeClinic(
      dynamic clinicId, AuthenticationViewModel authProvider) async {
    setState(ViewState.BusyLocal);
    await getDoctorProfile();
    ClinicModel clinicModel = ClinicModel(
      doctorID: doctorProfile!.doctorID,
      clinicID: clinicId,
      projectID: doctorProfile!.projectID,
    );
    await authProvider.getDoctorProfileBasedOnClinic(clinicModel);
    if (authProvider.state == ViewState.ErrorLocal) {
      error = authProvider.error;
    }
  }

  getPatientCount(DashboardModel inPatientCount) {
    int value = 0;
    inPatientCount.summaryoptions!
        .forEach((result) => {value += result.value!});

    return value.toString();
  }

  GetSpecialClinicalCareListResponseModel getSpecialClinic(clinicId) {
    GetSpecialClinicalCareListResponseModel special =
        GetSpecialClinicalCareListResponseModel();
    specialClinicalCareList.forEach((element) {
      if (element.clinicID == clinicId) {
        special = element;
      }
    });

    return special;
  }

  Future getNotRepliedCount() async {
    setState(ViewState.BusyLocal);
    await getDoctorProfile();
    await _doctorReplyService.getNotRepliedCount();
    if (_doctorReplyService.hasError) {
      error = _doctorReplyService.error;
      setState(ViewState.ErrorLocal);
    } else {
      notifyListeners();
      setState(ViewState.Idle);
    }
  }
}
