import 'package:diplomaticquarterapp/core/enum/viewstate.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/service/medical/BloodSugarService.dart';
import 'package:diplomaticquarterapp/locator.dart';
import 'package:charts_flutter/flutter.dart' as charts;

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

class BloodSugarViewMode extends BaseViewModel {
  BloodSugarService bloodSugarService = locator<BloodSugarService>();

  ///BLOOD 
  List<WeekChartDate> _bloodWeeklyTimeSeriesSalesList = List();
  List<YearMonthlyChartDate> _bloodMonthlyTimeSeriesSalesList = List();
  List<WeekChartDate> _bloodYearTimeSeriesSalesList = List();

  List<DiabtecPatientResult> get monthDiabtecPatientResult =>
      bloodSugarService.monthDiabtecPatientResult;

  List<DiabtecPatientResult> get weekDiabtecPatientResult =>
      bloodSugarService.weekDiabtecPatientResult;

  List<DiabtecPatientResult> get yearDiabtecPatientResult =>
      bloodSugarService.yearDiabtecPatientResult;

  Future getBloodSugar() async {
    setState(ViewState.Busy);
    await bloodSugarService.getBloodSugar();
    await bloodSugarService.getDiabtecResults();
    if (bloodSugarService.hasError) {
      error = bloodSugarService.error;
      setState(ViewState.Error);
    } else {
      bloodSugarService.weekDiabtectResultAverageList.forEach((element) {
        _bloodWeeklyTimeSeriesSalesList.add(
            WeekChartDate(x: element.dateChart, y: element.dailyAverageResult));
      });

      for (int index = 0;
          index < bloodSugarService.monthDiabtectResultAverageList.length;
          index++) {
        _bloodMonthlyTimeSeriesSalesList.add(YearMonthlyChartDate(
            x: index,
            y: bloodSugarService
                .monthDiabtectResultAverageList[index].weekAverageResult));
        var asd="";
      }

      bloodSugarService.yearDiabtecResultAverageList.forEach((element) {
        _bloodYearTimeSeriesSalesList
            .add(WeekChartDate(x: element.date, y: element.monthAverageResult));
      });

      setState(ViewState.Idle);
    }
  }

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

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

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

  addDiabtecResult(
      {String bloodSugerDateChart,
      String bloodSugerResult,
      String diabtecUnit,
      int measuredTime}) async {
    setState(ViewState.BusyLocal);
    await bloodSugarService.addDiabtecResult(
        bloodSugerDateChart: bloodSugerDateChart,
        bloodSugerResult: bloodSugerResult ,
        diabtecUnit: diabtecUnit,
        measuredTime: measuredTime);
    if (bloodSugarService.hasError) {
      error = bloodSugarService.error;
      setState(ViewState.Error);
    } else {
      await getBloodSugar();
      setState(ViewState.Idle);
    }
  }
  
  
  
}
