import 'package:hmg_patient_app/core/model/reports/admission_for_medical_report.dart';
import 'package:hmg_patient_app/pages/feedback/appointment_history.dart';
import 'package:hmg_patient_app/uitl/app_toast.dart';

import '../../../core/enum/reportfilter_type.dart';
import '../../../core/enum/viewstate.dart';
import '../../../core/model/reports/Reports.dart';
import '../../../core/service/medical/reports_service.dart';
import '../../../locator.dart';
import '../base_view_model.dart';

class ReportsViewModel extends BaseViewModel {
  ReportFilterType filterType = ReportFilterType.Requested;

  ReportsService _reportsService = locator<ReportsService>();

  List<Reports> reportsOrderRequestList =[];
  List<Reports> reportsOrderReadyList =[];
  List<Reports> reportsOrderCanceledList =[];

  List<Reports> reportsInPatientOrderRequestList =[];
  List<Reports> reportsInPatientOrderReadyList =[];
  List<Reports> reportsInPatientOrderCanceledList =[];

  List<AppointmentHistory> get appointHistoryList => _reportsService.appointHistoryList;

  List<AdmissionMedicalReport> get admissionsMedicalReportList => _reportsService.admissionsMedicalReport;

  getReports(int languageID) async {
    setState(ViewState.Busy);
    reportsOrderRequestList.clear();
    reportsOrderReadyList.clear();
    reportsOrderCanceledList.clear();
    await _reportsService.getReports();
    if (_reportsService.hasError) {
      error = _reportsService.error!;
      setState(ViewState.Error);
    } else {
      _filterList();
      await _reportsService.getInpatientAdmissionsList(languageID);
      setState(ViewState.Idle);
    }
  }

  getInPatientReports() async {
    setState(ViewState.Busy);
    reportsInPatientOrderRequestList.clear();
    reportsInPatientOrderReadyList.clear();
    reportsInPatientOrderCanceledList.clear();
    await _reportsService.getInPatientReports();
    if (_reportsService.hasError) {
      error = _reportsService.error!;
      setState(ViewState.Error);
    } else {
      _filterInPatientList();
      setState(ViewState.Idle);
    }
  }

  getPatentAppointmentHistory() async {
    setState(ViewState.Busy);
    await _reportsService.getPatentAppointmentHistory();
    if (_reportsService.hasError) {
      error = _reportsService.error!;
      setState(ViewState.Error);
    } else {
      setState(ViewState.Idle);
    }
  }

  void _filterInPatientList() {
    _reportsService.inpatientReportsList.forEach((report) {
      switch (report.status) {
        case 1:
          reportsInPatientOrderRequestList.add(report);
          break;
        case 2:
          reportsInPatientOrderReadyList.add(report);
          break;
        case 4:
          reportsInPatientOrderCanceledList.add(report);
          break;
        default:
      }
    });
  }

  void _filterList() {
    _reportsService.reportsList.forEach((report) {
      switch (report.status) {
        case 1:
          reportsOrderRequestList.add(report);
          break;
        case 2:
          reportsOrderReadyList.add(report);
          break;
        case 4:
          reportsOrderCanceledList.add(report);
          break;
        default:
      }
    });
  }

  insertRequestForMedicalReport(AppointmentHistory appointmentHistory, String mes) async {
    setState(ViewState.Busy);
    await _reportsService.insertRequestForMedicalReport(appointmentHistory);
    if (_reportsService.hasError) {
      error = _reportsService.error!;
      AppToast.showErrorToast(message: error);
      setState(ViewState.ErrorLocal);
    } else {
      AppToast.showSuccessToast(message: mes);
      getInPatientReports();
      setState(ViewState.Idle);
    }
  }

  insertRequestForInPatientMedicalReport(int clinicID, int doctorID, String setupID, int admissionNo, int projectID, String mes) async {
    setState(ViewState.Busy);
    await _reportsService.insertRequestForInPatientMedicalReport(clinicID, doctorID, setupID, admissionNo, projectID);
    if (_reportsService.hasError) {
      error = _reportsService.error!;
      AppToast.showErrorToast(message: error);
      setState(ViewState.ErrorLocal);
    } else {
      AppToast.showSuccessToast(message: mes);
      setState(ViewState.Idle);
    }
  }

}
