import 'dart:convert';

import 'package:aitrainer_app/model/exercise_result.dart';
import 'package:aitrainer_app/service/logging.dart';

import 'api.dart';

class ExerciseResultApi with Logging {
  final APIClient _client = new APIClient();

  Future<void> saveExerciseResult(ExerciseResult exerciseResult) async {
    String body = JsonEncoder().convert(exerciseResult.toJson());
    log(" ===== saving exercise result:" + body);
    await _client.post("exercise_result", body);
  }

  Future<List<ExerciseResult>> getExerciseResultsByCustomer(int customerId) async {
    final body = await _client.get("exercise_result/", customerId.toString());
    final Iterable json = jsonDecode(body);
    final List<ExerciseResult> exerciseResults = json.map((exerciseResult) {
      ExerciseResult item = ExerciseResult.fromJson(exerciseResult);
      return item;
    }).toList();
    //exercises.sort( (a, b) => b.dateAdd.compareTo(a.dateAdd) );

    return exerciseResults;
  }
}
