import "package:collection/collection.dart";
import 'package:diplomaticquarterapp/core/enum/filter_type.dart';
import 'package:diplomaticquarterapp/core/enum/viewstate.dart';
import 'package:diplomaticquarterapp/core/model/radiology/final_radiology.dart';
import 'package:diplomaticquarterapp/core/service/medical/radiology_service.dart';
import 'package:diplomaticquarterapp/models/Authentication/authenticated_user.dart';
import 'package:diplomaticquarterapp/uitl/app_toast.dart';

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

class RadiologyViewModel extends BaseViewModel {
  FilterType filterType = FilterType.Clinic;
  RadiologyService _radiologyService = locator<RadiologyService>();

  List<FinalRadiologyList> _finalRadiologyListClinic = List();
  List<FinalRadiologyList> _finalRadiologyListHospital = List();

  List<FinalRadiologyList> get finalRadiologyList => filterType == FilterType.Clinic ? _finalRadiologyListClinic : _finalRadiologyListHospital;

  bool _isRadiologyVIDAPlus = false;

  bool get isRadiologyVIDAPlus => _isRadiologyVIDAPlus;

  void getPatientRadOrders() async {
    setState(ViewState.Busy);
    await _radiologyService.getPatientRadOrders();
    _isRadiologyVIDAPlus = _radiologyService.isRadiologyVIDAPlus;
    if (_radiologyService.hasError) {
      error = _radiologyService.error;
      setState(ViewState.Error);
    } else {
      //Clinic Sorting
      var clinicMap = groupBy(_radiologyService.finalRadiologyList, (FinalRadiology obj) => obj.clinicDescription);
      clinicMap.forEach((key, value) {
        _finalRadiologyListClinic.add(FinalRadiologyList(filterName: key, finalRadiologyList: value.toList()));
      });

      //Hospital Sorting
      var hospitalMap = groupBy(_radiologyService.finalRadiologyList, (FinalRadiology obj) => obj.projectName);
      hospitalMap.forEach((key, value) {
        _finalRadiologyListHospital.add(FinalRadiologyList(filterName: key, finalRadiologyList: value.toList()));
      });

      setState(ViewState.Idle);
    }
  }

  String get radImageURL => _radiologyService.url;

  getRadImageURL({int invoiceNo, String invoiceType, int lineItem, int projectId, bool isVidaPlus}) async {
    setState(ViewState.Busy);
    await _radiologyService.getRadImageURL(invoiceNo: invoiceNo, invoiceType: invoiceType, lineItem: lineItem, projectId: projectId, isVidaPlus: isVidaPlus);
    if (_radiologyService.hasError) {
      error = _radiologyService.error;
      setState(ViewState.Error);
    } else
      setState(ViewState.Idle);
  }

  sendRadReportEmail({FinalRadiology finalRadiology, String mes, AuthenticatedUser userObj}) async {
    setState(ViewState.BusyLocal);
    await _radiologyService.sendRadReportEmail(finalRadiology: finalRadiology, userObj: userObj);
    if (_radiologyService.hasError) {
      error = _radiologyService.error;
      AppToast.showErrorToast(message: error);
    } else {
      AppToast.showSuccessToast(message: mes);
    }
    setState(ViewState.Idle);
  }

  setFilterType(FilterType filterType) {
    this.filterType = filterType;
    notifyListeners();
  }
}
