import 'dart:convert';

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

enum ExercisePlanDetailState { start, inProgress, skipped, finished }

extension ExericisePlanDetailStateExt on ExercisePlanDetailState {
  bool equalsTo(ExercisePlanDetailState state) => this.toString() == state.toString();
  bool equalsStringTo(String state) => this.toString() == state;
  String toStr() => this.toString().split(".").last;
}

class ExercisePlanDetail {
  int? exercisePlanDetailId;
  int? exercisePlanId;
  late int exerciseTypeId;
  int? serie;
  int? repeats;
  String? weightEquation;

  /// List<Exercise>
  List<Exercise>? exercises;

  /// bool finished
  bool? finished;
  ExercisePlanDetailState state = ExercisePlanDetailState.start;

  ExerciseType? exerciseType;

  String? change; // 1: update -1:delete 0: new

  ExercisePlanDetail(int exerciseTypeId) {
    this.exerciseTypeId = exerciseTypeId;
  }

  ExercisePlanDetail.fromJson(Map json) {
    this.exercisePlanDetailId = json['exercisePlanDetailId'];
    this.exercisePlanId = json['exercisePlanId'];
    this.exerciseTypeId = json['exerciseTypeId'];
    this.serie = json['serie'];
    this.repeats = json['repeats'];
    this.weightEquation = json['weightEquation'];
  }

  ExercisePlanDetail.fromJsonWithExerciseList(Map json) {
    this.exercisePlanDetailId = json['exercisePlanDetailId'];
    this.exercisePlanId = json['exercisePlanId'];
    this.exerciseTypeId = json['exerciseTypeId'];
    this.serie = json['serie'];
    this.repeats = json['repeats'];
    this.weightEquation = json['weightEquation'];
    try {
      final String exercises = json['exercises'];
      String jsonExercises = exercises.replaceAllMapped(
          RegExp(r'([a-zA-Z]+|[0-9]{4}\-[0-9]{2}\-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2})'), (Match m) => "\"${m[0]}\"");

      jsonExercises = jsonExercises.replaceAll(r'\"null\"', 'null');

      //print("Exercises $jsonExercises");
      Iterable iterable = jsonDecode(jsonExercises);
      this.exercises = iterable.map((exercise) => Exercise.fromJson(exercise)).toList();
    } on Exception catch (e) {
      print("JsonDecode error " + e.toString());
    }
  }

  Map<String, dynamic> toJson() => {
        "exercisePlanId": exercisePlanId == null ? 0 : exercisePlanId,
        "exerciseTypeId": exerciseTypeId,
        "serie": serie,
        "repeats": repeats,
        "weightEquation": weightEquation
      };

  Map<String, dynamic> toJsonWithExerciseList() => {
        "exercisePlanDetailId": exercisePlanDetailId,
        "exercisePlanId": exercisePlanId,
        "exerciseTypeId": exerciseTypeId,
        "serie": serie,
        "repeats": repeats,
        "weightEquation": weightEquation,
        'exercises': exercises == null ? [].toString() : exercises!.map((exercise) => exercise.toJson()).toList().toString(),
      };

  @override
  String toString() {
    Map<String, dynamic> json = toJsonWithExerciseList();
    return json.toString();
  }
}
