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 'package:doctor_app_flutter/models/patient/patiant_info_model.dart';
import 'package:doctor_app_flutter/util/translations_delegate_base.dart';
import 'package:flutter/cupertino.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<PendingReferral> get patientReferral =>
      _referralPatientService.patientReferralList;

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

  Future getPatientReferral(PatiantInformtion patient) async {
    setState(ViewState.Busy);
    await _referralPatientService.getPatientReferral(patient);
    if (_referralPatientService.hasError) {
      error = _referralPatientService.error;
      setState(ViewState.Error);
    } else {
      if (patientReferral.length == 0) {
        await getMasterLookup(MasterKeysService.physiotherapyGoals);
      } else {
        setState(ViewState.Idle);
      }
    }
  }

  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.BusyLocal);
    await _referralPatientService.getReferralFacilities();
    if (_referralPatientService.hasError) {
      error = _referralPatientService.error;
      setState(ViewState.Error);
    } else {
      setState(ViewState.Idle);
    }
  }

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

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

  Future<dynamic> getDoctorBranch() async {
    DoctorProfileModel doctorProfile = await getDoctorProfile();
    if (doctorProfile != null) {
      dynamic _selectedBranch = {
        "facilityId": doctorProfile.projectID,
        "facilityName": 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 responseReferral(
      PendingReferral pendingReferral, bool isAccepted) async {
    setState(ViewState.Busy);
    await _referralPatientService.responseReferral(pendingReferral, isAccepted);
    if (_referralPatientService.hasError) {
      error = _referralPatientService.error;
      setState(ViewState.ErrorLocal);
    } else
      setState(ViewState.Idle);
  }

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

  Future getPatientDetails(
      String fromDate, String toDate, int patientMrn, int appointmentNo) async {
    setState(ViewState.Busy);

    await _referralPatientService.getPatientArrivalList(toDate,
        fromDate: fromDate,
        patientMrn: patientMrn,
        appointmentNo: appointmentNo);
    if (_referralPatientService.hasError) {
      error = _referralPatientService.error;
      setState(ViewState.Error);
    } else {
      setState(ViewState.Idle);
    }
  }

  /*
  * model
          .getPatientArrivalList()*/

  String getReferralStatusNameByCode(int statusCode, BuildContext context) {
    switch (statusCode) {
      case 1:
        return TranslationBase.of(context).referralStatusHold;
      case 2:
        return TranslationBase.of(context).referralStatusActive;
      case 4:
        return TranslationBase.of(context).referralStatusCancelled;
      case 46:
        return TranslationBase.of(context).referralStatusCompleted;
      case 63:
        return TranslationBase.of(context).referralStatusNotSeen;
      default:
        return "-";
    }
  }
}
