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/service/medical/BloodPressureService.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 BloodPressureViewMode extends BaseViewModel {
  BloodPressureService bloodPressureService = locator<BloodPressureService>();

  ///BLOOD
  List<WeekChartDate> _bloodDiastolicPressureWeeklyTimeSeriesSalesList = List();
  List<WeekChartDate> _bloodSystolicePressureWeeklyTimeSeriesSalesList = List();

  List<YearMonthlyChartDate> _bloodDiastolicMonthlyTimeSeriesSalesList = List();
  List<YearMonthlyChartDate> _bloodSystolicMonthlyTimeSeriesSalesList = List();

  List<WeekChartDate> _bloodSystoliceYearTimeSeriesSalesList = List();
  List<WeekChartDate> _bloodDiastolicYearTimeSeriesSalesList = List();

  List<BloodPressureResult> get monthDiabtecPatientResult => bloodPressureService.monthDiabtecPatientResult;

  List<BloodPressureResult> get weekDiabtecPatientResult => bloodPressureService.weekDiabtecPatientResult;

  List<BloodPressureResult> get yearDiabtecPatientResult => bloodPressureService.yearDiabtecPatientResult;

  Future getBloodPressure() async {
    setState(ViewState.Busy);
    await bloodPressureService.getBloodSugar();
    await bloodPressureService.getDiabtecResults();
    if (bloodPressureService.hasError) {
      error = bloodPressureService.error;
      setState(ViewState.Error);
    } else {
      bloodPressureService.weekDiabtectResultAverageList.forEach((element) {
        _bloodDiastolicPressureWeeklyTimeSeriesSalesList.add(WeekChartDate(
            x: element.bloodPressureDate,
            y: element.dailyDiastolicPressureAverageResult));
        _bloodSystolicePressureWeeklyTimeSeriesSalesList.add(WeekChartDate(
            x: element.bloodPressureDate,
            y: element.dailySystolicePressureAverageResult));
      });

      for (int index = 0;
          index < bloodPressureService.monthDiabtectResultAverageList.length;
          index++) {
        _bloodDiastolicMonthlyTimeSeriesSalesList.add(YearMonthlyChartDate(
            x: index,
            y: bloodPressureService.monthDiabtectResultAverageList[index]
                .weekDiastolicPressureAverageResult));
        _bloodSystolicMonthlyTimeSeriesSalesList.add(YearMonthlyChartDate(
            x: index,
            y: bloodPressureService.monthDiabtectResultAverageList[index]
                .weekSystolicePressureAverageResult));
      }

      bloodPressureService.yearDiabtecResultAverageList.forEach((element) {
        _bloodSystoliceYearTimeSeriesSalesList
            .add(WeekChartDate(x: element.date, y: element.monthSystolicePressureAverageResult));

        _bloodDiastolicYearTimeSeriesSalesList
            .add(WeekChartDate(x: element.date, y: element.monthDiastolicPressureAverageResult));
      });

      setState(ViewState.Idle);
    }
  }

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

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

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

  addDiabtecResult(
      {String bloodPressureDate,
        String diastolicPressure,
        String systolicePressure,
        int measuredArm}) async {
    setState(ViewState.BusyLocal);
    await bloodPressureService.addDiabtecResult(
        bloodPressureDate: bloodPressureDate,
        diastolicPressure: diastolicPressure,
        systolicePressure: systolicePressure,
        measuredArm: measuredArm);
    if (bloodPressureService.hasError) {
      error = bloodPressureService.error;
      setState(ViewState.Error);
    } else {
      await getBloodPressure();
      setState(ViewState.Idle);
    }
  }
}
