import 'package:diplomaticquarterapp/config/config.dart';
import 'package:diplomaticquarterapp/config/shared_pref_kay.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/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;

  GetAllSharedRecordsByStatusResponse getAllSharedRecordsByStatusResponse =
      GetAllSharedRecordsByStatusResponse();

  clearInsuranceCard() {
    _cardList.clear();
  }

  clearUpdatedList() {
    _cardUpdated.clear();
  }

  InsuranceCardModel _insuranceCardModel = InsuranceCardModel(
    channel: 3,
    deviceTypeID: 2,
    generalid: "Cs2020@2016\$2958",
    iPAdress: "10.20.10.20",
    isDentalAllowedBackend: false,
    languageID: 1,
    patientID: 1231755,
    patientOutSA: 0,
    patientType: 1,
    patientTypeID: 1,
    sessionID: "uoKFXSLUwEaHYPwKZNA",
    tokenID: "@dm!n",
    versionID: 5.5,
  );

  InsuranceUpdateModel _insuranceUpdateModel = InsuranceUpdateModel(
    channel: 3,
    deviceTypeID: 2,
    generalid: "Cs2020@2016\$2958",
    iPAdress: "10.20.10.20",
    isDentalAllowedBackend: false,
    patientID: 1231755,
    patientType: 1,
    versionID: 5.5,
    languageID: 2,
    patientOutSA: 0,
    sessionID: "ENRSJBKXnzCuuVQ",
  );

  InsuranceApprovalModel _insuranceApprovalModel = InsuranceApprovalModel(
      versionID: 5.5,
      channel: 3,
      languageID: LANGUAGE_ID,
      iPAdress: "10.20.10.20",
      generalid: "Cs2020@2016\$2958",
      patientOutSA: 0,
      sessionID: "DypNmtMkivzURHjeYg",
      isDentalAllowedBackend: false,
      deviceTypeID: 2,
      patientID: 1231755,
      tokenID: "@dm!n",
      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: _insuranceCardModel.toJson());
  }

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

  Future getInsuranceApproval({int appointmentNo}) async {
    hasError = false;
    // _cardList.clear();
    if (appointmentNo != null) {
      _insuranceApprovalModel.appointmentNo = appointmentNo;
      _insuranceApprovalModel.eXuldAPPNO = null;
      _insuranceApprovalModel.projectID = null;
    } else {
      _insuranceApprovalModel.appointmentNo = null;
      _insuranceApprovalModel.eXuldAPPNO = 0;
      _insuranceApprovalModel.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: _insuranceApprovalModel.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;
    }
  }
}
