/*
 * @Author       : WuWei
 * @LastEditors  : WuWei
 * @Date         : 2022-01-19 16:44:35
 * @LastEditTime : 2023-10-25 15:51:32
 * @Description  : Do not edit
 */
import 'package:app_package/app_package.dart';
import 'package:dynamic_package/src/models/dynamic_item.dart';
import 'package:wisdom_cli/wisdom_cli.dart';

import '../models/dynamic_detail.dart';

class DynamicServiceImpl {
  /// http://yapi.wisdomcity.com.cn/project/16/interface/api/cat_236
  /// 服务动态列表
  Future<ResModel<PagingModel<DynamicItemModel>>> getDynamicList(data) async {
    var res = await HttpUtil.instance.get(
      '/app/admin/communityMessage',
      queryParameters: data,
    );

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

  /// 草稿箱列表
  Future<ResModel<PagingModel<DynamicItemModel>>> getDynamicDraftList(
      data) async {
    var res = await HttpUtil.instance.get(
      '/app/admin/communityMessage/draft/list',
      queryParameters: data,
    );

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

  ///服务动态草稿新增
  Future<ResModel<int>> postDynamicTextAdd(data) async {
    var res = await HttpUtil.instance.post(
      '/app/admin/communityMessage/draft/add',
      data: data,
    );

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

  ///服务动态新增
  Future<ResModel<int?>> postDynamicAdd(data) async {
    var res = await HttpUtil.instance.post(
      '/app/admin/communityMessage/add/push',
      data: data,
    );

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

  ///服务动态草稿编辑
  Future<ResModel<int>> postDynamicTextEdit(data) async {
    var res = await HttpUtil.instance.post(
      '/app/admin/communityMessage/draft/edit',
      data: data,
    );

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

  /// 服务动态草稿发布
  Future<ResModel<int?>> postDynamicDraftpush(data) async {
    var res = await HttpUtil.instance.post(
      '/app/admin/communityMessage/draft/push',
      data: data,
    );

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

  ///服务动态编辑
  Future<ResModel<int?>> postDynamicEdit(data) async {
    var res = await HttpUtil.instance.post(
      '/app/admin/communityMessage/draft/edit',
      data: data,
    );

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

  ///服务动态删除
  Future<ResModel<int?>> deleteDynamic(String? id) async {
    var res = await HttpUtil.instance.delete(
      '/app/admin/communityMessage/$id',
    );

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

  ///服务动态撤回
  /// http://yapi.wisdomcity.com.cn/project/16/interface/api/22403
  Future<ResModel<int?>> undoDynamic(String id) async {
    var res = await HttpUtil.instance.post(
      '/app/admin/communityMessage/$id/withdraw',
    );

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

  /// 服务动态详情
  /// http://yapi.wisdomcity.com.cn/project/16/interface/api/5881
  Future<ResModel<DynamicDetailModel>> getDynamicDetail(data) async {
    var res = await HttpUtil.instance.get(
      '/app/admin/communityMessage/detail',
      queryParameters: data,
    );

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

  /// 获取类型列表
  Future<ResModel<List<GlobalIdToNameModel>>> getClassIdList() async {
    var res = await HttpUtil.instance.get(
      '/app/admin/communityMessage/class',
    );

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