/*
 * @Author: XianKaiQun
 * @Date: 2020-09-03 11:23:15
 * @LastEditors: wangjiacheng
 * @LastEditTime: 2021-11-03 14:27:44
 * @Description: 
 */
import 'package:app_package/app_package.dart';
import 'package:face_package/src/models/cascade.dart';
import 'package:face_package/src/models/door_group.dart';
import 'package:face_package/src/models/face_detail.dart';
import 'package:face_package/src/models/device_detail.dart';
import '../face_audit_service.dart';
import '../../models/face_audit_list_model.dart';
import '../../models/pass_card.dart';

class FaceAuditServiceImpl implements FaceAuditService {
  static String prefix = '/app/admin';

  /// 允许通行
  /// http://yapi.wisdomcity.com.cn/project/16/interface/api/4133
  @override
  Future<int?> putFaceAuditAllow(data) async {
    var res = await HttpUtil.instance.put(
      '$prefix/faceAuditRecord/allow',
      data: data,
    );

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

  /// 拒绝通行
  /// http://yapi.wisdomcity.com.cn/project/16/interface/api/4014
  @override
  Future<int?> putFaceAuditRefuse(data) async {
    var res = await HttpUtil.instance.put(
      '$prefix/faceAuditRecord/refuse',
      data: data,
    );
    if (res.data['data'] != null && res.data['data'] is int) {
      return res.data['data'];
    } else {
      return null;
    }
  }

  ///
  /// 人脸审核列表
  /// http://yapi.wisdomcity.com.cn/project/16/interface/api/4000
  Future<ResModel<PagingModel<FaceAuditRecordModel>>>
      getAppAdminFaceAuditRecordList(data) async {
    var res = await HttpUtil.instance.get(
      '$prefix/faceAuditRecord/list',
      queryParameters: data,
    );

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

  ///人脸详情
  /// http://yapi.wisdomcity.com.cn/project/16/interface/api/4007
  Future<ResModel<FaceDetailModel>> getFaceDetail(id) async {
    var res = await HttpUtil.instance.get(
      '$prefix/faceAuditRecord/$id',
    );
    return ResModel<FaceDetailModel>(
      json: res.data,
      to: (v) => v.toJson(),
      from: (v) => FaceDetailModel.fromJson(v),
    );
  }

  ///门禁列表
  ///http://yapi.wisdomcity.com.cn/project/16/interface/api/3979

  Future<ResModel<List<DoorGroupModel>?>> getdoorGroupList(communityId) async {
    var res = await HttpUtil.instance.get(
      '/app/admin/faceAuditRecord/doorGroupList/$communityId',
    );
    return ResModel<List<DoorGroupModel>?>(
      json: res.data,
      to: (v) => (v ?? []).map((f) => f.toJson()).toList(),
      from: (v) => (v ?? [])
          .map<DoorGroupModel>((f) => DoorGroupModel.fromJson(f))
          .toList(),
    );
  }

  ///添加人脸审核
  Future<int?> postFaceAdd(data) async {
    var res = await HttpUtil.instance.post(
      '/app/admin/faceAuditRecord',
      data: data,
    );
    if (res.data['data'] != null && res.data['data'] is int) {
      return res.data['data'];
    } else {
      return null;
    }
  }

  ///人脸审核修改
  Future<int?> postFaceModify(data) async {
    var res = await HttpUtil.instance.post(
      '/app/admin/faceAuditRecord/updateFaceAudit/${data['id']}',
      data: data,
    );
    if (res.data['data'] != null && res.data['data'] is int) {
      return res.data['data'];
    } else {
      return null;
    }
  }

  ///楼栋列表
  Future<ResModel<List<CascadeModel>?>> getBuildInfo(data) async {
    var res = await HttpUtil.instance.get(
      '/app/admin/assets/getBuildInfo',
      queryParameters: data,
    );
    return ResModel<List<CascadeModel>?>(
      json: res.data,
      to: (v) => (v ?? []).map((f) => f.toJson()).toList(),
      from: (v) =>
          (v ?? []).map<CascadeModel>((f) => CascadeModel.fromJson(f)).toList(),
    );
  }

  ///单元列表
  Future<ResModel<List<CascadeModel>?>> getUnitInfo(data) async {
    var res = await HttpUtil.instance.get(
      '/app/admin/assets/getUnitInfo',
      queryParameters: data,
    );
    return ResModel<List<CascadeModel>?>(
      json: res.data,
      to: (v) => (v ?? []).map((f) => f.toJson()).toList(),
      from: (v) =>
          (v ?? []).map<CascadeModel>((f) => CascadeModel.fromJson(f)).toList(),
    );
  }

  ///楼层列表
  Future<ResModel<List<CascadeModel>?>> getFloorInfo(data) async {
    var res = await HttpUtil.instance.get(
      '/app/admin/assets/getFloorInfo',
      queryParameters: data,
    );
    return ResModel<List<CascadeModel>?>(
      json: res.data,
      to: (v) => (v ?? []).map((f) => f.toJson()).toList(),
      from: (v) =>
          (v ?? []).map<CascadeModel>((f) => CascadeModel.fromJson(f)).toList(),
    );
  }

  ///房屋列表
  Future<ResModel<List<CascadeModel>?>> getHouseInfo(data) async {
    var res = await HttpUtil.instance.get(
      '/app/admin/assets/getHouseInfo',
      queryParameters: data,
    );
    return ResModel<List<CascadeModel>?>(
      json: res.data,
      to: (v) => (v ?? []).map((f) => f.toJson()).toList(),
      from: (v) =>
          (v ?? []).map<CascadeModel>((f) => CascadeModel.fromJson(f)).toList(),
    );
  }

  ///住户列表
  Future<ResModel<List<CascadeModel>?>> getOwner(data) async {
    var res = await HttpUtil.instance.get(
      '/app/admin/resident/getOwnerByHouseId',
      queryParameters: data,
    );
    var map = res.data;
    if (map != null && map['data'] != null) {
      for (var i = 0; i < map['data'].length; i++) {
        map['data'][i]['status'] = 'owner';
      }
    }
    return ResModel<List<CascadeModel>?>(
      json: res.data,
      to: (v) => (v ?? []).map((f) => f.toJson()).toList(),
      from: (v) => (v ?? [])
          .map<CascadeModel>(
            (f) => CascadeModel.fromJson(f),
          )
          .toList(),
    );
  }

  ///根据住户姓名，手机号获取同一小区下的住户信息
  Future<ResModel<List<EnumModel>?>> getOwnerByParams(data) async {
    var res = await HttpUtil.instance.get(
      '/app/admin/resident/getResidentInfoByParam',
      queryParameters: data,
    );
    var map = res.data;
    if (map != null && map['data'] != null) {
      for (var i = 0; i < map['data'].length; i++) {
        map['data'][i]['code'] = map['data'][i]['id'];
        map['data'][i]['name'] =
            map['data'][i]['roomNo'] + map['data'][i]['name'];
      }
    }
    return ResModel<List<EnumModel>?>(
      json: res.data,
      to: (v) => (v ?? []).map((f) => f.toJson()).toList(),
      from: (v) => (v ?? [])
          .map<EnumModel>(
            (f) => EnumModel.fromJson(f),
          )
          .toList(),
    );
  }

  ///审核记录下的通行证列表
  ///http://yapi.wisdomcity.com.cn/project/16/interface/api/18284
  Future<ResModel<List<PassCardDetailModel>?>> getPassCardList(
      faceAuditId) async {
    var res = await HttpUtil.instance.get(
      '/app/admin/faceAuditRecord/$faceAuditId/passCard/list',
    );
    return ResModel<List<PassCardDetailModel>?>(
      json: res.data,
      to: (v) => (v ?? []).map((f) => f.toJson()).toList(),
      from: (v) => (v ?? [])
          .map<PassCardDetailModel>((f) => PassCardDetailModel.fromJson(f))
          .toList(),
    );
  }

  ///审核记录下未通过通行证重新下发
  ///http://yapi.wisdomcity.com.cn/project/16/interface/api/18293
  Future<bool?> putPassCardRePush(faceAuditId) async {
    var res = await HttpUtil.instance.put(
      '/app/admin/faceAuditRecord/$faceAuditId/passCard/rePush',
    );
    if (res.data['data'] != null && res.data['data'] is bool) {
      return res.data['data'];
    } else {
      return null;
    }
  }

  /// 删除审核记录
  Future<int?> deleteRecord(faceAuditId) async {
    var res = await HttpUtil.instance.delete(
      '/app/admin/faceAuditRecord/$faceAuditId',
    );
    if (res.data['data'] != null && res.data['data'] is int) {
      return res.data['data'];
    } else {
      return null;
    }
  }

  ///审核记录下全部通行证删除
  ///http://yapi.wisdomcity.com.cn/project/16/interface/api/18293
  Future<bool?> deleteAllPassCard(faceAuditId) async {
    var res = await HttpUtil.instance.delete(
      '/app/admin/faceAuditRecord/$faceAuditId/passCard/delete',
    );
    if (res.data['data'] != null && res.data['data'] is bool) {
      return res.data['data'];
    } else {
      return null;
    }
  }

  ///门禁设备列表
  ///http://yapi.wisdomcity.com.cn/project/16/interface/api/18338
  Future<ResModel<List<DeviceDetailModel>?>> getDoorDeviceList(data) async {
    var res = await HttpUtil.instance.get(
      '/app/admin/hardware/remote/doors',
      queryParameters: data,
    );
    return ResModel<List<DeviceDetailModel>?>(
      json: res.data,
      to: (v) => (v ?? []).map((f) => f.toJson()).toList(),
      from: (v) => (v ?? [])
          .map<DeviceDetailModel>((f) => DeviceDetailModel.fromJson(f))
          .toList(),
    );
  }

  ///门禁设备远程开门
  ///http://yapi.wisdomcity.com.cn/project/16/interface/api/18347
  Future<bool?> postDoorOpen(id) async {
    var res = await HttpUtil.instance.post(
      '/app/admin/hardware/remote/doors/$id/open',
    );
    if (res.data['data'] != null && res.data['data'] is bool) {
      return res.data['data'];
    } else {
      return null;
    }
  }
}
