import 'package:didvan/models/requests/news.dart';
import 'package:didvan/models/requests/radar.dart';
import 'package:didvan/models/requests/studio.dart';

class RequestHelper {
  static const String baseUrl = 'https://api.didvan.app';
  static const String _baseUserUrl = baseUrl + '/user';
  static const String _baseRadarUrl = baseUrl + '/radar';
  static const String _baseNewsUrl = baseUrl + '/news';
  static const String _baseStudioUrl = baseUrl + '/studio';
  static const String _baseDirectUrl = _baseUserUrl + '/direct';

  static const String confirmUsername = _baseUserUrl + '/confirmUsername';
  static const String login = _baseUserUrl + '/login';
  static const String directs = _baseUserUrl + '/direct';
  static const String userInfo = _baseUserUrl + '/info';
  static const String updateProfilePhoto = _baseUserUrl + '/profile/photo';
  static const String checkUsername = _baseUserUrl + '/CheckUsername';
  static const String updateProfile = _baseUserUrl + '/profile/edit';
  static const String otp = _baseUserUrl + '/otp';
  static String bookmarks({String? type}) =>
      _baseUserUrl + '/marked/${type ?? ''}';

  static const String directTypes = baseUrl + '/direct/types';
  static String direct(int id) => _baseDirectUrl + '/$id';
  static String sendDirectMessage(int id) =>
      _baseDirectUrl + '/$id/sendMessage';
  static String tag({
    required List<int> ids,
    String? type,
    int? itemId,
    int? page,
    int? limit,
  }) =>
      baseUrl +
      '/tag' +
      _urlConcatGenerator([
        MapEntry('limit', limit?.toString() ?? '3'),
        MapEntry('type', type),
        MapEntry('id', itemId?.toString() ?? '1'),
        MapEntry('tags', _urlListConcatGenerator(ids))
      ]);

  static String markRadar(int id) => _baseRadarUrl + '/$id/mark';
  static String radarComments(int id) => _baseRadarUrl + '/$id/comments';
  static String addRadarComment(int id) => _baseRadarUrl + '/$id/comments/add';
  static String feedbackRadarComment(int radarId, int id) =>
      _baseRadarUrl + '/$radarId/comments/$id/feedback';
  static String radarDetails(int id, RadarRequestArgs args) =>
      _baseRadarUrl +
      '/$id' +
      _urlConcatGenerator([
        MapEntry('page', args.page.toString()),
        MapEntry('start', args.startDate),
        MapEntry('end', args.endDate),
        MapEntry('search', args.search),
        MapEntry('categories', _urlListConcatGenerator(args.categories)),
      ]);
  static String radarOverviews({required RadarRequestArgs args}) =>
      _baseRadarUrl +
      _urlConcatGenerator([
        MapEntry('page', args.page.toString()),
        MapEntry('start', args.startDate),
        MapEntry('end', args.endDate),
        MapEntry('search', args.search),
        MapEntry('categories', _urlListConcatGenerator(args.categories)),
      ]);

  static String markNews(int id) => _baseNewsUrl + '/$id/mark';
  static String newsComments(int id) => _baseNewsUrl + '/$id/comments';
  static String addNewsComment(int id) => _baseNewsUrl + '/$id/comments/add';
  static String feedbackNewsComment(int radarId, int id) =>
      _baseNewsUrl + '/$radarId/comments/$id/feedback';
  static String newsDetails(int id, NewsRequestArgs args) =>
      _baseNewsUrl +
      '/$id' +
      _urlConcatGenerator([
        MapEntry('page', args.page.toString()),
        MapEntry('start', args.startDate),
        MapEntry('end', args.endDate),
        MapEntry('search', args.search),
      ]);
  static String newsOverviews({required NewsRequestArgs args}) =>
      _baseNewsUrl +
      _urlConcatGenerator([
        MapEntry('page', args.page.toString()),
        MapEntry('start', args.startDate),
        MapEntry('end', args.endDate),
        MapEntry('search', args.search),
      ]);

  static String markStudioItem(int id) => _baseStudioUrl + '/$id/mark';
  static String studioItemComments(int id) => _baseStudioUrl + '/$id/comments';
  static String addStudioItemComment(int id) =>
      _baseStudioUrl + '/$id/comments/add';
  static String feedbackStudioItemComment(int radarId, int id) =>
      _baseStudioUrl + '/$radarId/comments/$id/feedback';
  static String studioItemDetails(int id, StudioRequestArgs args) =>
      _baseStudioUrl +
      '/$id' +
      _urlConcatGenerator([
        MapEntry('page', args.page.toString()),
        MapEntry('search', args.search),
        MapEntry('order', args.order),
        MapEntry('type', args.type),
      ]);
  static String studioItemOverviews({required StudioRequestArgs args}) =>
      _baseStudioUrl +
      _urlConcatGenerator([
        MapEntry('page', args.page.toString()),
        MapEntry('search', args.search),
        MapEntry('order', args.order),
        MapEntry('type', args.type),
      ]);

  static String _urlConcatGenerator(List<MapEntry<String, String?>> additions) {
    String result = '';
    additions.removeWhere((element) => element.value == null);
    if (additions.isNotEmpty) {
      result += '?';
      for (var i = 0; i < additions.length; i++) {
        result += (additions[i].key + '=' + additions[i].value!);
        if (i != additions.length - 1) {
          result += '&';
        }
      }
    }
    return result;
  }

  static String? _urlListConcatGenerator(List? input) {
    String? result;
    if (input == null) return null;
    if (input.isNotEmpty) {
      result = '';
      for (var i = 0; i < input.length; i++) {
        result = result! + input[i].toString();
        if (i != input.length - 1) {
          result += ',';
        }
      }
      return result;
    }
    return null;
  }
}
