import 'dart:convert';

import 'package:aitrainer_app/model/cache.dart';
import 'package:aitrainer_app/model/exercise.dart';
import 'package:aitrainer_app/model/exercise_plan_detail.dart';
import 'package:aitrainer_app/model/exercise_type.dart';

class CustomerTrainingPlanDetails {
  /// customerTrainingPlanDetails
  int? customerTrainingPlanDetailsId;

  /// exerciseTypeId
  int? exerciseTypeId;

  /// set
  int? set;

  /// repeats
  int? repeats;

  /// weight
  double? weight;

  int? restingTime;
  bool? parallel;
  String? day;

  /// exerciseType
  ExerciseType? exerciseType;

  ExercisePlanDetailState state = ExercisePlanDetailState.start;

  List<Exercise> exercises = [];

  CustomerTrainingPlanDetails();

  CustomerTrainingPlanDetails.fromJson(Map json) {
    this.customerTrainingPlanDetailsId = json['customerTrainingPlanDetailsId'];
    this.exerciseTypeId = json['exerciseTypeId'];
    this.set = json['set'];
    this.repeats = json['repeats'];
    this.weight = json['weight'];
    this.restingTime = json['restingTime'];
    this.parallel = json['parallel'];
    this.day = json['day'];
  }

  CustomerTrainingPlanDetails.fromJsonWithExerciseList(Map json) {
    this.customerTrainingPlanDetailsId = json['customerTrainingPlanDetailsId'] == "null" || json['customerTrainingPlanDetailsId'] == null
        ? 0
        : json['customerTrainingPlanDetailsId'];
    this.exerciseTypeId = json['exerciseTypeId'];
    this.set = json['set'];
    this.repeats = json['repeats'] == "null" ? -1 : json['repeats'];
    this.weight = json['weight'];
    this.restingTime = json['restingTime'];
    this.parallel = json['parallel'] == "false"
        ? false
        : json['parallel'] == "true"
            ? true
            : null;
    this.day = json['day'].toString();
    if (this.day == null || this.day == "null") {
      this.day = "";
    }
    try {
      Iterable iterable = json['exercises'];
      this.exercises = iterable.map((exercise) => Exercise.fromJson(exercise)).toList();
    } on Exception catch (e) {
      print("JsonDecode error " + e.toString());
    }

    if (json['state'] == ExercisePlanDetailState.finished.toStr()) {
      this.state = ExercisePlanDetailState.finished;
    } else if (json['state'] == ExercisePlanDetailState.inProgress.toStr()) {
      this.state = ExercisePlanDetailState.inProgress;
    } else if (json['state'] == ExercisePlanDetailState.skipped.toStr()) {
      this.state = ExercisePlanDetailState.skipped;
    } else {
      this.state = ExercisePlanDetailState.start;
    }

    this.exerciseType = Cache().getExerciseTypeById(exerciseTypeId!);
  }

  ExerciseType? getExerciseType() => exerciseType;

  Map<String, dynamic> toJson() => {
        "customerTrainingPlanDetailsId": this.customerTrainingPlanDetailsId,
        "exerciseTypeId": this.exerciseTypeId,
        "set": this.set,
        "repeats": this.repeats,
        "weight": this.weight,
        "restingTime": this.restingTime,
        "parallel": this.parallel,
        "day": this.day == null ? '1.' : this.day,
      };

  Map<String, dynamic> toJsonWithExercises() {
    final Map<String, dynamic> jsonMap = {
      //"customerTrainingPlanDetailsId": this.customerTrainingPlanDetailsId,
      "exerciseTypeId": this.exerciseTypeId,
      "set": this.set,
      "repeats": this.repeats,
      "weight": this.weight,
      "restingTime": this.restingTime,
      "parallel": this.parallel,
      'exercises': exercises.isEmpty ? [].toString() : exercises.map((exercise) => exercise.toJson()).toList().toString(),
      'state': this.state.toStr(),
    };
    if (this.day != null && this.day!.isNotEmpty) {
      jsonMap["day"] = this.day;
    }

    //print("Detail $jsonMap");
    return jsonMap;
  }

  @override
  String toString() => this.toJsonWithExercises().toString();
}
