import 'package:diplomaticquarterapp/pages/feedback/appointment_history.dart';
import 'package:diplomaticquarterapp/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();
  List<Reports> reportsOrderReadyList = List();
  List<Reports> reportsOrderCompletedList = List();
  List<Reports> reportsOrderCanceledList = List();

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

  getReports() async {
    setState(ViewState.Busy);
    reportsOrderRequestList.clear();
    reportsOrderReadyList.clear();
    reportsOrderCompletedList.clear();
    reportsOrderCanceledList.clear();
    await _reportsService.getReports();
    if (_reportsService.hasError) {
      error = _reportsService.error;
      setState(ViewState.Error);
    } else {
      _filterList();
      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 _filterList() {
    _reportsService.reportsList.forEach((report) {
      switch (report.status) {
        case 1:
          reportsOrderRequestList.add(report);
          break;
        case 2:
          reportsOrderReadyList.add(report);
          break;
        case 3:
          reportsOrderCompletedList.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);
       setState(ViewState.Idle);
     }
  }
}
