import 'package:diplomaticquarterapp/core/enum/viewstate.dart';
import 'package:diplomaticquarterapp/core/model/my_trakers/blood_pressur/BloodPressureResult.dart';
import 'package:diplomaticquarterapp/core/model/my_trakers/blood_sugar/DiabtecPatientResult.dart';
import 'package:diplomaticquarterapp/core/model/my_trakers/chartData/WeekChartDate.dart';
import 'package:diplomaticquarterapp/core/model/my_trakers/chartData/YearMonthlyChartDate.dart';
import 'package:diplomaticquarterapp/core/model/my_trakers/weight/WeightMeasurementResult.dart';
import 'package:diplomaticquarterapp/core/service/medical/BloodPressureService.dart';
import 'package:diplomaticquarterapp/core/service/medical/BloodSugarService.dart';
import 'package:diplomaticquarterapp/core/service/medical/WeightPressureService.dart';
import 'package:diplomaticquarterapp/locator.dart';
import 'package:charts_flutter/flutter.dart' as charts;

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

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

  List<WeekChartDate> _weightWeeklyTimeSeriesSalesList = List();
  List<YearMonthlyChartDate> _weightMonthlyTimeSeriesSalesList = List();
  List<WeekChartDate> _weightYearTimeSeriesSalesList = List();

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

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

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

  Future getWeight() async {
    setState(ViewState.Busy);
    await weightService.getWeightAverage();
    await weightService.getWeightMeasurementResult();
    if (weightService.hasError) {
      error = weightService.error;
      setState(ViewState.Error);
    } else {
      weightService.weekWeightMeasurementResultAverage.forEach((element) {
        _weightWeeklyTimeSeriesSalesList.add(WeekChartDate(
            x: element.weightDate, y: element.dailyAverageResult));
      });

      for (int index = 0;
          index < weightService.monthWeightMeasurementResultAverage.length;
          index++) {
        _weightMonthlyTimeSeriesSalesList.add(YearMonthlyChartDate(
            x: index,
            y: weightService.monthWeightMeasurementResultAverage[index].weekAverageResult));
      }

      weightService.yearWeightMeasurementResultAverage.forEach((element) {
        _weightYearTimeSeriesSalesList
            .add(WeekChartDate(x: element.date, y: element.monthAverageResult));
      });

      setState(ViewState.Idle);
    }
  }

  List<charts.Series<WeekChartDate, DateTime>> getWeightWeeklySeries() {
    return [
      charts.Series<WeekChartDate, DateTime>(
        id: 'Diastolic',
        colorFn: (_, __) => charts.MaterialPalette.red.shadeDefault,
        domainFn: (WeekChartDate sales, _) => sales.x,
        measureFn: (WeekChartDate sales, _) => sales.y,
        data: _weightWeeklyTimeSeriesSalesList,
      ),
    ];
  }

  List<charts.Series<YearMonthlyChartDate, int>>
      getWeightMonthlyTimeSeriesSales() {
    return [
      charts.Series<YearMonthlyChartDate, int>(
        id: 'Sales',
        colorFn: (_, __) => charts.MaterialPalette.red.shadeDefault,
        domainFn: (YearMonthlyChartDate sales, _) => sales.x,
        measureFn: (YearMonthlyChartDate sales, _) => sales.y,
        data: _weightMonthlyTimeSeriesSalesList,
      ),
    ];
  }

  List<charts.Series<WeekChartDate, DateTime>> getWeightYearTimeSeriesSales() {
    return [
      charts.Series<WeekChartDate, DateTime>(
        id: 'Sales',
        colorFn: (_, __) => charts.MaterialPalette.red.shadeDefault,
        domainFn: (WeekChartDate sales, _) => sales.x,
        measureFn: (WeekChartDate sales, _) => sales.y,
        data: _weightYearTimeSeriesSalesList,
      ),
    ];
  }

  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);
    }
  }
}
