import 'package:diplomaticquarterapp/core/enum/viewstate.dart';
import 'package:diplomaticquarterapp/core/model/my_trakers/blood_pressur/BloodPressureResult.dart';
import 'package:diplomaticquarterapp/core/service/medical/BloodPressureService.dart';
import 'package:diplomaticquarterapp/locator.dart';
import 'package:diplomaticquarterapp/widgets/charts/app_time_series_chart.dart';

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

class BloodPressureViewMode extends BaseViewModel {
  BloodPressureService bloodPressureService = locator<BloodPressureService>();

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

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

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

  List<TimeSeriesSales2> weightWeekTimeSeriesDataTop = [];
  List<TimeSeriesSales2> weightWeekTimeSeriesDataLow = [];

  List<TimeSeriesSales3> weighMonthTimeSeriesDataTop = [];
  List<TimeSeriesSales3> weighMonthTimeSeriesDataLow = [];

  List<TimeSeriesSales2> weightYearTimeSeriesDataTop = [];
  List<TimeSeriesSales2> weightYearTimeSeriesDataLow = [];

  Future getBloodPressure() async {
    setState(ViewState.Busy);
    await bloodPressureService.getBloodSugar();
    await bloodPressureService.getDiabtecResults();
    if (bloodPressureService.hasError) {
      error = bloodPressureService.error;
      setState(ViewState.ErrorLocal);
    } else {
      clearDate();
      bloodPressureService.weekDiabtectResultAverageList.forEach((element) {
        weightWeekTimeSeriesDataTop.add(TimeSeriesSales2(element.bloodPressureDate, element.dailyDiastolicPressureAverageResult.toDouble()));
        weightWeekTimeSeriesDataLow.add(TimeSeriesSales2(element.bloodPressureDate, element.dailySystolicePressureAverageResult.toDouble()));
      });

      for (int index = 0; index < bloodPressureService.monthDiabtectResultAverageList.length; index++) {
        // weighMonthTimeSeriesDataTop.add(TimeSeriesSales3("Week " + (index + 1).toString(), bloodPressureService.monthDiabtectResultAverageList[index].weekDiastolicPressureAverageResult.toDouble()));
        // weighMonthTimeSeriesDataLow.add(TimeSeriesSales3("Week " + (index + 1).toString(), bloodPressureService.monthDiabtectResultAverageList[index].weekSystolicePressureAverageResult.toDouble()));
        weighMonthTimeSeriesDataTop.add(TimeSeriesSales3(bloodPressureService.monthDiabtectResultAverageList[index].weekDesc, bloodPressureService.monthDiabtectResultAverageList[index].weekDiastolicPressureAverageResult.toDouble()));
        weighMonthTimeSeriesDataLow.add(TimeSeriesSales3(bloodPressureService.monthDiabtectResultAverageList[index].weekDesc, bloodPressureService.monthDiabtectResultAverageList[index].weekSystolicePressureAverageResult.toDouble()));
      }

      bloodPressureService.yearDiabtecResultAverageList.forEach((element) {
        weightYearTimeSeriesDataTop.add(TimeSeriesSales2(element.date, element.monthSystolicePressureAverageResult.toDouble()));

        weightYearTimeSeriesDataLow.add(TimeSeriesSales2(element.date, element.monthDiastolicPressureAverageResult.toDouble()));
      });

      setState(ViewState.Idle);
    }
  }

  void clearDate() {
    weightWeekTimeSeriesDataTop.clear();
    weightWeekTimeSeriesDataLow.clear();
    weighMonthTimeSeriesDataTop.clear();
    weighMonthTimeSeriesDataLow.clear();
    weightYearTimeSeriesDataTop.clear();
    weightYearTimeSeriesDataLow.clear();
  }

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

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

  Future addORUpdateDiabtecResult({String bloodPressureDate, String diastolicPressure, String systolicePressure, int measuredArm, int lineItemNo, bool isUpdate = false}) async {
    setState(ViewState.BusyLocal);
    if (!isUpdate)
      await bloodPressureService.addDiabtecResult(bloodPressureDate: bloodPressureDate, diastolicPressure: diastolicPressure, systolicePressure: systolicePressure, measuredArm: measuredArm);
    else
      await bloodPressureService.updateDiabtecResult(bloodPressureDate: bloodPressureDate, diastolicPressure: diastolicPressure, systolicePressure: systolicePressure, measuredArm: measuredArm, lineItemNo: lineItemNo);
    if (bloodPressureService.hasError) {
      error = bloodPressureService.error;
      setState(ViewState.ErrorLocal);
    } else {
      // await getBloodPressure();
      setState(ViewState.Idle);
    }
  }

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

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