import 'dart:async';
import 'dart:convert';

import 'package:dio/dio.dart';
import 'package:mnemo_cards/di/injector.dart';
import 'package:mnemo_cards/features/packs/packs_api.dart';
import 'package:mnemo_cards/managers/repository/api.dart';
import 'package:mnemo_cards_common/mnemo_cards_common.dart';


class AdminApi with Api {
  final Dio _dio;

  AdminApi(this._dio);

  Future<bool> addCard(GameCardDto dto) async {
    final r = await _dio.post<String>(
      '$path/cards/add',
      data: jsonEncode(dto.toJson()),
    );
    return r.statusCode == 200;
  }

  Future<bool> deleteCard(GameCardDto dto) async {
    final r = await _dio.post<String>(
      '$path/cards/delete',
      data: jsonEncode({'id': dto.id.toString()}),
    );
    return r.statusCode == 200;
  }

  Future<List<String>> getCardIds() async {
    final r = await _dio.get<String>(
      '$path/cards/ids',
    );
    return (jsonDecode(r.data!)['ids'] as String).split(',').cast();
  }

  Future<List<GameCardDto>> getCards(List<String> ids) async {
    final r = await _dio.get<String>(
      '$path/cards',
      queryParameters: {'ids': ids.join(',')},
    );
    return (r.data as String).decodeList(GameCardDto.fromJson);
  }

  Future<List<UserDto>> getUsers(List<String> ids) async {
    final r = await _dio.get<String>(
      '$path/users',
      queryParameters: {'ids': ids.join(',')},
    );
    return (jsonDecode(r.data!) as List)
        .map((e) => UserDto.fromJson(e as Map<String, dynamic>))
        .toList()
        .cast();
  }

  Future<List<String>> getUserIds() async {
    final r = await _dio.get<String>(
      '$path/users/ids',
    );
    return (jsonDecode(r.data!)['ids'] as String).split(',').cast();
  }

  Future<List<PaymentDto>> getUserPurchases(String userId) async {
    final r = await _dio
        .get<String>('$path/user/purchases', queryParameters: {'id': userId});
    return (jsonDecode(r.data!)['payments'] as List)
        .cast<Map<String, Object?>>()
        .map(PaymentDto.fromJson)
        .toList();
  }

  Future<bool> deleteUser(UserDto dto) async {
    final r = await _dio.post<String>(
      '$path/users/delete',
      data: jsonEncode({'id': dto.id.toString()}),
    );
    return r.statusCode == 200;
  }

  Future<bool> editUser(UserDto dto) async {
    final r = await _dio.post<String>(
      '$path/users/add',
      data: jsonEncode(dto.toJson()),
    );
    return r.statusCode == 200;
  }

  Future<EditCardPackDto?> getEditPack(String id) async {
    final r = await _dio.get<String>(
      '$path/pack/edit/$id',
    );
    return (r.data as String).decode(EditCardPackDto.fromJson);
  }

  Future<bool> deletePack(EditCardPackDto dto) async {
    final r = await _dio.post<String>(
      '$path/pack/delete',
      data: jsonEncode({'id': dto.id.toString()}),
    );
    return r.statusCode == 200;
  }

  Future<bool> editPack(EditCardPackDto dto) async {
    final r = await _dio.post<String>(
      '$path/pack/edit',
      data: jsonEncode(dto.toJson()),
    );
    return r.statusCode == 200;
  }

  Future<List<CardPackPreviewDto>> packs() =>
      getIt.get<PacksApi>().getPacksPreviews(null);
}
