import 'dart:collection';

import 'package:aitrainer_app/model/training_plan_detail.dart';

class TrainingPlan {
  late int trainingPlanId;
  late String type;
  late String name;
  late String internalName;
  late String description;
  late bool free;
  int? treeId;

  HashMap<String, String> nameTranslations = HashMap();
  HashMap<String, String> descriptionTranslations = HashMap();

  List<TrainingPlanDetail>? details;

  TrainingPlan.fromJson(Map<String, dynamic> json) {
    this.trainingPlanId = json['trainingPlanId'];
    this.name = json['name'];
    this.type = json['type'];
    this.internalName = json['internalName'];
    this.description = json['description'];
    this.free = json['free'];
    this.treeId = json['treeId'];

    nameTranslations['en'] = name;
    descriptionTranslations['en'] = description;
    if (json['translations'] != null && json['translations'].length > 0) {
      json['translations'].forEach((translation) {
        nameTranslations[translation['languageCode']] = translation['nameTranslation'];
        descriptionTranslations[translation['languageCode']] = translation['descriptionTranslation'];
      });
    }

    if (json['details'] != null && json['details'].length > 0) {
      details = json['details'].map<TrainingPlanDetail>((detail) => TrainingPlanDetail.fromJson(detail)).toList();
      if (details != null && details!.isNotEmpty) {
        details!.sort((a, b) {
          if (a.sort == 0 || b.sort == 0) {
            if (a.trainingPlanDetailId <= b.trainingPlanDetailId) {
              return -1;
            } else {
              return 1;
            }
          }
          if (a.sort <= b.sort) {
            return -1;
          } else {
            return 1;
          }
        });
      }
    }
  }

  Map<String, dynamic> toJson() => {
        "trainingPlanId": this.trainingPlanId,
        "treeId": this.treeId,
        "name": this.name,
        "type": this.type,
        "internalName": this.internalName,
        "free": this.free,
        "description": this.description,
        "nameTranslation": this.nameTranslations.toString(),
      };

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