import 'package:hmg_patient_app/config/config.dart';
import 'package:hmg_patient_app/core/model/rate/appointment_details.dart';
import 'package:hmg_patient_app/core/model/rate/appointment_rate.dart';
import 'package:hmg_patient_app/core/model/rate/appoitment_rated.dart';
import 'package:hmg_patient_app/core/service/base_service.dart';

class AppointmentRateService extends BaseService {
  List<AppoitmentRated> appointmentRatedList =[];
  AppointmentDetails? appointmentDetails;

  Future getIsLastAppointmentRatedList(int languageID) async {
    hasError = false;
    Map<String, dynamic> bodyData = Map();
    bodyData['LanguageID'] = languageID;
    await baseAppClient.post(IS_LAST_APPOITMENT_RATED, onSuccess: (dynamic response, int statusCode) {
      appointmentRatedList.clear();
      response['IsLastAppoitmentRatedList'].forEach((appoint) {
        appointmentRatedList.add(AppoitmentRated.fromJson(appoint));
      });
    }, onFailure: (String error, int statusCode) {
      hasError = true;
      super.error = error;
    }, body: bodyData);
  }

  Future getAppointmentDetails() async {
    hasError = false;
    Map<String, dynamic> bodyData = Map();
    bodyData['AppointmentNumber'] = appointmentRatedList[0].appointmentNo;
    bodyData['ProjectID'] = appointmentRatedList[0].projectID;
    await baseAppClient.post(GET_APPOINTMENT_DETAILS_BY_NO, onSuccess: (dynamic response, int statusCode) {
      if (response['AppointmentDetails'] != null) appointmentDetails = AppointmentDetails.fromJson(response['AppointmentDetails']);
    }, onFailure: (String error, int statusCode) {
      hasError = true;
      super.error = error;
    }, body: bodyData);
  }

  Future sendAppointmentRate(int rate, int appointmentNo, int projectID, int doctorID, int clinicID, String note, int languageID) async {
    hasError = false;
    AppointmentRate appointmentRate = AppointmentRate();
    appointmentRate.rate = rate;
    appointmentRate.appointmentNo = appointmentNo;
    appointmentRate.projectID = projectID;
    appointmentRate.doctorID = doctorID;
    appointmentRate.clinicID = clinicID;
    appointmentRate.note = note;
    appointmentRate.createdBy = 2;
    appointmentRate.editedBy = 2;
    appointmentRate.languageID = languageID;

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

  Future sendDoctorRate(int rate, int appointmentNo, int projectID, int doctorID, int clinicID, String note, String appoDate, String docName, String projectName, String clinicName) async {
    Map<String, dynamic> request;
    hasError = false;
    request = {
      "DoctorID": doctorID,
      "Rate": rate,
      "ClinicID": clinicID,
      "ProjectID": projectID,
      "AppointmentNo": appointmentNo,
      "Note": note,
      "MobileNumber": authenticatedUserObject.user!.mobileNumber,
      "AppointmentDate": appoDate,
      "DoctorName": docName,
      "ProjectName": projectName,
      "COCTypeName": 1,
      "PatientName": authenticatedUserObject.user!.firstName! + " " + authenticatedUserObject.user!.lastName!,
      "PatientOutSA": authenticatedUserObject.user!.outSA,
      "PatientTypeID": authenticatedUserObject.user!.patientType,
      "ClinicName": clinicName,
      "PatientIdentificationID": authenticatedUserObject.user!.patientIdentificationNo
    };

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

  AppoitmentRated get lastAppointmentRated {
    if (appointmentRatedList.length > 0) return appointmentRatedList[appointmentRatedList.length - 1];
    return AppoitmentRated();
  }

  deleteAppointmentRated(AppoitmentRated appointmentRated) {
    appointmentRatedList.remove(appointmentRated);
  }

  deleteAllAppAppointmentRate() => appointmentRatedList.clear();
}
