import 'package:diplomaticquarterapp/config/config.dart';
import 'package:diplomaticquarterapp/core/model/reports/Reports.dart';
import 'package:diplomaticquarterapp/core/model/reports/request_reports.dart';
import 'package:diplomaticquarterapp/core/service/base_service.dart';
import 'package:diplomaticquarterapp/pages/feedback/appointment_history.dart';

class ReportsService extends BaseService {
  List<Reports> reportsList = List();
  List<AppointmentHistory> appointHistoryList = List();
  String userAgreementContent = "";
  RequestReports _requestReports = RequestReports(
    isReport: true,
    encounterType: 1,
    requestType: 1,
    patientOutSA: 0,
  );

  Future getReports() async {
    hasError = false;
    await baseAppClient.post(REPORTS,
        onSuccess: (dynamic response, int statusCode) {
      reportsList.clear();
      response['GetPatientMedicalStatus'].forEach((reports) {
        reportsList.add(Reports.fromJson(reports));
      });
    }, onFailure: (String error, int statusCode) {
      hasError = true;
      super.error = error;
    }, body: _requestReports.toJson());
  }

  Future getPatentAppointmentHistory() async {
    hasError = false;
    Map<String, dynamic> body = new Map<String, dynamic>();
    body['IsForMedicalReport'] = true;
    await baseAppClient.post(GET_PATIENT_AppointmentHistory,
        onSuccess: (dynamic response, int statusCode) {
      appointHistoryList = [];
      response['AppoimentAllHistoryResultList'].forEach((appoint) {
        appointHistoryList.add(AppointmentHistory.fromJson(appoint));
      });
    }, onFailure: (String error, int statusCode) {
      hasError = true;
      super.error = error;
    }, body: body);
  }

  Future getUserTermsAndConditions() async {
    hasError = false;
    await baseAppClient.post(GET_USER_TERMS,
        onSuccess: (dynamic response, int statusCode) {
      userAgreementContent = response['UserAgreementContent'];
    }, onFailure: (String error, int statusCode) {
      hasError = true;
      super.error = error;
    }, body: Map<String, dynamic>());
  }

  Future updatePatientHealthSummaryReport({bool isSummary}) async {
    Map<String, dynamic> body = Map<String, dynamic>();
    body['RSummaryReport'] = isSummary;
    hasError = false;
    await baseAppClient.post(UPDATE_HEALTH_TERMS,
        onSuccess: (dynamic response, int statusCode) {},
        onFailure: (String error, int statusCode) {
      hasError = true;
      super.error = error;
    }, body: body);
  }

  Future updateEmail({String email}) async {
    Map<String, dynamic> body = Map<String, dynamic>();
    body['EmailAddress'] = email;
    body['isDentalAllowedBackend'] = false;
    hasError = false;
    await baseAppClient.post(UPDATE_PATENT_EMAIL,
        onSuccess: (dynamic response, int statusCode) {},
        onFailure: (String error, int statusCode) {
          hasError = true;
          super.error = error;
        }, body: body);
  }

  Future insertRequestForMedicalReport(
      AppointmentHistory appointmentHistory) async {
    Map<String, dynamic> body = new Map<String, dynamic>();
    body['ClinicID'] = appointmentHistory.clinicID;
    body['DoctorID'] = appointmentHistory.doctorID;
    body['SetupID'] = appointmentHistory.setupID;
    body['EncounterNo'] = appointmentHistory.appointmentNo;
    body['EncounterType'] = 1; // appointmentHistory.appointmentType;
    body['IsActive'] = appointmentHistory.isActiveDoctor;
    body['ProjectID'] = appointmentHistory.projectID;
    body['Remarks'] = "";
    body['ProcedureId'] = "";
    body['RequestType'] = 1;
    body['Source'] = 2;
    body['Status'] = 1;
    body['CreatedBy'] = 102;
    hasError = false;
    await baseAppClient.post(INSERT_REQUEST_FOR_MEDICAL_REPORT,
        onSuccess: (dynamic response, int statusCode) {},
        onFailure: (String error, int statusCode) {
      hasError = true;
      super.error = error;
    }, body: body);
  }

  Future sendEmailForMedicalReport(
      String projectName,
      String clinicName,
      String doctorName,
      String requestDate,
      String invoiceNo,
      int projectID,
      String stamp,
      String setupID) async {
    Map<String, dynamic> body = new Map<String, dynamic>();
    body['SetupID'] = setupID;
    body['PrintDate'] = requestDate;
    body['ProcedureID'] = "05005009";
    body['Reporttype'] = "MEDICAL REPORT";
    body['stamp'] = stamp;
    body['To'] = user.emailAddress;
    body['DateofBirth'] = user.dateofBirth;
    body['PatientIditificationNum'] = user.patientIdentificationNo;
    body['PatientMobileNumber'] = user.mobileNumber;
    body['PatientName'] = user.firstName + " " + user.lastName;
    body['ProjectName'] = projectName;
    body['ClinicName'] = clinicName;
    body['ProjectID'] = projectID;
    body['InvoiceNo'] = invoiceNo;
    body['PrintedByName'] = user.firstName + " " + user.lastName;

    dynamic response;

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