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/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;

  void getPatientRadOrders() async {
    setState(ViewState.Busy);
    await _radiologyService.getPatientRadOrders();
    if (_radiologyService.hasError) {
      error = _radiologyService.error;
      setState(ViewState.Error);
    } else {
      _radiologyService.finalRadiologyList.forEach((element) {
        List<FinalRadiologyList> finalRadiologyListClinic =
            _finalRadiologyListClinic
                .where((elementClinic) =>
                    elementClinic.filterName == element.clinicDescription)
                .toList();

        if (finalRadiologyListClinic.length != 0) {
          _finalRadiologyListClinic[
                  finalRadiologyListClinic.indexOf(finalRadiologyListClinic[0])]
              .finalRadiologyList
              .add(element);
        } else {
          _finalRadiologyListClinic.add(FinalRadiologyList(
              filterName: element.clinicDescription, finalRadiology: element));
        }

        //   FinalRadiologyList list sort via project
        List<FinalRadiologyList> finalRadiologyListHospital =
            _finalRadiologyListHospital
                .where(
                  (elementClinic) =>
                      elementClinic.filterName == element.projectName,
                )
                .toList();

        if (finalRadiologyListHospital.length != 0) {
          _finalRadiologyListHospital[finalRadiologyListHospital
                  .indexOf(finalRadiologyListHospital[0])]
              .finalRadiologyList
              .add(element);
        } else {
          _finalRadiologyListHospital.add(FinalRadiologyList(
              filterName: element.projectName, finalRadiology: element));
        }
      });

      setState(ViewState.Idle);
    }
  }

  String get radImageURL => _radiologyService.url;

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

  sendRadReportEmail(
      {FinalRadiology finalRadiology,String mes}) async {
    setState(ViewState.BusyLocal);
    await _radiologyService.sendRadReportEmail(
     finalRadiology: finalRadiology
    );
    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();
  }
}
