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 'package:diplomaticquarterapp/widgets/charts/app_time_series_chart.dart';

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

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

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

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

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

  List<TimeSeriesSales2> bloodWeekTimeSeriesData = [];
  List<TimeSeriesSales2> yearTimeSeriesData = [];
  List<TimeSeriesSales3> monthTimeSeriesData = [];

  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) {
        bloodWeekTimeSeriesData.add(TimeSeriesSales2(
          element.dateChart,
          element.dailyAverageResult.toDouble(),
        ));
      });
      if (bloodWeekTimeSeriesData.isEmpty) {
        bloodWeekTimeSeriesData.add(TimeSeriesSales2(DateTime.now(), 0));
      }

      for (int index = 0;
          index < bloodSugarService.monthDiabtectResultAverageList.length;
          index++) {
        monthTimeSeriesData.add(TimeSeriesSales3(
          index,
          bloodSugarService
              .monthDiabtectResultAverageList[index].weekAverageResult
              .toDouble(),
        ));
      }

      bloodSugarService.yearDiabtecResultAverageList.forEach((element) {
        yearTimeSeriesData.add(TimeSeriesSales2(
          element.date,
          element.monthAverageResult.toDouble(),
        ));
      });

      setState(ViewState.Idle);
    }
  }

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

  Future updateDiabtecResult(
      {DateTime month,
      DateTime hour,
      String bloodSugerResult,
      String diabtecUnit,
      int measuredTime,
      int lineItemNo}) async {
    setState(ViewState.BusyLocal);

    await bloodSugarService.updateDiabtecResult(
        bloodSugerResult: bloodSugerResult,
        diabtecUnit: diabtecUnit,
        hour: hour,
        measuredTime: measuredTime,
        lineItemNo: lineItemNo,
        month: month);
    if (bloodSugarService.hasError) {
      error = bloodSugarService.error;
      setState(ViewState.ErrorLocal);
    } else {
      await getBloodSugar();
      setState(ViewState.Idle);
    }
  }

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

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

  Future deactivateDiabeticStatus({int lineItemNo}) async {
    setState(ViewState.BusyLocal);

    await bloodSugarService.deactivateDiabeticStatus(lineItemNo: lineItemNo);
    if (bloodSugarService.hasError) {
      error = bloodSugarService.error;
      setState(ViewState.ErrorLocal);
    } else {
      await getBloodSugar();
      setState(ViewState.Idle);
    }
  }
}
