import 'package:doctor_app_flutter/core/enum/filter_type.dart';
import 'package:doctor_app_flutter/core/enum/view_state.dart';
import 'package:doctor_app_flutter/core/model/patient/patiant_info_model.dart';
import 'package:doctor_app_flutter/core/model/radiology/final_radiology.dart';
import 'package:doctor_app_flutter/core/service/patient_medical_file/radiology/radiology_service.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<FinalRadiologyList> _finalRadiologyListHospital = [];

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

  void getPatientRadOrders(PatiantInformtion patient, {isInPatient = false}) async {
    setState(ViewState.Busy);
    await _radiologyService.getPatientRadOrders(patient, isInPatient: isInPatient);
    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({required int invoiceNo, required int lineItem, required int projectId, required bool isVidaPlus, required PatiantInformtion patient, required String invoiceType}) async {
    setState(ViewState.Busy);
    await _radiologyService.getRadImageURL(invoiceNo: invoiceNo, lineItem: lineItem, projectId: projectId, isVidaPlus: isVidaPlus, patient: patient, invoiceType: invoiceType);
    if (_radiologyService.hasError) {
      error = _radiologyService.error;
      setState(ViewState.Error);
    } else
      setState(ViewState.Idle);
  }

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