import 'package:doctor_app_flutter/config/config.dart';
import 'package:doctor_app_flutter/models/SOAP/ChiefComplaint/GetChiefComplaintReqModel.dart';
import 'package:doctor_app_flutter/models/SOAP/ChiefComplaint/GetChiefComplaintResModel.dart';
import 'package:doctor_app_flutter/models/SOAP/GeneralGetReqForSOAP.dart';
import 'package:doctor_app_flutter/models/SOAP/GetAllergiesResModel.dart';
import 'package:doctor_app_flutter/models/SOAP/GetAssessmentReqModel.dart';
import 'package:doctor_app_flutter/models/SOAP/GetAssessmentResModel.dart';
import 'package:doctor_app_flutter/models/SOAP/GetGetProgressNoteReqModel.dart';
import 'package:doctor_app_flutter/models/SOAP/GetGetProgressNoteResModel.dart';
import 'package:doctor_app_flutter/models/SOAP/GetHistoryReqModel.dart';
import 'package:doctor_app_flutter/models/SOAP/GetHistoryResModel.dart';
import 'package:doctor_app_flutter/models/SOAP/GetPhysicalExamListResModel.dart';
import 'package:doctor_app_flutter/models/SOAP/GetPhysicalExamReqModel.dart';
import 'package:doctor_app_flutter/models/SOAP/PostEpisodeReqModel.dart';
import 'package:doctor_app_flutter/models/SOAP/get_Allergies_request_model.dart';
import 'package:doctor_app_flutter/models/SOAP/master_key_model.dart';
import 'package:doctor_app_flutter/models/SOAP/post_allergy_request_model.dart';
import 'package:doctor_app_flutter/models/SOAP/post_assessment_request_model.dart';
import 'package:doctor_app_flutter/models/SOAP/post_chief_complaint_request_model.dart';
import 'package:doctor_app_flutter/models/SOAP/post_histories_request_model.dart';
import 'package:doctor_app_flutter/models/SOAP/post_physical_exam_request_model.dart';
import 'package:doctor_app_flutter/models/SOAP/post_progress_note_request_model.dart';
import 'package:doctor_app_flutter/widgets/patients/profile/soap_update/update_assessment_page.dart';

import 'base/lookup-service.dart';

class SOAPService extends LookupService {
  List<GetChiefComplaintResModel> patientChiefComplaintList  = [];
  List<GetAllergiesResModel> patientAllergiesList  = [];
  List<GetHistoryResModel> patientHistoryList  = [];
  List<GetPhysicalExamResModel> patientPhysicalExamList  = [];
  List<GetPatientProgressNoteResModel> patientProgressNoteList  = [];
  List<GetAssessmentResModel> patientAssessmentList  = [];

  int episodeID;
  Future getAllergies(GetAllergiesRequestModel getAllergiesRequestModel) async {
    await baseAppClient.post(
      GET_ALLERGIES,
      onSuccess: (dynamic response, int statusCode) {
        allergiesList.clear();
        response['List_Allergies']['entityList'].forEach((v) {
          allergiesList.add(MasterKeyModel.fromJson(v));
        });
      },
      onFailure: (String error, int statusCode) {
        hasError = true;
        super.error = error;
      },
      body: getAllergiesRequestModel.toJson(),
    );
  }

  Future postEpisode(PostEpisodeReqModel postEpisodeReqModel) async {
    hasError = false;

    await baseAppClient.post(POST_EPISODE,
        onSuccess: (dynamic response, int statusCode) {

          print("Success");
          episodeID = response['EpisodeID'];
        }, onFailure: (String error, int statusCode) {
          hasError = true;
          super.error = error;
        }, body: postEpisodeReqModel.toJson());
  }

  Future postAllergy(PostAllergyRequestModel postAllergyRequestModel) async {
    hasError = false;

    await baseAppClient.post(POST_ALLERGY,
        onSuccess: (dynamic response, int statusCode) {
          print("Success");
        }, onFailure: (String error, int statusCode) {
          hasError = true;
          super.error = error;
        }, body: postAllergyRequestModel.toJson());
  }

  Future postHistories(
      PostHistoriesRequestModel postHistoriesRequestModel) async {
    hasError = false;
    await baseAppClient.post(POST_HISTORY,
        onSuccess: (dynamic response, int statusCode) {
          print("Success");
        }, onFailure: (String error, int statusCode) {
          hasError = true;
          super.error = error;
        }, body: postHistoriesRequestModel.toJson());
  }

  Future postChiefComplaint(
      PostChiefComplaintRequestModel postChiefComplaintRequestModel) async {
    hasError = false;
    await baseAppClient.post(POST_CHIEF_COMPLAINT,
        onSuccess: (dynamic response, int statusCode) {
          print("Success");
    }, onFailure: (String error, int statusCode) {
      hasError = true;
      super.error = error;
    }, body: postChiefComplaintRequestModel.toJson());
  }

  Future postPhysicalExam(
      PostPhysicalExamRequestModel postPhysicalExamRequestModel) async {
    hasError = false;
    await baseAppClient.post(POST_PHYSICAL_EXAM,
        onSuccess: (dynamic response, int statusCode) {
      print("Success");
    }, onFailure: (String error, int statusCode) {
      hasError = true;
      super.error = error;
    }, body: postPhysicalExamRequestModel.toJson());
  }

  Future postProgressNote(
      PostProgressNoteRequestModel postProgressNoteRequestModel) async {
    hasError = false;
    await baseAppClient.post(POST_PROGRESS_NOTE,
        onSuccess: (dynamic response, int statusCode) {
      print("Success");
    }, onFailure: (String error, int statusCode) {
      hasError = true;
      super.error = error;
    }, body: postProgressNoteRequestModel.toJson());
  }

  Future postAssessment(
      PostAssessmentRequestModel postAssessmentRequestModel) async {
    hasError = false;
    await baseAppClient.post(POST_ASSESSMENT,
        onSuccess: (dynamic response, int statusCode) {
      print("Success");
    }, onFailure: (String error, int statusCode) {
      hasError = true;
      super.error = error;
    }, body: postAssessmentRequestModel.toJson());
  }



  Future patchAllergy(PostAllergyRequestModel patchAllergyRequestModel) async {
    hasError = false;

    await baseAppClient.post(PATCH_ALLERGY,
        onSuccess: (dynamic response, int statusCode) {
          print("Success");
        }, onFailure: (String error, int statusCode) {
          hasError = true;
          super.error = error;
        }, body: patchAllergyRequestModel.toJson());
  }

  Future patchHistories(
      PostHistoriesRequestModel patchHistoriesRequestModel) async {
    hasError = false;
    await baseAppClient.post(PATCH_HISTORY,
        onSuccess: (dynamic response, int statusCode) {
          print("Success");
        }, onFailure: (String error, int statusCode) {
          hasError = true;
          super.error = error;
        }, body: patchHistoriesRequestModel.toJson());
  }

  Future patchChiefComplaint(
      PostChiefComplaintRequestModel patchChiefComplaintRequestModel) async {
    hasError = false;
    await baseAppClient.post(PATCH_CHIEF_COMPLAINT,
        onSuccess: (dynamic response, int statusCode) {
          print("Success");
        }, onFailure: (String error, int statusCode) {
          hasError = true;
          super.error = error;
        }, body: patchChiefComplaintRequestModel.toJson());
  }

  Future patchPhysicalExam(
      PostPhysicalExamRequestModel patchPhysicalExamRequestModel) async {
    hasError = false;
    await baseAppClient.post(PATCH_PHYSICAL_EXAM,
        onSuccess: (dynamic response, int statusCode) {
          print("Success");
        }, onFailure: (String error, int statusCode) {
          hasError = true;
          super.error = error;
        }, body: patchPhysicalExamRequestModel.toJson());
  }

  Future patchProgressNote(
      PostProgressNoteRequestModel patchProgressNoteRequestModel) async {
    hasError = false;
    await baseAppClient.post(PATCH_PROGRESS_NOTE,
        onSuccess: (dynamic response, int statusCode) {
          print("Success");
        }, onFailure: (String error, int statusCode) {
          hasError = true;
          super.error = error;
        }, body: patchProgressNoteRequestModel.toJson());
  }

  Future patchAssessment(
      PatchAssessmentReqModel patchAssessmentRequestModel) async {
    hasError = false;
    await baseAppClient.post(PATCH_ASSESSMENT,
        onSuccess: (dynamic response, int statusCode) {
          print("Success");
        }, onFailure: (String error, int statusCode) {
          hasError = true;
          super.error = error;
        }, body: patchAssessmentRequestModel.toJson());
  }



  Future getPatientAllergy(GeneralGetReqForSOAP generalGetReqForSOAP) async {
    hasError = false;

    await baseAppClient.post (GET_ALLERGY,
        onSuccess: (dynamic response, int statusCode) {
          print("Success");
          patientAllergiesList.clear();

          response['List_Allergies']['entityList'].forEach((v) {
            patientAllergiesList.add(GetAllergiesResModel.fromJson(v));
          });
        }, onFailure: (String error, int statusCode) {
          hasError = true;
          super.error = error;
        }, body: generalGetReqForSOAP.toJson());
  }

  Future getPatientHistories(
      GetHistoryReqModel getHistoryReqModel, {bool isFirst = false}) async {
    hasError = false;
    await baseAppClient.post(GET_HISTORY,
        onSuccess: (dynamic response, int statusCode) {
      print("Success");
      if (isFirst)
        patientHistoryList.clear();
      response['List_History']['entityList'].forEach((v) {
        patientHistoryList.add(GetHistoryResModel.fromJson(v));
      });
    }, onFailure: (String error, int statusCode) {
      hasError = true;
          super.error = error;
        }, body: getHistoryReqModel.toJson());
  }

  Future getPatientChiefComplaint(
      GetChiefComplaintReqModel getChiefComplaintReqModel) async {
    hasError = false;
    await baseAppClient.post (GET_CHIEF_COMPLAINT,
        onSuccess: (dynamic response, int statusCode) {
          print("Success");
          patientChiefComplaintList.clear();
          response['List_ChiefComplaint']['entityList'].forEach((v) {
            patientChiefComplaintList.add(GetChiefComplaintResModel.fromJson(v));
          });
        }, onFailure: (String error, int statusCode) {
          hasError = true;
          super.error = error;
        }, body: getChiefComplaintReqModel.toJson());
  }

  Future getPatientPhysicalExam(
      GetPhysicalExamReqModel getPhysicalExamReqModel) async {
    hasError = false;
    await baseAppClient.post (GET_PHYSICAL_EXAM,
        onSuccess: (dynamic response, int statusCode) {
          patientPhysicalExamList.clear();
          response['PhysicalExamList']['entityList'].forEach((v) {
            patientPhysicalExamList.add(GetPhysicalExamResModel.fromJson(v));
          });

        }, onFailure: (String error, int statusCode) {
          hasError = true;
          super.error = error;
        }, body: getPhysicalExamReqModel.toJson());
  }

  Future getPatientProgressNote(
      GetGetProgressNoteReqModel getGetProgressNoteReqModel) async {
    hasError = false;
    await baseAppClient.post (GET_PROGRESS_NOTE,
        onSuccess: (dynamic response, int statusCode) {
          print("Success");
          patientProgressNoteList.clear();
          response['ProgressNoteList']['entityList'].forEach((v) {
            patientProgressNoteList.add(GetPatientProgressNoteResModel.fromJson(v));
          });

        }, onFailure: (String error, int statusCode) {
          hasError = true;
          super.error = error;
        }, body: getGetProgressNoteReqModel.toJson());
  }

  Future getPatientAssessment(
      GetAssessmentReqModel getAssessmentReqModel) async {
    hasError = false;
    await baseAppClient.post (GET_ASSESSMENT,
        onSuccess: (dynamic response, int statusCode) {
          print("Success");
          patientAssessmentList.clear();
          response['AssessmentList']['entityList'].forEach((v) {
            patientAssessmentList.add(GetAssessmentResModel.fromJson(v));
          });
        }, onFailure: (String error, int statusCode) {
          hasError = true;
          super.error = error;
        }, body: getAssessmentReqModel.toJson());
  }
}
