import 'package:doctor_app_flutter/core/enum/viewstate.dart';
import 'package:doctor_app_flutter/core/model/procedure/categories_procedure.dart';
import 'package:doctor_app_flutter/core/model/procedure/get_ordered_procedure_model.dart';
import 'package:doctor_app_flutter/core/model/procedure/get_procedure_model.dart';
import 'package:doctor_app_flutter/core/model/procedure/post_procedure_req_model.dart';
import 'package:doctor_app_flutter/core/model/procedure/procedure_valadate_model.dart';
import 'package:doctor_app_flutter/core/model/procedure/procedure_valadate_request_model.dart';
import 'package:doctor_app_flutter/core/model/procedure/update_procedure_request_model.dart';
import 'package:doctor_app_flutter/core/service/procedure_service.dart';
import 'package:doctor_app_flutter/core/viewModel/base_view_model.dart';
import 'package:doctor_app_flutter/locator.dart';

class ProcedureViewModel extends BaseViewModel {
  bool hasError = false;
  ProcedureService _procedureService = locator<ProcedureService>();
  List<GetOrderedProcedureModel> get procedureList =>
      _procedureService.procedureList;
  List<ProcedureValadteModel> get valadteProcedureList =>
      _procedureService.valadteProcedureList;
  List<CategoriseProcedureModel> get categoriesList =>
      _procedureService.categoriesList;
  List<dynamic> get categoryList => _procedureService.categoryList;

  Future getProcedure({int mrn}) async {
    hasError = false;
    //_insuranceCardService.clearInsuranceCard();
    setState(ViewState.Busy);
    await _procedureService.getProcedure(mrn: mrn);
    if (_procedureService.hasError) {
      error = _procedureService.error;
      setState(ViewState.ErrorLocal);
    } else
      setState(ViewState.Idle);
  }

  Future getProcedureCategory({String categoryName, String categoryID}) async {
    hasError = false;
    //_insuranceCardService.clearInsuranceCard();
    setState(ViewState.Busy);
    await _procedureService.getProcedureCategory(
        categoryName: categoryName, categoryID: categoryID);
    if (_procedureService.hasError) {
      error = _procedureService.error;
      setState(ViewState.ErrorLocal);
    } else
      setState(ViewState.Idle);
  }

  Future getCategory() async {
    hasError = false;
    //_insuranceCardService.clearInsuranceCard();
    setState(ViewState.Busy);
    await _procedureService.getCategory();
    if (_procedureService.hasError) {
      error = _procedureService.error;
      setState(ViewState.ErrorLocal);
    } else
      setState(ViewState.Idle);
  }

  Future postProcedure(
      PostProcedureReqModel postProcedureReqModel, int mrn) async {
    hasError = false;
    //_insuranceCardService.clearInsuranceCard();
    setState(ViewState.Busy);
    await _procedureService.postProcedure(postProcedureReqModel);
    if (_procedureService.hasError) {
      error = _procedureService.error;
      setState(ViewState.ErrorLocal);
    } else {
      await getProcedure(mrn: mrn);
      setState(ViewState.Idle);
    }
  }

  Future valadteProcedure(
      ProcedureValadteRequestModel procedureValadteRequestModel) async {
    hasError = false;
    //_insuranceCardService.clearInsuranceCard();
    setState(ViewState.Busy);
    await _procedureService.valadteProcedure(procedureValadteRequestModel);
    if (_procedureService.hasError) {
      error = _procedureService.error;
      setState(ViewState.ErrorLocal);
    } else {
      setState(ViewState.Idle);
    }
  }

  Future updateProcedure(
      {UpdateProcedureRequestModel updateProcedureRequestModel,
      int mrn}) async {
    hasError = false;
    //_insuranceCardService.clearInsuranceCard();
    setState(ViewState.Busy);
    await _procedureService.updateProcedure(updateProcedureRequestModel);
    if (_procedureService.hasError) {
      error = _procedureService.error;
      setState(ViewState.ErrorLocal);
    } else
      setState(ViewState.Idle);
    //await getProcedure(mrn: mrn);
  }
}
