import '../../../core/api/api_client.dart';
import '../../../core/api/api_exception.dart';
import '../../../core/api/api_paths.dart';
import '../domain/parsed_recipe.dart';
import '../domain/recipe.dart';
import '../domain/inventory_preview.dart';

class RecipeRepository {
  final ApiClient _api;

  RecipeRepository(this._api);

  Future<List<Recipe>> fetchRecipes({String? token}) async {
    try {
      final dynamic data = await _api.getJson(RecipeApiPaths.list, token: token);
      if (data is! List) {
        throw const ApiException(
            type: ApiErrorType.unknown, message: 'Ogiltigt svar från servern.');
      }
      return data
          .map((e) => Recipe.fromJson(e as Map<String, dynamic>))
          .toList();
    } on ApiException {
      rethrow;
    } catch (_) {
      throw const ApiException(
          type: ApiErrorType.network, message: 'Kunde inte hämta recept.');
    }
  }

  Future<Recipe> fetchRecipeDetail(int id, {String? token}) async {
    try {
      final data = await _api.getJson(RecipeApiPaths.detail(id), token: token);
      if (data is! Map<String, dynamic>) {
        throw const ApiException(
            type: ApiErrorType.unknown, message: 'Ogiltigt svar från servern.');
      }
      return Recipe.fromJson(data);
    } on ApiException {
      rethrow;
    } catch (_) {
      throw const ApiException(
          type: ApiErrorType.network, message: 'Kunde inte hämta recept.');
    }
  }

  Future<Recipe> createRecipe(Map<String, dynamic> body,
      {String? token}) async {
    try {
        final data =
          await _api.postJson(RecipeApiPaths.list, body: body, token: token);
      if (data is! Map<String, dynamic>) {
        throw const ApiException(
            type: ApiErrorType.unknown, message: 'Ogiltigt svar från servern.');
      }
      return Recipe.fromJson(data);
    } on ApiException {
      rethrow;
    } catch (_) {
      throw const ApiException(
          type: ApiErrorType.network, message: 'Kunde inte spara recept.');
    }
  }

  Future<Recipe> updateRecipe(int id, Map<String, dynamic> body,
      {String? token}) async {
    try {
      final data = await _api.patchJson(
        RecipeApiPaths.update(id),
        body: body,
        token: token,
      );
      if (data is! Map<String, dynamic>) {
        throw const ApiException(
            type: ApiErrorType.unknown, message: 'Ogiltigt svar från servern.');
      }
      return Recipe.fromJson(data);
    } on ApiException {
      rethrow;
    } catch (_) {
      throw const ApiException(
          type: ApiErrorType.network, message: 'Kunde inte uppdatera recept.');
    }
  }

  Future<void> deleteRecipe(int id, {String? token}) async {
    try {
      await _api.deleteJson(RecipeApiPaths.remove(id), token: token);
    } on ApiException {
      rethrow;
    } catch (_) {
      throw const ApiException(
          type: ApiErrorType.network, message: 'Kunde inte ta bort recept.');
    }
  }

  Future<Recipe> setRecipeVisibility(int id, {required bool isPublic, String? token}) async {
    try {
      final data = await _api.patchJson(
        RecipeApiPaths.setVisibility(id),
        body: {'isPublic': isPublic},
        token: token,
      );
      if (data is! Map<String, dynamic>) {
        throw const ApiException(
            type: ApiErrorType.unknown, message: 'Ogiltigt svar från servern.');
      }
      return Recipe.fromJson(data);
    } on ApiException {
      rethrow;
    } catch (_) {
      throw const ApiException(
          type: ApiErrorType.network, message: 'Kunde inte uppdatera synlighet.');
    }
  }

  Future<Recipe> shareRecipeWithUsername(int id, {required String username, String? token}) async {
    try {
      final data = await _api.postJson(
        RecipeApiPaths.share(id),
        body: {'username': username},
        token: token,
      );
      if (data is! Map<String, dynamic>) {
        throw const ApiException(
            type: ApiErrorType.unknown, message: 'Ogiltigt svar från servern.');
      }
      return Recipe.fromJson(data);
    } on ApiException {
      rethrow;
    } catch (_) {
      throw const ApiException(
          type: ApiErrorType.network, message: 'Kunde inte dela receptet.');
    }
  }

  Future<Recipe> unshareRecipeWithUsername(int id, {required String username, String? token}) async {
    try {
      final data = await _api.deleteJson(
        RecipeApiPaths.unshare(id, username),
        token: token,
      );
      if (data is! Map<String, dynamic>) {
        throw const ApiException(
            type: ApiErrorType.unknown, message: 'Ogiltigt svar från servern.');
      }
      return Recipe.fromJson(data);
    } on ApiException {
      rethrow;
    } catch (_) {
      throw const ApiException(
          type: ApiErrorType.network, message: 'Kunde inte ta bort delning.');
    }
  }

  Future<InventoryPreview> fetchInventoryPreview(int id,
      {String? token}) async {
    try {
      final data = await _api.getJson(
        RecipeApiPaths.inventoryPreview(id),
        token: token,
      );
      if (data is! Map<String, dynamic>) {
        throw const ApiException(
            type: ApiErrorType.unknown, message: 'Ogiltigt svar från servern.');
      }
      return InventoryPreview.fromJson(data);
    } on ApiException {
      rethrow;
    } catch (_) {
      throw const ApiException(
          type: ApiErrorType.network,
          message: 'Kunde inte hämta inventariestatus.');
    }
  }

  Future<ParsedRecipe> parseMarkdown(String markdown,
      {String? token}) async {
    try {
      final data = await _api.postJson(
        RecipeApiPaths.parseMarkdown,
        body: {'markdown': markdown},
        token: token,
      );
      if (data is! Map<String, dynamic>) {
        throw const ApiException(
            type: ApiErrorType.unknown, message: 'Ogiltigt svar från servern.');
      }
      return ParsedRecipe.fromJson(data);
    } on ApiException {
      rethrow;
    } catch (_) {
      throw const ApiException(
          type: ApiErrorType.network, message: 'Kunde inte tolka receptet.');
    }
  }
}
