/*
 * @Author: WuWei
 * @Date: 2020-05-27 10:54:44
 * @LastEditors  : WuWei
 * @LastEditTime : 2023-11-08 11:12:38
 * @Description:
 */
import 'package:app_package/app_package.dart';
import 'package:search_package/src/models/cascade_house.dart';
import 'package:search_package/src/models/search_house_list.dart';
import 'package:search_package/src/models/search_resident_detail.dart';
import 'package:search_package/src/service/search_index.dart';

import '../models/search_resident_list.dart';
import '../models/search_resident_wechat.dart';
import '../models/search_resident_house.dart';
import '../models/search_resident_car.dart';
import '../models/search_resident_park.dart';
import '../models/search_house_detail.dart';
import '../models/search_house_resident.dart';
import '../models/search_house_car.dart';
import '../models/search_house_park.dart';
import '../models/search_house_bill.dart';
import '../models/search_house_meter.dart';
import '../models/search_car_list.dart';
import '../models/search_car_detail.dart';
import '../models/search_park_list.dart';
import '../models/search_park_detail.dart';

class SearchIndexServiceImpl implements SearchIndexService {
  static SearchIndexServiceImpl? get instance => _getInstance();
  static SearchIndexServiceImpl? _instance;
  SearchIndexServiceImpl._internal() {
    // 初始化
  }
  static SearchIndexServiceImpl? _getInstance() {
    if (_instance == null) {
      _instance = new SearchIndexServiceImpl._internal();
    }
    return _instance;
  }

  static String prefix = '/app/admin/search';

  Future<ResModel<PagingModel<SearchResidentListModel>>> getResidentList(
      data) async {
    var res = await HttpUtil.instance.get(
      '$prefix/v1/resident',
      queryParameters: Map<String, dynamic>.from(data),
    );

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

  Future<ResModel<PagingModel<SearchHouseListModel>>> getHouseList(data) async {
    var res = await HttpUtil.instance.get(
      '$prefix/v1/house',
      queryParameters: Map<String, dynamic>.from(data),
    );

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

  Future<ResModel<List<SearchResidentWechatModel>?>> getResidentWechat(
      data) async {
    var res = await HttpUtil.instance.get(
      '$prefix/v1/resident/wechat',
      queryParameters: Map<String, dynamic>.from(data),
    );

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

  Future<ResModel<SearchResidentDetailModel>> getResidentDetail(data) async {
    var res = await HttpUtil.instance.get(
      '$prefix/v1/resident/detail',
      queryParameters: Map<String, dynamic>.from(data),
    );

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

  Future<ResModel<List<SearchResidentHouseModel>?>> getResidentHouse(
      data) async {
    var res = await HttpUtil.instance.get(
      '$prefix/v1/resident/other/house',
      queryParameters: data,
    );

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

  Future<ResModel<List<SearchResidentCarModel>?>> getResidentCar(data) async {
    var res = await HttpUtil.instance.get(
      '$prefix/v1/resident/other/car',
      queryParameters: data,
    );

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

  Future<ResModel<List<SearchResidentParkModel>?>> getResidentPark(data) async {
    var res = await HttpUtil.instance.get(
      '$prefix/v1/resident/other/parking_space',
      queryParameters: data,
    );

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

  Future<ResModel<SearchHouseDetailModel>> getHouseDetail(data) async {
    var res = await HttpUtil.instance.get(
      '$prefix/v1/house/detail',
      queryParameters: Map<String, dynamic>.from(data),
    );

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

  Future<ResModel<List<SearchHouseResidentModel>?>> getHouseResident(
      data) async {
    var res = await HttpUtil.instance.get(
      '$prefix//v1/house/other/resident',
      queryParameters: Map<String, dynamic>.from(data),
    );

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

  Future<ResModel<List<SearchHouseCarModel>?>> getHouseCar(data) async {
    var res = await HttpUtil.instance.get(
      '$prefix/v1/house/other/car',
      queryParameters: data,
    );

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

  Future<ResModel<List<SearchHouseParkModel>?>> getHousePark(data) async {
    var res = await HttpUtil.instance.get(
      '$prefix/v1/house/other/parking_space',
      queryParameters: data,
    );

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

  Future<ResModel<List<SearchHouseBillModel>?>> getHouseBill(data) async {
    var res = await HttpUtil.instance.get(
      '$prefix/v1/house/arrearsFee',
      queryParameters: data,
    );

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

  Future<ResModel<List<SearchHouseMeterModel>?>> getHouseMeter(data) async {
    var res = await HttpUtil.instance.get(
      '$prefix/v1/house/meter/transcribe',
      queryParameters: data,
    );

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

  Future<ResModel<PagingModel<SearchCarListModel>>> getCarList(data) async {
    var res = await HttpUtil.instance.get(
      '$prefix/v1/car',
      queryParameters: Map<String, dynamic>.from(data),
    );

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

  Future<ResModel<SearchCarDetailModel>> getCarDetail(data) async {
    var res = await HttpUtil.instance.get(
      '$prefix/v1/car/detail',
      queryParameters: Map<String, dynamic>.from(data),
    );

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

  Future<ResModel<PagingModel<SearchParkListModel>>> getParkList(data) async {
    var res = await HttpUtil.instance.get(
      '$prefix/v1/parking_space',
      queryParameters: Map<String, dynamic>.from(data),
    );

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

  Future<ResModel<SearchParkDetailModel>> getParkDetail(data) async {
    var res = await HttpUtil.instance.get(
      '$prefix/v1/parking_space/detail',
      queryParameters: Map<String, dynamic>.from(data),
    );

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

  ///获取楼栋
  Future<ResModel<List<CascadeHouseModel>?>> getBuildInfo(data) async {
    var res = await HttpUtil.instance.get(
      '/app/admin/assets/getBuildInfo',
      queryParameters: data,
    );

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

  ///获取单元
  Future<ResModel<List<CascadeHouseModel>?>> getUnitInfo(data) async {
    var res = await HttpUtil.instance.get(
      '/app/admin/assets/getUnitInfo',
      queryParameters: data,
    );

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

  ///获取楼层
  Future<ResModel<List<CascadeHouseModel>?>> getFloorInfo(data) async {
    var res = await HttpUtil.instance.get(
      '/app/admin/assets/getFloorInfo',
      queryParameters: data,
    );

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

  ///获取房屋
  Future<ResModel<List<CascadeHouseModel>?>> getHouseInfo(data) async {
    var res = await HttpUtil.instance.get(
      '/app/admin/assets/getHouseInfo',
      queryParameters: data,
    );

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

  /// 添加住户、住户新增
  /// http://yapi.wisdomcity.com.cn/project/40/interface/api/18131
  Future<String?> postAddResident(data) async {
    var res = await HttpUtil.instance.post(
      '$prefix/v1/resident',
      data: data,
    );

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

  ///住户编辑
  Future<int?> patchEditResident(id, data) async {
    var res = await HttpUtil.instance.patch(
      '$prefix/v1/resident/$id',
      data: data,
    );

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