import 'package:diplomaticquarterapp/config/config.dart';
import 'package:diplomaticquarterapp/config/shared_pref_kay.dart';
import 'package:diplomaticquarterapp/core/model/insurance/Insurance_card_details.dart';
import 'package:diplomaticquarterapp/core/model/insurance/ReauestInsuranceApprovalModel.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_model.dart';
import 'package:diplomaticquarterapp/core/service/base_service.dart';
import 'package:diplomaticquarterapp/models/FamilyFiles/GetAllSharedRecordByStatusResponse.dart';
import 'package:diplomaticquarterapp/models/FamilyFiles/GetAllSharedRecordsByStatusReq.dart';
import 'package:diplomaticquarterapp/services/family_files/family_files_provider.dart';
import 'package:diplomaticquarterapp/uitl/app_toast.dart';

class InsuranceCardService extends BaseService {
  List<InsuranceCardModel> _cardList = List();
  List<InsuranceUpdateModel> _cardUpdated = List();
  List<InsuranceApprovalModel> _insuranceApproval = List();

  List<InsuranceCardModel> get cardList => _cardList;

  List<InsuranceUpdateModel> get updatedCard => _cardUpdated;

  List<InsuranceApprovalModel> get insuranceApproval => _insuranceApproval;

  InsuranceCardDetailsModel insuranceCardDetails;
  List<InsuranceCardDetailsModel> insuranceCardDetailsList = List();
  bool isHaveInsuranceCard = false;

  GetAllSharedRecordsByStatusResponse getAllSharedRecordsByStatusResponse = GetAllSharedRecordsByStatusResponse();

  clearInsuranceCard() {
    _cardList.clear();
  }

  clearUpdatedList() {
    _cardUpdated.clear();
  }

  RequestInsuranceApprovalModel _requestInsuranceApprovalModel = RequestInsuranceApprovalModel(isDentalAllowedBackend: false, patientTypeID: 1, patientType: 1, eXuldAPPNO: 0, projectID: 0);

  Future getInsurance() async {
    hasError = false;
    _cardList.clear();
    await baseAppClient.post(GET_PAtIENTS_INSURANCE, onSuccess: (dynamic response, int statusCode) {
      response['List_PatientInsuranceCard'].forEach((item) {
        _cardList.add(InsuranceCardModel.fromJson(item));
      });
    }, onFailure: (String error, int statusCode) {
      hasError = true;
      super.error = error;
    }, body: Map());
  }

  Future getInsuranceUpdate() async {
    hasError = false;
    _cardList.clear();
    await baseAppClient.post(GET_PAtIENTS_INSURANCE_UPDATED, onSuccess: (dynamic response, int statusCode) {
      _cardUpdated.clear();
      response['List_PatientInsuranceCardHistory'].forEach((item) {
        _cardUpdated.add(InsuranceUpdateModel.fromJson(item));
      });
    }, onFailure: (String error, int statusCode) {
      hasError = true;
      super.error = error;
    }, body: Map());
  }

  Future getInsuranceApproval({int appointmentNo}) async {
    hasError = false;
    // _cardList.clear();
    if (appointmentNo != null) {
     // _requestInsuranceApprovalModel.appointmentNo = appointmentNo;
      _requestInsuranceApprovalModel.eXuldAPPNO = null;
      _requestInsuranceApprovalModel.projectID = null;
    } else {
     // _requestInsuranceApprovalModel.appointmentNo = null;
      _requestInsuranceApprovalModel.eXuldAPPNO = 0;
      _requestInsuranceApprovalModel.projectID = 0;
    }

    await baseAppClient.post(GET_PAtIENTS_INSURANCE_APPROVALS, onSuccess: (dynamic response, int statusCode) {
      print(response['HIS_Approval_List'].length);
      _insuranceApproval.clear();
      _insuranceApproval.length = 0;
      response['HIS_Approval_List'].forEach((item) {
        _insuranceApproval.add(InsuranceApprovalModel.fromJson(item));
      });
    }, onFailure: (String error, int statusCode) {
      hasError = true;
      super.error = error;
    }, body: _requestInsuranceApprovalModel.toJson());
  }

  Future getFamilyFiles() async {
    var myFamily = await sharedPref.getObject(FAMILY_FILE);
    if (myFamily != null) {
      getAllSharedRecordsByStatusResponse = GetAllSharedRecordsByStatusResponse.fromJson(myFamily);
    } else {
      getSharedRecordByStatus();
    }
  }

  Future getSharedRecordByStatus() async {
    try {
      dynamic localRes;
      var request = GetAllSharedRecordsByStatusReq();
      request.status = 0;
      await baseAppClient.post(GET_SHARED_RECORD_BY_STATUS, onSuccess: (dynamic response, int statusCode) {
        localRes = response;
      }, onFailure: (String error, int statusCode) {
        AppToast.showErrorToast(message: error);
        throw error;
      }, body: request.toJson());
      sharedPref.setObject(FAMILY_FILE, localRes);
      getAllSharedRecordsByStatusResponse = GetAllSharedRecordsByStatusResponse.fromJson(localRes);
    } catch (error) {
      print(error);
      throw error;
    }
  }

  Future getInsuranceDetails(data) async {
    dynamic localRes;
    await baseAppClient.post(
      INSURANCE_DETAILS,
      onSuccess: (dynamic response, int statusCode) {
        localRes = response['List_InsuranceCheckList'];
      },
      onFailure: (String error, int statusCode) {
        hasError = true;
        super.error = error;
      },
      body: {'CompanyID': data.companyID, 'SubCategoryID': data.subCategoryID},
    );
    return Future.value(localRes);
  }

  Future getPatientInsuranceDetails({String setupID, int projectID, String patientIdentificationID, int patientID}) async {
    error = "";
    hasError = false;
    insuranceCardDetails = null;
    isHaveInsuranceCard = false;
    await baseAppClient.post(
      GET_PATIENT_INSURANCE_DETAILS,
      onSuccess: (dynamic response, int statusCode) {
        insuranceCardDetailsList.clear();
        isHaveInsuranceCard = true;
        response['List_PatientInsuranceDetails'].forEach((item) {
          insuranceCardDetailsList.add(InsuranceCardDetailsModel.fromJson(item));
        });
      },
      onFailure: (String error, int statusCode) {
        hasError = true;
        super.error = error;
      },
      body: {'SetupID': setupID, 'ProjectID': projectID, 'PatientIdentificationID': patientIdentificationID, 'isDentalAllowedBackend': false, 'PatientID': patientID},
    );
  }

  Future uploadInsuranceCard({String patientIdentificationID, int patientID, String image = ""}) async {
    error = "";
    Map<String, dynamic> body = Map();
    body['PatientID'] = patientID;
    body['PatientType'] = user.patientType;
    body['MobileNo'] = user.mobileNumber;
    body['PatientIdentificationID'] = patientIdentificationID;
    body['InsuranceCardImage'] = image;

    await baseAppClient.post(
      UPLOAD_INSURANCE_CARD,
      onSuccess: (dynamic response, int statusCode) {},
      onFailure: (String error, int statusCode) {
        hasError = true;
        super.error = error;
      },
      body: body,
    );
  }
}
