import 'package:aitrainer_app/model/exercise_plan.dart';
import 'package:aitrainer_app/model/exercise_plan_detail.dart';
import 'package:aitrainer_app/util/not_found_exception.dart';
import 'dart:convert';
import 'api.dart';

class ExercisePlanApi {
  final APIClient _client = new APIClient();

  Future<ExercisePlan> saveExercisePlan(ExercisePlan exercisePlan) async {
    String body = JsonEncoder().convert(exercisePlan.toJson());
    print(" ===== saving exercisePlan $exercisePlan");
    ExercisePlan savedExercisePlan;
    try {
      final String responseBody = await _client.post(
        "exercise_plan",
        body);
      savedExercisePlan = ExercisePlan.fromJson(jsonDecode(responseBody));

    } on Exception catch(e) {
      throw new Exception(e.toString());
    }
    return savedExercisePlan;
  }

  Future<ExercisePlan> updateExercisePlan(
      ExercisePlan exercisePlan,
      int exercisePlanId) async {
    String body = JsonEncoder().convert(exercisePlan.toJson());
    print(" ===== update exercisePlan $exercisePlan");
    ExercisePlan updatedExercisePlan;
    try {
      final String responseBody = await _client.post(
        "exercise_plan/" + exercisePlanId.toString(),
        body);
      updatedExercisePlan = ExercisePlan.fromJson(jsonDecode(responseBody));

    } on Exception catch(e) {
      throw new Exception(e.toString());
    }
    return updatedExercisePlan;
  }

  Future<ExercisePlanDetail> saveExercisePlanDetail(ExercisePlanDetail exercisePlanDetail) async {
    String body = JsonEncoder().convert(exercisePlanDetail.toJson());
    print(" ===== save exercisePlanDetail $exercisePlanDetail");
    ExercisePlanDetail savedExercisePlanDetail;
    try {
      final String responseBody = await _client.post(
        "exercise_plan_detail",
        body);
      savedExercisePlanDetail = ExercisePlanDetail.fromJson(jsonDecode(responseBody));
    } on Exception catch(e) {
      throw new Exception(e.toString());
    }
    return savedExercisePlanDetail;
  }

  Future<ExercisePlanDetail> updateExercisePlanDetail(ExercisePlanDetail exercisePlanDetail,
      int exercisePlanDetailId) async {
    String body = JsonEncoder().convert(exercisePlanDetail.toJson());
    print(" ===== update exercisePlanDetail $exercisePlanDetail");
    ExercisePlanDetail savedExercisePlanDetail;
    try {
      final String responseBody = await _client.post(
        "exercise_plan_detail/" + exercisePlanDetailId.toString(),
        body);
      savedExercisePlanDetail = ExercisePlanDetail.fromJson(jsonDecode(responseBody));
    } on Exception catch(e) {
      throw new Exception(e.toString());
    }
    return savedExercisePlanDetail;
  }

  Future<void> deleteExercisePlanDetail(int exercisePlanDetailId) async {
    print(" ===== delete exercisePlanDetail $exercisePlanDetailId");
    String body = "";
    try {
      await _client.post(
        "exercise_plan_detail/delete/" + exercisePlanDetailId.toString(),
        body);

    } on Exception catch(e) {
      throw new Exception(e.toString());
    }
    return;
  }

  Future<void> deleteExercisePlan(int exercisePlanId) async {
    String body = "";
    print(" ===== delete exercisePlan $exercisePlanId");

    try {
      await _client.post(
        "exercise_plan/delete/" + exercisePlanId.toString(),
        body);

    } on Exception catch(e) {
      throw new Exception(e.toString());
    }
    return;
  }

  Future<ExercisePlan> getLastExercisePlan(int customerId) async {
    String body = "";
    print(" ===== get last exercisePlan $customerId");
    ExercisePlan exercisePlan;
    try {
      final String responseBody = await _client.get(
        "exercise_plan/last/" + customerId.toString(),
        body);
      exercisePlan = ExercisePlan.fromJson(jsonDecode(responseBody));
    } on Exception catch(e) {
      if ( e is NotFoundException) {
        print("ExercisePlan not found for " + customerId.toString());
        return exercisePlan;
      } else {
        throw new Exception(e.toString());
      }
    }
    return exercisePlan;
  }

  Future<List<ExercisePlanDetail>> getExercisePlanDetail(int exercisePlanId) async {
    String body = "";
    print(" ===== get exercisePlanDetail $exercisePlanId");
    List<ExercisePlanDetail> listExercisePlanDetail;
    try {
      final String responseBody = await _client.get(
        "exercise_plan_detail/" + exercisePlanId.toString(),
        body);
      print("response body:" + responseBody);
      final Iterable json = jsonDecode(responseBody);
      listExercisePlanDetail = json.map( (planDetail) => ExercisePlanDetail.fromJson(planDetail) ) .toList();
    } on Exception catch(e) {
      throw new Exception(e.toString());
    }
    return listExercisePlanDetail;
  }




}