import 'dart:convert';

class RecipeDetail {
  int id;
  String title;
  String imageUrl;
  int time;
  int servings;
  String difficulty;
  String sourceUrl;
  Rating rating;
  bool isFavorited;
  List<Ingredient> ingredients;
  List<String> instructions;
  List<Ingredient> ownedIngredients;
  List<Ingredient> notOwnedIngredients;

  RecipeDetail({
    required this.id,
    required this.title,
    required this.imageUrl,
    required this.time,
    required this.servings,
    required this.difficulty,
    required this.sourceUrl,
    required this.rating,
    required this.isFavorited,
    required this.ingredients,
    required this.instructions,
    required this.ownedIngredients,
    required this.notOwnedIngredients,
  });

  factory RecipeDetail.fromJson(Map<String, dynamic> json) => RecipeDetail(
        id: json["id"],
        title: json["title"],
        imageUrl: json["image_url"],
        time: json["time"],
        servings: json["servings"],
        difficulty: json["difficulty"],
        sourceUrl: json["source_url"],
        rating: Rating.fromJson(json["rating"]),
        isFavorited: json["is_favorited"],
        ingredients: List<Ingredient>.from(
            json["ingredients"].map((x) => Ingredient.fromJson(x))),
        instructions: List<String>.from(json["instructions"].map((x) => x)),
        ownedIngredients: List<Ingredient>.from(
            json["owned_ingredients"].map((x) => Ingredient.fromJson(x))),
        notOwnedIngredients: List<Ingredient>.from(
            json["not_owned_ingredients"].map((x) => Ingredient.fromJson(x))),
      );

  Map<String, dynamic> toJson() => {
        "id": id,
        "title": title,
        "image_url": imageUrl,
        "time": time,
        "servings": servings,
        "difficulty": difficulty,
        "source_url": sourceUrl,
        "rating": rating.toJson(),
        "is_favorited": isFavorited,
        "ingredients": List<dynamic>.from(ingredients.map((x) => x.toJson())),
        "instructions": List<dynamic>.from(instructions.map((x) => x)),
        "owned_ingredients":
            List<dynamic>.from(ownedIngredients.map((x) => x.toJson())),
        "not_owned_ingredients":
            List<dynamic>.from(notOwnedIngredients.map((x) => x.toJson())),
      };
}

class Ingredient {
  int id;
  String name;
  double amount;
  String state;
  int recipe;
  String unit;

  Ingredient({
    required this.id,
    required this.name,
    required this.amount,
    required this.state,
    required this.recipe,
    required this.unit,
  });

  factory Ingredient.fromJson(Map<String, dynamic> json) => Ingredient(
        id: json["id"],
        name: json["name"],
        amount: json["amount"],
        state: json["state"],
        recipe: json["recipe"],
        unit: json["unit"] ?? "",
      );

  Map<String, dynamic> toJson() => {
        "id": id,
        "name": name,
        "amount": amount,
        "state": state,
        "recipe": recipe,
        "unit": unit,
      };
}

class Rating {
  double score;
  int amount;

  Rating({
    required this.score,
    required this.amount,
  });

  factory Rating.fromJson(Map<String, dynamic> json) => Rating(
        score: json["score"],
        amount: json["amount"],
      );

  Map<String, dynamic> toJson() => {
        "score": score,
        "amount": amount,
      };
}

class EnumValues<T> {
  Map<String, T> map;
  late Map<T, String> reverseMap;

  EnumValues(this.map);

  Map<T, String> get reverse {
    reverseMap = map.map((k, v) => MapEntry(v, k));
    return reverseMap;
  }
}
