import 'dart:typed_data';

import 'package:dio/dio.dart';

import 'package:taafee_mobile/core/apis/apis.dart';
import 'package:taafee_mobile/core/local_storage/local_storage.dart';

import 'package:taafee_mobile/core/network/http.dart';
import 'package:taafee_mobile/features/card/data_layer/model/add_card.dart';
import 'package:taafee_mobile/features/card/data_layer/model/card_images.dart';

import '../model/card_model.dart';
import '../model/edit_card.dart';

class CardService {
  LocalStorage localStorage = LocalStorage();
  Future<void> addCard(AddCardModel addCardModel) async {
    Request request = Request(EndPoint.card, RequestMethod.post,
        body: addCardModel.toJson(), isFormData: true, authorized: true);
    //no need to use variable
    await request.sendRequest();
  }

  Future<List<CardModel>> getCards(
      {int? page,
      int? categoryId,
      void Function(Object)? onConnectionError}) async {
    Request request = Request(EndPoint.card, RequestMethod.get,
        authorized: true,
        cacheable: true,
        queryParams: {
          "page": page,
          if (categoryId != null) 'category_id': categoryId
        });

    Map<String, dynamic> response =
        await request.sendRequest(onConnectionError: onConnectionError);

    return CardModel.fromJsonList(response);
  }

  Future<CardModel> showCard({required int cardId}) async {
    Request request = Request('${EndPoint.card}/$cardId', RequestMethod.get,
        authorized: true);
    Map<String, dynamic> response = await request.sendRequest();
    return CardModel.fromJson(response['data']);
  }

  Future<void> addToFavorite(int id) async {
    Request request = Request(
      EndPoint.addToFavorite,
      RequestMethod.post,
      authorized: true,
      body: {
        "card_id": id,
      },
    );
    //no need to use variable

    await request.sendRequest();
  }

  Future<void> removeFromFavorite(int id) async {
    Request request = Request(
      EndPoint.removeFromFavorite(id),
      RequestMethod.delete,
      authorized: true,
    );
    //no need to use variable

    await request.sendRequest();
  }

  Future<List<CardModel>> getMyCards(int userId,
      {int? page, void Function(Object)? onConnectionError}) async {
    Request request = Request(EndPoint.card, RequestMethod.get,
        cacheable: true,
        authorized: true,
        queryParams: {"user_id": userId, 'page': page});

    Map<String, dynamic> response =
        await request.sendRequest(onConnectionError: onConnectionError);

    return CardModel.fromJsonList(response);
  }

  Future<void> editCard(int cardId, EditCardModel editCardModel) async {
    Request request = Request(
      EndPoint.editCard(cardId),
      RequestMethod.put,
      body: editCardModel.toJson(),
      authorized: true,
    );
    await request.sendRequest();
  }

  /// -------------add images------------ ///
  Future<void> addImagesToCard(EditCardModel editCardModel) async {
    Request request = Request(
      EndPoint.editCardImages,
      RequestMethod.post,
      authorized: true,
      body: editCardModel.imagesAddingData(),
      isFormData: true,
    );
    await request.sendRequest();
  }

  /// ----------delete image --------------///
  Future<void> deleteImage(int imageId) async {
    Request request = Request(
      EndPoint.deleteCardImage(imageId),
      RequestMethod.delete,
      authorized: true,
    );
    await request.sendRequest();
  }

  Future<List<CardImages>> getCardImages(int cardId) async {
    Request request = Request(
      EndPoint.editCard(cardId),
      RequestMethod.get,
      authorized: true,
    );
    Map<String, dynamic> response = await request.sendRequest();

    return CardImages.fromJsonList(response['data']);
  }

  /// ------------delete card ------------///
  Future<void> deleteCard(int cardId) async {
    Request request = Request(
      EndPoint.editCard(cardId),
      RequestMethod.delete,
      authorized: true,
    );
    await request.sendRequest();
  }

  ///----------- save images to gallery -------///
  Future<Uint8List> downloadImage(String imageUrl) async {
    var response = await Dio().get(imageUrl,
        options: Options(
          responseType: ResponseType.bytes,
          headers: {
            "Authorization": localStorage.getChatToken(),
          },
        ));
    return Uint8List.fromList(response.data);
  }
}
