import 'package:doctor_app_flutter/core/enum/viewstate.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/models/dashboard/dashboard_model.dart';
import 'package:doctor_app_flutter/models/dashboard/get_special_clinical_care_List_Respose_Model.dart';
import 'package:doctor_app_flutter/models/doctor/clinic_model.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
import '../../locator.dart';
import 'authentication_view_model.dart';
import 'base_view_model.dart';


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

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

  bool get hasVirtualClinic => _dashboardService.hasVirtualClinic;

  String get sServiceID => _dashboardService.sServiceID;

  int get notRepliedCount => _doctorReplyService.notRepliedCount;

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

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

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

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

    setFirebaseNotification(authProvider);
  }

  Future setFirebaseNotification(AuthenticationViewModel authProvider) async {
    _firebaseMessaging.requestNotificationPermissions(
        const IosNotificationSettings(
            sound: true, badge: true, alert: true, provisional: true));
    _firebaseMessaging.onIosSettingsRegistered
        .listen((IosNotificationSettings settings) {
      print("Settings registered: $settings");
    });

    _firebaseMessaging.getToken().then((String token) async {
      if (token != '') {
        // DEVICE_TOKEN = token;
        authProvider.insertDeviceImei(token);
      }
    });
  }

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

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

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

  Future changeClinic(
      int 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;
    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);
    }
  }
}
