import 'package:diplomaticquarterapp/core/enum/viewstate.dart';
import 'package:diplomaticquarterapp/core/model/my_trakers/weight/WeightMeasurementResult.dart';
import 'package:diplomaticquarterapp/core/service/medical/WeightPressureService.dart';
import 'package:diplomaticquarterapp/locator.dart';
import 'package:diplomaticquarterapp/widgets/charts/app_time_series_chart.dart';

import '../../../core/viewModels/base_view_model.dart';

class WeightPressureViewModel extends BaseViewModel {
  WeightService weightService = locator<WeightService>();

  List<WeightMeasurementResult> get monthWeightMeasurementResult => weightService.monthWeightMeasurementResult;

  List<WeightMeasurementResult> get weekWeightMeasurementResult => weightService.weekWeightMeasurementResult;

  List<WeightMeasurementResult> get yearWeightMeasurementResult => weightService.yearWeightMeasurementResult;

  List<TimeSeriesSales2> weightWeekTimeSeriesData = [];
  List<TimeSeriesSales3> weighMonthTimeSeriesData = [];
  List<TimeSeriesSales2> weightYearTimeSeriesData = [];

  Future getWeight() async {
    setState(ViewState.Busy);
    await weightService.getWeightAverage();
    await weightService.getWeightMeasurementResult();
    if (weightService.hasError) {
      error = weightService.error;
      setState(ViewState.ErrorLocal);
    } else {
      weightService.weekWeightMeasurementResultAverage.forEach((element) {
        weightWeekTimeSeriesData.add(TimeSeriesSales2(
          element.weightDate,
          element.dailyAverageResult.toDouble(),
        ));
      });

      for (int index = 0; index < weightService.monthWeightMeasurementResultAverage.length; index++) {
        weighMonthTimeSeriesData.add(TimeSeriesSales3(
          index,
          weightService.monthWeightMeasurementResultAverage[index].weekAverageResult.toDouble(),
        ));
      }

      weightService.yearWeightMeasurementResultAverage.forEach((element) {
        weightYearTimeSeriesData.add(TimeSeriesSales2(
          element.date,
          element.monthAverageResult.toDouble(),
        ));
      });

      setState(ViewState.Idle);
    }
  }

  addWeightResult({String weightDate, String weightMeasured, int weightUnit}) async {
    setState(ViewState.BusyLocal);
    await weightService.addWeightResult(
      weightDate: weightDate,
      weightMeasured: weightMeasured,
      weightUnit: weightUnit,
    );
    if (weightService.hasError) {
      error = weightService.error;
      setState(ViewState.Error);
    } else {
      await getWeight();
      setState(ViewState.Idle);
    }
  }

  Future sendReportByEmail() async {
    setState(ViewState.BusyLocal);

    await weightService.sendReportByEmail();
    if (weightService.hasError) {
      error = weightService.error;
      setState(ViewState.ErrorLocal);
    } else {
      setState(ViewState.Idle);
    }
  }

  updateWeightResult({int lineItemNo, int weightUnit, String weightMeasured, String weightDate}) async {
    setState(ViewState.BusyLocal);
    await weightService.updateWeightResult(
        lineItemNo: lineItemNo, weightMeasured: weightMeasured, weightUnit: weightUnit, weightDate: weightDate);
    if (weightService.hasError) {
      error = weightService.error;
      setState(ViewState.Error);
    } else {
      await getWeight();
      setState(ViewState.Idle);
    }
  }

  deleteWeightResult({
    int lineItemNo,
  }) async {
    setState(ViewState.BusyLocal);
    await weightService.deleteWeightResult(
      lineItemNo: lineItemNo,
    );
    if (weightService.hasError) {
      error = weightService.error;
      setState(ViewState.Error);
    } else {
      await getWeight();
      setState(ViewState.Idle);
    }
  }
}
