import 'package:diplomaticquarterapp/core/enum/viewstate.dart';
import 'package:diplomaticquarterapp/core/model/insurance/insurance_approval.dart';
import 'package:diplomaticquarterapp/core/model/insurance/insurance_card.dart';
import 'package:diplomaticquarterapp/core/model/insurance/insurance_card_update.dart';
import 'package:diplomaticquarterapp/core/service/insurance_service.dart';
import 'package:diplomaticquarterapp/models/FamilyFiles/GetAllSharedRecordByStatusResponse.dart';

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

class InsuranceViewModel extends BaseViewModel {
  bool hasError = false;

  InsuranceCardService _insuranceCardService = locator<InsuranceCardService>();

  List<InsuranceCardModel> get insurance => _insuranceCardService.cardList;

  List<InsuranceUpdateModel> get insuranceUpdate =>
      _insuranceCardService.updatedCard;

  List<InsuranceApprovalModel> get insuranceApproval =>
      _insuranceCardService.insuranceApproval;

  GetAllSharedRecordsByStatusResponse get getAllSharedRecordsByStatusResponse =>
      _insuranceCardService.getAllSharedRecordsByStatusResponse;

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

  Future getInsuranceUpdated() async {
    hasError = false;
    //_insuranceCardService.clearInsuranceCard();
    setState(ViewState.Busy);
    await _insuranceCardService.getInsuranceUpdate();
    if (_insuranceCardService.hasError) {
      error = _insuranceCardService.error;
      setState(ViewState.ErrorLocal);
    } else
      getFamilyFiles();
  }

  Future getInsuranceApproval({int appointmentNo}) async {
    hasError = false;
    //_insuranceCardService.clearInsuranceCard();
    setState(ViewState.Busy);
    if (appointmentNo != null)
      await _insuranceCardService.getInsuranceApproval(
          appointmentNo: appointmentNo);
    else
      await _insuranceCardService.getInsuranceApproval();
    if (_insuranceCardService.hasError) {
      error = _insuranceCardService.error;
      setState(ViewState.ErrorLocal);
    } else
      setState(ViewState.Idle);
  }

  Future getFamilyFiles() async {
    await _insuranceCardService.getFamilyFiles();
    if (_insuranceCardService.hasError) {
      error = _insuranceCardService.error;
      setState(ViewState.Error);
    } else
      setState(ViewState.Idle);
  }
}
