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/model/DischargeReferralPatient.dart';
import 'package:doctor_app_flutter/core/model/MyReferralPatientModel.dart';
import 'package:doctor_app_flutter/core/service/DischargedPatientService.dart';
import 'package:doctor_app_flutter/core/service/MyReferralPatientService.dart';
import 'package:doctor_app_flutter/core/service/ReferralService.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/my_referral/PendingReferral.dart';
import 'package:doctor_app_flutter/models/patient/my_referral/clinic-doctor.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>();

  ReferralService _referralService = locator<ReferralService>();

  MyReferralInPatientService _myReferralService =
      locator<MyReferralInPatientService>();

  DischargedPatientService _dischargedPatientService = locator<DischargedPatientService>();
  List<DischargeReferralPatient> get myDischargeReferralPatient =>
      _dischargedPatientService.myDischargeReferralPatients;

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

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

  List<dynamic> get referralFrequencyList =>
      _referralPatientService.frequencyList;

  List<dynamic> doctorsList = [];

  List<ClinicDoctor> get clinicDoctorsList =>
      _referralPatientService.doctorsList;

  List<MyReferralPatientModel> get myReferralPatients =>
      _myReferralService.myReferralPatients;

  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(
      PatiantInformtion patient, int clinicId, int branchId) async {
    setState(ViewState.BusyLocal);
    await _referralPatientService.getDoctorsList(patient, clinicId, branchId);
    if (_referralPatientService.hasError) {
      error = _referralPatientService.error;
      setState(ViewState.ErrorLocal);
    } else {
      doctorsList.clear();
      clinicDoctorsList.forEach((element) {
        doctorsList.add(element.toJson());
      });
      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 getMyReferralPatientService() async {
    setState(ViewState.Busy);
    await _myReferralService.getMyReferralPatientService();
    if (_myReferralService.hasError) {
      error = _myReferralService.error;
      setState(ViewState.Error);
    } else
      setState(ViewState.Idle);
  }
  Future replay(String referredDoctorRemarks, MyReferralPatientModel referral) async{
    setState(ViewState.Busy);
    await _myReferralService.replay(referredDoctorRemarks,referral);
    if (_myReferralService.hasError) {
      error = _myReferralService.error;
      setState(ViewState.ErrorLocal);
    } else
      getMyReferralPatientService();
  }

  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 makeInPatientReferral(
      {PatiantInformtion patient,
      int projectID,
      int clinicID,
      int doctorID,
      int frequencyCode,
      int priority,
      String referralDate,
      String remarks,
      String ext}) async {
    setState(ViewState.Busy);
    await _referralService.referralPatient(
      patientID: patient.patientId,
      roomID: patient.roomId,
      referralClinic: clinicID,
      admissionNo: patient.appointmentNo,
      referralDoctor: doctorID,
      patientTypeID: patient.patientType,
      referringDoctorRemarks: remarks,
      priority: priority,
      frequency: frequencyCode,
      extension: ext,
    );
    if (_referralService.hasError) {
      error = _referralService.error;
      setState(ViewState.ErrorLocal);
    } 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);
    if (_referralPatientService.hasError) {
      error = _referralPatientService.error;
      setState(ViewState.Error);
    } else {
      setState(ViewState.Idle);
    }
  }

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

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

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

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

  PatiantInformtion getPatientFromReferral(
      MyReferredPatientModel referredPatient) {
    PatiantInformtion patient = PatiantInformtion();
    patient.doctorId = referredPatient.doctorID;
    patient.doctorName = referredPatient.doctorName;
    patient.patientId = referredPatient.patientID;
    patient.firstName = referredPatient.firstName;
    patient.middleName = referredPatient.middleName;
    patient.lastName = referredPatient.lastName;
    patient.gender = referredPatient.gender;
    patient.dateofBirth = referredPatient.dateofBirth;
    patient.mobileNumber = referredPatient.mobileNumber;
    patient.emailAddress = referredPatient.emailAddress;
    patient.patientIdentificationNo = referredPatient.patientIdentificationNo;
    patient.patientType = referredPatient.patientType;
    patient.admissionNo = referredPatient.admissionNo;
    patient.admissionDate = referredPatient.admissionDate;
    patient.roomId = referredPatient.roomID;
    patient.bedId = referredPatient.bedID;
    patient.nationalityName = referredPatient.nationalityName;
    patient.nationalityFlagURL = referredPatient.nationalityFlagURL;
    patient.age = referredPatient.age;
    patient.clinicDescription = referredPatient.clinicDescription;
    return patient;
  }

  PatiantInformtion getPatientFromReferralO(
      MyReferralPatientModel referredPatient) {
    PatiantInformtion patient = PatiantInformtion();
    patient.doctorId = referredPatient.doctorID;
    patient.doctorName = referredPatient.doctorName;
    patient.patientId = referredPatient.patientID;
    patient.firstName = referredPatient.firstName;
    patient.middleName = referredPatient.middleName;
    patient.lastName = referredPatient.lastName;
    patient.gender = referredPatient.gender;
    patient.dateofBirth = referredPatient.dateofBirth;
    patient.mobileNumber = referredPatient.mobileNumber;
    patient.emailAddress = referredPatient.emailAddress;
    patient.patientIdentificationNo = referredPatient.patientIdentificationNo;
    patient.patientType = referredPatient.patientType;
    patient.admissionNo = referredPatient.admissionNo;
    patient.admissionDate = referredPatient.admissionDate;
    patient.roomId = referredPatient.roomID;
    patient.bedId = referredPatient.bedID;
    patient.nationalityName = referredPatient.nationalityName;
    patient.nationalityFlagURL = referredPatient.nationalityFlagURL;
    patient.age = referredPatient.age;
    patient.clinicDescription = referredPatient.clinicDescription;
    return patient;
  }

  PatiantInformtion getPatientFromDischargeReferralPatient(
      DischargeReferralPatient referredPatient) {
    PatiantInformtion patient = PatiantInformtion();
    patient.doctorId = referredPatient.doctorID;
    patient.doctorName = referredPatient.doctorName;
    patient.patientId = referredPatient.patientID;
    patient.firstName = referredPatient.firstName;
    patient.middleName = referredPatient.middleName;
    patient.lastName = referredPatient.lastName;
    patient.gender = referredPatient.gender;
    patient.dateofBirth = referredPatient.dateofBirth;
    patient.mobileNumber = referredPatient.mobileNumber;
    patient.emailAddress = referredPatient.emailAddress;
    patient.patientIdentificationNo = referredPatient.patientIdentificationNo;
    patient.patientType = referredPatient.patientType;
    patient.admissionNo = referredPatient.admissionNo;
    patient.admissionDate = referredPatient.admissionDate;
    patient.roomId = referredPatient.roomID;
    patient.bedId = referredPatient.bedID;
    patient.nationalityName = referredPatient.nationalityName;
    patient.nationalityFlagURL = '';// TODO from backend referredPatient.nationalityFlagURL;
    patient.age = referredPatient.age;
    patient.clinicDescription = referredPatient.clinicDescription;
    return patient;
  }
}
