import 'package:diplomaticquarterapp/config/config.dart';
import 'package:diplomaticquarterapp/core/model/my_trakers/weight/MonthWeightMeasurementResultAverage.dart';
import 'package:diplomaticquarterapp/core/model/my_trakers/weight/WeekWeightMeasurementResultAverage.dart';
import 'package:diplomaticquarterapp/core/model/my_trakers/weight/WeightMeasurementResult.dart';
import 'package:diplomaticquarterapp/core/model/my_trakers/weight/YearWeightMeasurementResultAverage.dart';
import 'package:diplomaticquarterapp/core/service/base_service.dart';

class WeightService extends BaseService {
  ///Average
  List<MonthWeightMeasurementResultAverage> monthWeightMeasurementResultAverage = List();
  List<WeekWeightMeasurementResultAverage> weekWeightMeasurementResultAverage = List();
  List<YearWeightMeasurementResultAverage> yearWeightMeasurementResultAverage = List();

  ///Result
  List<WeightMeasurementResult> monthWeightMeasurementResult = List();
  List<WeightMeasurementResult> weekWeightMeasurementResult = List();
  List<WeightMeasurementResult> yearWeightMeasurementResult = List();

  Future getWeightAverage() async {
    hasError = false;
    Map<String, dynamic> body = Map();
    body['isDentalAllowedBackend'] = false;
    monthWeightMeasurementResultAverage.clear();
    weekWeightMeasurementResultAverage.clear();
    yearWeightMeasurementResultAverage.clear();
    await baseAppClient.post(GET_WEIGHT_PRESSURE_RESULT_AVERAGE, onSuccess: (dynamic response, int statusCode) {
      response['List_MonthWeightMeasurementResultAverage'].forEach((item) {
        monthWeightMeasurementResultAverage.add(MonthWeightMeasurementResultAverage.fromJson(item));
      });

      response['List_WeekWeightMeasurementResultAverage'].forEach((item) {
        weekWeightMeasurementResultAverage.add(WeekWeightMeasurementResultAverage.fromJson(item));
      });

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

  Future getWeightMeasurementResult() async {
    hasError = false;
    monthWeightMeasurementResult.clear();
    weekWeightMeasurementResult.clear();
    yearWeightMeasurementResult.clear();
    await baseAppClient.post(GET_WEIGHT_PRESSURE_RESULT, onSuccess: (dynamic response, int statusCode) {

      response['List_WeekWeightMeasurementResult'].forEach((item) {
        weekWeightMeasurementResult.add(WeightMeasurementResult.fromJson(item));
      });

      response['List_MonthWeightMeasurementResult'].forEach((item) {
        monthWeightMeasurementResult.add(WeightMeasurementResult.fromJson(item));
      });

      response['List_YearWeightMeasurementResult'].forEach((item) {
        yearWeightMeasurementResult.add(WeightMeasurementResult.fromJson(item));
      });
    }, onFailure: (String error, int statusCode) {
      hasError = true;
      super.error = error;
    }, body: Map());
  }

  addWeightResult({String weightDate, String weightMeasured, int weightUnit}) async {
    hasError = false;
    super.error = "";

    Map<String, dynamic> body = Map();
    body['WeightDate'] = weightDate;
    body['WeightMeasured'] = weightMeasured;
    body['weightUnit'] = weightUnit;
    body['isDentalAllowedBackend'] = false;

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

  updateWeightResult({int lineItemNo, int weightUnit, String weightMeasured, String weightDate}) async {
    hasError = false;
    super.error = "";
    Map<String, dynamic> body = Map();
    body['LineItemNo'] = lineItemNo;
    body['weightUnit'] = '$weightUnit';
    body['WeightMeasured'] = weightMeasured;
    body['WeightDate'] = weightDate;
    body['isDentalAllowedBackend'] = false;

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

  deleteWeightResult({
    int lineItemNo,
  }) async {
    hasError = false;
    super.error = "";
    Map<String, dynamic> body = Map();
    body['LineItemNo'] = lineItemNo;
    body['isDentalAllowedBackend'] = false;

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