/*
 * @Author       : WuWei
 * @LastEditors  : WuWei
 * @Date         : 2022-01-19 16:44:34
 * @LastEditTime : 2023-05-16 09:37:20
 * @Description  : Do not edit
 */

import 'package:app_package/app_package.dart';
import 'package:app_package/src/models/message_count.dart';
import 'package:app_package/src/models/message_index_list.dart';
import 'package:app_package/src/models/msg_count.dart';
import 'package:app_package/src/models/msg_item.dart';

class MessageApi {
  static var _prefix = '/app/admin/message';

  ///消息列表
  static Future<ResModel<PagingModel<MsgItemModel>>> getMsgList(data) async {
    var res = await HttpUtil.instance.get(
      '$_prefix/page',
      queryParameters: data,
    );

    return ResModel<PagingModel<MsgItemModel>>(
      json: res.data,
      to: (v) => v.toJson(),
      from: (v) => PagingModel(
        json: v,
        to: (v) => v.toJson(),
        from: (v) => MsgItemModel.fromJson(v),
      ),
    );
  }

  ///未读数目
  static Future<ResModel<List<MsgCountModel>?>> getMsgCount() async {
    var res = await HttpUtil.instance.get(
      '$_prefix/v1/unread',
    );
    return ResModel<List<MsgCountModel>?>(
      json: res.data,
      to: (v) => (v ?? []).map((f) => f.toJson()).toList(),
      from: (v) => (v ?? [])
          .map<MsgCountModel>((f) => MsgCountModel.fromJson(f))
          .toList(),
    );
  }

  /// 全部标记为已读
  ///  http://yapi.wisdomcity.com.cn/project/16/interface/api/3195

  static Future<ResModel<int>> putReadMessage() async {
    var res = await HttpUtil.instance.put(
      '/app/admin/message/allMarkAsRead',
    );

    return ResModel<int>(
      json: res.data,
      to: (v) => v,
      from: (v) => v,
    );
  }

  /// 消息提醒首页分组消息数
  /// http://yapi.wisdomcity.com.cn/project/16/interface/api/21158
  static Future<ResModel<List<MessageCountModel>>> getMsgGroupCount() async {
    var res = await HttpUtil.instance.get(
      '/app/admin/message/home/group/count',
    );

    return ResModel<List<MessageCountModel>>(
      json: res.data,
      to: (v) => (v).map((f) => f.toJson()).toList(),
      from: (v) => (v ?? [])
          .map<MessageCountModel>((f) => MessageCountModel.fromJson(f))
          .toList(),
    );
  }

  /// 消息提醒首页
  /// http://yapi.wisdomcity.com.cn/project/16/interface/api/21153
  static Future<ResModel<List<MessageIndexListModel>>>
      getMessageIndexList() async {
    var res = await HttpUtil.instance.get(
      '/app/admin/message/home',
    );

    return ResModel<List<MessageIndexListModel>>(
      json: res.data,
      to: (v) => (v).map((f) => f.toJson()).toList(),
      from: (v) => (v ?? [])
          .map<MessageIndexListModel>((f) => MessageIndexListModel.fromJson(f))
          .toList(),
    );
  }

  /// 单条消息已读
  static Future<ResModel<int>> putSingleMessageRead(data) async {
    var res = await HttpUtil.instance.put(
      '/app/admin/message/markAsRead?id=${data['id']}',
    );
    return ResModel<int>(
      json: res.data,
      to: (v) => v,
      from: (v) => v,
    );
  }

  /// 按分组消息已读
  static Future<ResModel<int>> putGroupRead(data) async {
    var res = await HttpUtil.instance.put(
      '/app/admin/message/groud/read',
      data: data,
    );

    return ResModel<int>(
      json: res.data,
      to: (v) => v,
      from: (v) => v,
    );
  }
}
