import 'package:diplomaticquarterapp/core/enum/viewstate.dart';
import 'package:diplomaticquarterapp/core/model/AlHabibMedicalService/H2O/insert_user_activity_request_model.dart';
import 'package:diplomaticquarterapp/core/model/AlHabibMedicalService/H2O/user_progress_for_month_data_model.dart';
import 'package:diplomaticquarterapp/core/model/AlHabibMedicalService/H2O/user_progress_for_today_data_model.dart';
import 'package:diplomaticquarterapp/core/model/AlHabibMedicalService/H2O/user_progress_for_week_data_model.dart';
import 'package:diplomaticquarterapp/core/service/AlHabibMedicalService/H2O_service.dart';
import 'package:diplomaticquarterapp/core/viewModels/base_view_model.dart';
import 'package:charts_flutter/flutter.dart' as charts;
import '../../../locator.dart';

class H2OViewModel extends BaseViewModel {

  H2OService _h2OService = locator<H2OService>();

  List<charts.Series> userProgressForWeekDataSeries;
  List<charts.Series> userProgressForMonthDataSeries;

  UserProgressForTodayDataModel get userProgressData {
    if (_h2OService.userProgressForTodayDataList.length != 0)
      return _h2OService.userProgressForTodayDataList[0];
   return null;
  }


  Future getUserProgressForTodayData() async {
    // if(_h2OService.userProgressForTodayDataList.length==0){
    setState(ViewState.Busy);
    await _h2OService.getUserProgressForTodayData();
    if (_h2OService.hasError) {
      error = _h2OService.error;
      setState(ViewState.Error);
    } else {
      setState(ViewState.Idle);
    }
  }

  Future getUserProgressForWeekData() async {
    setState(ViewState.Busy);
    await _h2OService.getUserProgressForWeekData();
    if (_h2OService.hasError) {
      error = _h2OService.error;
      setState(ViewState.Error);
    } else {
      userProgressForWeekDataSeries = createUserProgressForWeekDataSeries();
      setState(ViewState.Idle);
    }
  }

  Future getUserProgressForMonthData() async {
    setState(ViewState.Busy);
    await _h2OService.getUserProgressForMonthData();
    if (_h2OService.hasError) {
      error = _h2OService.error;
      setState(ViewState.Error);
    } else {
      userProgressForMonthDataSeries = createUserProgressForMonthDataSeries();
      setState(ViewState.Idle);
    }
  }


  List<charts.Series<ChartSeries,
      String>> createUserProgressForWeekDataSeries() {
    List<ChartSeries> globalData = [
    ];
    _h2OService.userProgressForWeekDataList.forEach((
        UserProgressForWeekDataModel data) {
      globalData.add(new ChartSeries(data.dayName, data.percentageConsumed));
    });
    return [
      new charts.Series<ChartSeries, String>(
        id: 'Global Revenue',
        domainFn: (ChartSeries sales, _) => sales.y,
        measureFn: (ChartSeries sales, _) => sales.x,
        data: globalData,
      ),
    ];
  }

  List<charts.Series<ChartSeries,
      String>> createUserProgressForMonthDataSeries() {
    List<ChartSeries> globalData = [
    ];
    _h2OService.userProgressForMonthDataList.forEach((
        UserProgressForMonthDataModel data) {
      globalData.add(new ChartSeries(data.monthName, data.percentageConsumed));
    });
    return [
      new charts.Series<ChartSeries, String>(
        id: 'Global Revenue',
        domainFn: (ChartSeries sales, _) => sales.y,
        measureFn: (ChartSeries sales, _) => sales.x,
        data: globalData,
      ),
    ];
  }


  Future insertUserActivity(
      InsertUserActivityRequestModel insertUserActivityRequestModel) async {
    setState(ViewState.BusyLocal);
    insertUserActivityRequestModel.mobileNumber =
        user.mobileNumber.substring(1);
    insertUserActivityRequestModel.identificationNo =
        user.patientIdentificationNo;

    await _h2OService.insertUserActivity(insertUserActivityRequestModel);
    if (_h2OService.hasError) {
      error = _h2OService.error;
      setState(ViewState.ErrorLocal);
    } else {
      setState(ViewState.Idle);
    }
  }

}


/// Sample ordinal data type.
class ChartSeries {
  final String y;
  final int x;

  ChartSeries(this.y, this.x);
}
