part of apis;

abstract class CommonAPI {

  /// 1、广告位接口
  ///
  static Future <List<AdModel>> list({
    required String type,
  }) async {
    final result = await HttpService.to.post(
      '/v1/ad/ad/getListAll',
      params: {
        'type': type,
      },
    );
    if (result.data is! Map && result.data['list'] is! List) return [];
    return List.generate(result.data['list'].length, (index){
      return AdModel.fromJson(result.data['list'][index]);
    });
  }

  /// 2、消息接口
  ///
  static Future <List<MsgModel>> msgs({
    int page = 1,
    int limit = 10,
  }) async {
    final result = await HttpService.to.post(
      '/v1/message/Message/getList',
      params: {
        'page': page,
        'page_size': limit,
      },
    );
    if (result.data is! Map && result.data['list'] is! List) return [];
    return List.generate(result.data['list'].length, (index){
      return MsgModel.fromJson(result.data['list'][index]);
    });
  }

  /// 3、消息未读数
  ///
  static Future <int> num() async {
    final result = await HttpService.to.post(
      '/v1/message/Message/getMessageNums',
      params: {},
    );
    if (result.data is! Map ) return 0;
    return result.data['num'];
  }

  /// 4、最近学习
  ///
  static Future <List<StudyHistoryModel>> history({
    int page = 1,
    int limit = 10,
  }) async {
    final result = await HttpService.to.post(
      '/v1/members/Information/myReadRecord',
      params: {
        'page': page,
        'page_size': limit,
      },
    );
    if (result.data is! Map && result.data['list'] is! List) return [];
    return List.generate(result.data['list'].length, (index){
      return StudyHistoryModel.fromJson(result.data['list'][index]);
    });
  }

  /// 5、消息未读变已读
  ///
  static Future <bool> read({
    required String id
  }) async {
    final result = await HttpService.to.post(
      '/v1/message/Message/editOneStatus',
      params: {
        'id':id
      },
    );
    if (result.data is Map && result.data['is_success'] == 1){
      return true;
    }
    return false;
  }

  /// 6、清除搜索记录
  ///
  static Future <bool> clear() async {
    final result = await HttpService.to.post(
      '/v1/book/Information/serachDel',
      params: {},
    );
    if (result.data is Map && result.data['is_success'] == 1){
      return true;
    }
    return false;
  }

  /// 7、搜索历史记录
  ///
  static Future <List<String>> searchRecords() async {
    final result = await HttpService.to.post(
      '/v1/book/Information/serachRecord',
      params: {},
    );
    if (result.data is! Map && result.data['list'] is! List) return [];
    return List.generate(result.data['list'].length, (index){
      return result.data['list'][index]['name'];
    });
  }

  /// 8、搜索书籍
  ///
  static Future <List<CourseModel>> searchBooks({
    int page = 1,
    int limit = 20,
    required String keyWord
  }) async {
    final result = await HttpService.to.post(
      '/v1/book/Information/searchBook',
      params: {
        'page': page,
        'page_size': limit,
        'keyword':keyWord
      },
    );
    if (result.data is! Map && result.data['list'] is! List) return [];
    return List.generate(result.data['list'].length, (index){
      return CourseModel.fromJson(result.data['list'][index]);
    });
  }

  /// 9、收藏 取消收藏
  ///
  static Future <bool> love({
    required String  bookId,
    required String love
  }) async {
    final result = await HttpService.to.post(
      '/v1/book/Information/collection',
      params: {
        'book_id':bookId,
        'is_select':love
      },
    );
    if (result.data is! Map || result.data['is_success'] == 0){
      return false;
    }
    return true;
  }

  /// 10、上传文件
  static Future <String> upload({
    required String path,
    required String fileTypes
  }) async {
    final result = await HttpService.to.upload(
        '/v1/members/Information/uploadFile',
        path: path,
        fileTypes: fileTypes
    );
    if (result.data is! Map) return '';
    return result.data['filesUrl'];
  }

  /// 11、获取OSS临时token
  static Future <OssModel> oss() async {
    final result = await HttpService.to.post(
      '/v1/common/Alioss/getUploadToken',
    );
    if (result.data is! Map ) return OssModel();
    return OssModel.fromJson(result.data);
  }

  static Future<String?> refreshToken() async {

    Console.log('--------refreshToken----------------');
    final result = await HttpService.to.post(
        '/v1/members/login/getToken',
        params: {
          'access_token':StorageService.to.getString(kLocalAccessToken)
        }
    );
    if (result.data is Map) {
      final String token = result.data['token'];
      final String accessToken = result.data['access_token'];
      if(token.isEmpty && accessToken.isEmpty){
        UserStore.to.logout();
        return null;
      }
      await Future.wait([
        UserStore.to.setToken(result.data['token']),
        UserStore.to.setAccessToken(result.data['access_token']),
      ]);
      return result.data['token'];
    }
    // 在这里执行刷新token的逻辑
    // 如果刷新成功，返回新的token；如果刷新失败，返回null
    return null;
  }


}