import 'package:news_getx/config/storage.dart';
import 'package:news_getx/data/model/categories.dart';
import 'package:news_getx/data/model/channels.dart';
import 'package:news_getx/data/model/news.dart';
import 'package:news_getx/data/model/tag.dart';
import 'package:news_getx/utils/http.dart';

/// 新闻API
class NewsAPI {
  /// 翻页
  /// refresh 是否刷新
  static Future<NewsPageListResponse> newsPageList({
    NewsPageListRequest? params,
    bool refresh = false,
    bool cacheDisk = false,
  }) async {
    var response = await HttpUtil().get(
      "/news",
      queryParameters: params?.toJson(),
      refresh: refresh,
      cacheDisk: cacheDisk,
      cacheKey: StorageIndexNewsCacheKey,
    );
    return NewsPageListResponse.fromJson(response);
  }

  /// 推荐
  static Future<NewsItem> newsRecommend({
    NewsRecommendRequest? params,
    bool refresh = false,
    bool cacheDisk = false,
  }) async {
    var response = await HttpUtil().get(
      '/news/recommend',
      queryParameters: params?.toJson(),
      refresh: refresh,
      cacheDisk: cacheDisk,
    );
    return NewsItem.fromJson(response);
  }

  /// 分类
  static Future<List<CategoryResponse>> categories({
    bool cacheDisk = false,
  }) async {
    var response = await HttpUtil().get(
      '/categories',
      cacheDisk: cacheDisk,
    );
    return response
        .map<CategoryResponse>((item) => CategoryResponse.fromJson(item))
        .toList();
  }

  /// 频道
  static Future<List<ChannelResponse>> channels({
    bool cacheDisk = false,
  }) async {
    var response = await HttpUtil().get(
      '/channels',
      cacheDisk: cacheDisk,
    );
    return response
        .map<ChannelResponse>((item) => ChannelResponse.fromJson(item))
        .toList();
  }

  /// 标签列表
  static Future<List<TagResponse>> tags({
    TagRequest? params,
    bool cacheDisk = false,
  }) async {
    var response = await HttpUtil().get(
      '/tags',
      queryParameters: params?.toJson(),
      cacheDisk: cacheDisk,
    );

    return response
        .map<TagResponse>((item) => TagResponse.fromJson(item))
        .toList();
  }
}
