import 'package:doctor_app_flutter/config/shared_pref_kay.dart';
import 'package:doctor_app_flutter/core/enum/master_lookup_key.dart';
import 'package:doctor_app_flutter/core/enum/viewstate.dart';
import 'package:doctor_app_flutter/core/service/patient-doctor-referral-service.dart';
import 'package:doctor_app_flutter/core/viewModel/base_view_model.dart';
import 'package:doctor_app_flutter/models/doctor/doctor_profile_model.dart';
import 'package:doctor_app_flutter/models/patient/PatientArrivalEntity.dart';
import 'package:doctor_app_flutter/models/patient/my_referral/PendingReferral.dart';
import 'package:doctor_app_flutter/models/patient/my_referral/my_referred_patient_model.dart';

import '../../locator.dart';

class PatientReferralViewModel extends BaseViewModel {
  PatientReferralService _referralPatientService =
      locator<PatientReferralService>();

  List<dynamic> get branchesList => _referralPatientService.projectsList;

  List<dynamic> get clinicsList => _referralPatientService.clinicsList;

  List<dynamic> get doctorsList => _referralPatientService.doctorsList;

  List<MyReferredPatientModel> get listMyReferredPatientModel =>
      _referralPatientService.listMyReferredPatientModel;

  List<PendingReferral> get pendingReferral =>
      _referralPatientService.pendingReferralList;

  List<PatientArrivalEntity> get patientArrivalList =>
      _referralPatientService.patientArrivalList;

  Future getMasterLookup(MasterKeysService masterKeys) async {
    setState(ViewState.Busy);
    await _referralPatientService.getMasterLookup(masterKeys);
    if (_referralPatientService.hasError) {
      error = _referralPatientService.error;
      setState(ViewState.Error);
    } else
    await getBranches();
  }

  Future getBranches() async {
    setState(ViewState.Busy);
    await _referralPatientService.getProjectsList();
    if (_referralPatientService.hasError) {
      error = _referralPatientService.error;
      setState(ViewState.Error);
    } else {
      setState(ViewState.Idle);
    }
  }

  Future getClinics(int projectId) async {
    setState(ViewState.Busy);
    await _referralPatientService.getClinicsList(projectId);
    await _referralPatientService.getProjectInfo(projectId);
    if (_referralPatientService.hasError) {
      error = _referralPatientService.error;
      setState(ViewState.Error);
    } else
      setState(ViewState.Idle);
  }

  Future getClinicDoctors(String clinicId) async {
    setState(ViewState.Busy);
    await _referralPatientService.getDoctorsList(clinicId);
    if (_referralPatientService.hasError) {
      error = _referralPatientService.error;
      setState(ViewState.Error);
    } else
      setState(ViewState.Idle);
  }

  Future<dynamic> getDoctorBranch() async {
    DoctorProfileModel doctorProfile = await getDoctorProfile();
    if (doctorProfile != null) {
      dynamic _selectedBranch = {
        "ID": doctorProfile.projectID,
        "Desciption": doctorProfile.projectName
      };
      return _selectedBranch;
    }
    return null;
  }

  Future getMyReferredPatient() async {
    setState(ViewState.Busy);
    await _referralPatientService.getMyReferredPatient();
    if (_referralPatientService.hasError) {
      error = _referralPatientService.error;
      setState(ViewState.Error);
    } else
      setState(ViewState.Idle);
  }

  MyReferredPatientModel getReferredPatientItem(int index) {
    return listMyReferredPatientModel[index];
  }

  Future getPendingReferralPatients() async {
    setState(ViewState.Busy);
    await _referralPatientService.getPendingReferralList();
    if (_referralPatientService.hasError) {
      error = _referralPatientService.error;
      setState(ViewState.Error);
    } else
      setState(ViewState.Idle);
  }

  Future getPatientArrivalList(String date) async {
    setState(ViewState.Busy);
    await _referralPatientService.getPatientArrivalList(date);
    if (_referralPatientService.hasError) {
      error = _referralPatientService.error;
      setState(ViewState.Error);
    } else {
      setState(ViewState.Idle);
    }
  }

  List<dynamic> getAppointmentsByPatientName(String patientName) {
    List<dynamic> appointments = [];
    patientArrivalList.forEach((element) {
      if (element.patientName == patientName) {
        appointments.add(element.toJson());
      }
    });
    return appointments;
  }

  Future makeReferral(PatientArrivalEntity patientArrivalEntity,
      String isoStringDate,
      int projectID,
      int clinicID,
      int doctorID,
      String remarks) async {
    setState(ViewState.Busy);
    await _referralPatientService.makeReferral(patientArrivalEntity, isoStringDate, projectID, clinicID, doctorID, remarks);
    if (_referralPatientService.hasError) {
      error = _referralPatientService.error;
      setState(ViewState.Error);
    } else {
      setState(ViewState.Idle);
    }
  }
}
