import 'package:hmg_patient_app/config/config.dart';
import 'package:hmg_patient_app/core/model/my_trakers/blood_pressur/BloodPressureResult.dart';
import 'package:hmg_patient_app/core/model/my_trakers/blood_pressur/MonthBloodPressureResultAverage.dart';
import 'package:hmg_patient_app/core/model/my_trakers/blood_pressur/WeekBloodPressureResultAverage.dart';
import 'package:hmg_patient_app/core/model/my_trakers/blood_pressur/YearBloodPressureResultAverage.dart';
import 'package:hmg_patient_app/core/service/base_service.dart';

class BloodPressureService extends BaseService {
  List<MonthBloodPressureResultAverage> monthDiabtectResultAverageList =[];
  List<WeekBloodPressureResultAverage> weekDiabtectResultAverageList =[];
  List<YearBloodPressureResultAverage> yearDiabtecResultAverageList =[];

  ///Result
  List<BloodPressureResult> monthDiabtecPatientResult =[];
  List<BloodPressureResult> weekDiabtecPatientResult =[];
  List<BloodPressureResult> yearDiabtecPatientResult =[];

  Future getBloodSugar() async {
    hasError = false;
    Map<String, dynamic> body = Map();
    body['isDentalAllowedBackend'] = false;
    await baseAppClient.post(GET_BLOOD_PRESSURE_RESULT_AVERAGE,
        onSuccess: (dynamic response, int statusCode) {
      monthDiabtectResultAverageList.clear();
      weekDiabtectResultAverageList.clear();
      yearDiabtecResultAverageList.clear();

      response['List_MonthBloodPressureResultAverage'].forEach((item) {
        monthDiabtectResultAverageList
            .add(MonthBloodPressureResultAverage.fromJson(item));
      });

      response['List_WeekBloodPressureResultAverage'].forEach((item) {
        weekDiabtectResultAverageList
            .add(WeekBloodPressureResultAverage.fromJson(item));
      });

      response['List_YearBloodPressureResultAverage'].forEach((item) {
        yearDiabtecResultAverageList
            .add(YearBloodPressureResultAverage.fromJson(item));
      });
    }, onFailure: (String error, int statusCode) {
      hasError = true;
      super.error = error;
    }, body: body);
  }

  Future getDiabtecResults() async {
    hasError = false;
    await baseAppClient.post(GET_BLOOD_PRESSURE_RESULT,
        onSuccess: (dynamic response, int statusCode) {
      monthDiabtecPatientResult.clear();
      weekDiabtecPatientResult.clear();
      yearDiabtecPatientResult.clear();

      response['List_WeekBloodPressureResult'].forEach((item) {
        weekDiabtecPatientResult.add(BloodPressureResult.fromJson(item));
      });

      response['List_MonthBloodPressureResult'].forEach((item) {
        monthDiabtecPatientResult.add(BloodPressureResult.fromJson(item));
      });

      response['List_YearBloodPressureResult'].forEach((item) {
        yearDiabtecPatientResult.add(BloodPressureResult.fromJson(item));
      });
    }, onFailure: (String error, int statusCode) {
      hasError = true;
      super.error = error;
    }, body: Map());
  }
  Future sendReportByEmail() async {
    hasError = false;
    super.error = "";
    Map<String, dynamic> body = Map();
    body['isDentalAllowedBackend'] = false;
    body['To'] = user.emailAddress;
    await baseAppClient.post(SEND_AVERAGE_BLOOD_PRESSURE_REPORT,
        onSuccess: (response, statusCode) async {},
        onFailure: (String error, int statusCode) {
          hasError = true;
          super.error = error;
        }, body: body);
  }

  addDiabtecResult(
      {String? bloodPressureDate,
      String? diastolicPressure,
      String? systolicePressure,
      int? measuredArm}) async {
    hasError = false;
    super.error = "";

    Map<String, dynamic> body = Map();
    body['BloodPressureDate'] = bloodPressureDate;
    body['DiastolicPressure'] = diastolicPressure;
    body['SystolicePressure'] = systolicePressure;
    body['MeasuredArm'] = measuredArm;
    body['isDentalAllowedBackend'] = false;

    await baseAppClient.post(ADD_BLOOD_PRESSURE_RESULT,
        onSuccess: (response, statusCode) async {

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

  updateDiabtecResult(
      {String? bloodPressureDate,
        String? diastolicPressure,
        String? systolicePressure,
        int? lineItemNo,
        int? measuredArm}) async {
    hasError = false;
    super.error = "";

    Map<String, dynamic> body = Map();
    body['BloodPressureDate'] = bloodPressureDate;
    body['DiastolicPressure'] = diastolicPressure;
    body['SystolicePressure'] = systolicePressure;
    body['MeasuredArm'] ='$measuredArm';
    body['LineItemNo'] = lineItemNo;
    body['isDentalAllowedBackend'] = false;

    await baseAppClient.post(UPDATE_BLOOD_PRESSURE_RESULT,
        onSuccess: (response, statusCode) async {

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

  Future deactivateDiabeticStatus({int? lineItemNo }) async {
    hasError = false;
    super.error = "";
    Map<String, dynamic> body = Map();
    body['isDentalAllowedBackend'] = false;
    body['LineItemNo'] =lineItemNo;
    await baseAppClient.post(DEACTIVATE_BLOOD_PRESSURES_STATUS,
        onSuccess: (response, statusCode) async {},
        onFailure: (String error, int statusCode) {
          hasError = true;
          super.error = error;
        }, body: body);
  }
}
