/*
 * @Author: LiOS
 * @Date: 2020-06-16 21:50:01
 * @LastEditors: WuXiangNan
 * @LastEditTime: 2021-02-04 11:49:01
 * @Description:
 */
import 'package:app_package/app_package.dart';
import '../../../src/models/notice_model.dart';
import '../../models/notice_type_model.dart';
import '../notice_service.dart';

class NoticeServiceImpl implements NoticeService {
  static String _prefix = '/app/admin';

  @override
  Future<ResModel<PagingModel<NoticeModel>>> getNoticeList(data) async {
    var res = await HttpUtil.instance.get(
      '$_prefix/notice/list',
      queryParameters: data,
    );

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

  @override
  Future<ResModel<NoticeModel>> getNoticeDetail(String? noticeId) async {
    var res = await HttpUtil.instance.get(
      '$_prefix/notice/$noticeId', //302070016653873152
      queryParameters: {},
    );

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

  @override
  Future<String?> saveToDraft(Map<String, String?> postParams) async {
    var res = await HttpUtil.instance.post(
      '$_prefix/notice/draft',
      data: postParams,
    );

    if (res.data['data'] != null && res.data['data'] is String) {
      return res.data['data'];
    } else {
      return null;
    }
  }

  @override
  Future<String?> editDraft(Map<String, String?> postParams) async {
    var res = await HttpUtil.instance.post(
      '$_prefix/notice/draft/edit',
      data: postParams,
    );

    if (res.data['data'] != null && res.data['data'] is String) {
      return res.data['data'];
    } else {
      return null;
    }
  }

  @override
  Future<String?> publishNotice(String? noticeId) async {
    var res = await HttpUtil.instance.put(
      '$_prefix/notice/pushNow',
      queryParameters: {'id': noticeId},
    );

    if (res.data['data'] != null && res.data['data'] is String) {
      return res.data['data'];
    } else {
      return null;
    }
  }

  Future<ResModel<List<NoticeTypeModel>>> getTypeList() async {
    var res = await HttpUtil.instance.get(
      '/app/admin/notice/getTypeEnums',
    );

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

  noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation);
}
