import 'package:hmg_patient_app/config/config.dart';
import 'package:hmg_patient_app/core/model/my_trakers/blood_sugar/DiabtecPatientResult.dart';
import 'package:hmg_patient_app/core/model/my_trakers/blood_sugar/MonthDiabtectResultAverage.dart';
import 'package:hmg_patient_app/core/model/my_trakers/blood_sugar/WeekDiabtectResultAverage.dart';
import 'package:hmg_patient_app/core/model/my_trakers/blood_sugar/YearDiabtecResultAverage.dart';
import 'package:hmg_patient_app/core/service/base_service.dart';

class BloodSugarService extends BaseService {
  List<MonthDiabtectResultAverage> monthDiabtectResultAverageList =[];
  List<WeekDiabtectResultAverage> weekDiabtectResultAverageList =[];
  List<YearDiabtecResultAverage> yearDiabtecResultAverageList =[];

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

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

      response['List_MonthDiabtectResultAverage'].forEach((item) {
        monthDiabtectResultAverageList.add(MonthDiabtectResultAverage.fromJson(item));
      });

      response['List_WeekDiabtectResultAverage'].forEach((item) {
        weekDiabtectResultAverageList.add(WeekDiabtectResultAverage.fromJson(item));
      });

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

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

      response['List_MonthDiabtecPatientResult'].forEach((item) {
        monthDiabtecPatientResult.add(DiabtecPatientResult.fromJson(item));
      });

      response['List_WeekDiabtecPatientResult'].forEach((item) {
        weekDiabtecPatientResult.add(DiabtecPatientResult.fromJson(item));
      });

      response['List_YearDiabtecPatientResult'].forEach((item) {
        yearDiabtecPatientResult.add(DiabtecPatientResult.fromJson(item));
      });
    }, onFailure: (String error, int statusCode) {
      super.error = error;
    }, body: Map());
  }

  addDiabtecResult({String? bloodSugerDateChart, String? bloodSugerResult, String? diabtecUnit, int? measuredTime}) async {
    hasError = false;
    super.error = "";
    Map<String, dynamic> body = Map();
    body['BloodSugerDateChart'] = bloodSugerDateChart;
    body['BloodSugerResult'] = bloodSugerResult;
    body['DiabtecUnit'] = diabtecUnit;
    body['MeasuredTime'] = measuredTime! + 1;
    body['isDentalAllowedBackend'] = false;
    await baseAppClient.post(ADD_DIABTEC_RESULT, onSuccess: (response, statusCode) async {}, onFailure: (String error, int statusCode) {
      hasError = true;
      super.error = error;
    }, body: body);
  }

  updateDiabtecResult({DateTime? month, DateTime? hour, String? bloodSugerResult, String? diabtecUnit, int? measuredTime, int? lineItemNo}) async {
    hasError = false;
    super.error = "";
    Map<String, dynamic> body = Map();
    body['BloodSugerResult'] = bloodSugerResult;
    body['DiabtecUnit'] = diabtecUnit;
    body['BloodSugerDateChart'] = '${month!.year}-${month.month}-${month.day} ${hour!.hour}:${hour.minute}:00';
    body['isDentalAllowedBackend'] = false;
    body['MeasuredTime'] = measuredTime! + 1;
    body['LineItemNo'] = lineItemNo;
    await baseAppClient.post(UPDATE_DIABETIC_RESULT, onSuccess: (response, statusCode) async {}, onFailure: (String error, int statusCode) {
      hasError = true;
      super.error = error;
    }, body: body);
  }

  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_SUGAR_REPORT, 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_DIABETIC_STATUS, onSuccess: (response, statusCode) async {}, onFailure: (String error, int statusCode) {
      hasError = true;
      super.error = error;
    }, body: body);
  }
}
