import 'package:app_package/app_package.dart';
import 'package:intelligent_package/src/models/community_conf.dart';
import 'package:intelligent_package/src/models/intelligent_detail_list_model.dart';

import '../intelligent_service.dart';
import '../../models/intelligent_list_model.dart';
import '../../models/intelligent_total_model.dart';

class IntelligentServiceImpl implements IntelligentService {
  static String prefix = '/app/admin';

  /// 智能表列表
  /// http://yapi.wisdomcity.com.cn/project/16/interface/api/5431
  @override
  Future<ResModel<PagingModel<IntelligentListModel>>> getIntelligentList(
      data) async {
    var res = await HttpUtil.instance.get(
      '$prefix/intelligent/getMeterHouseIntelligentList',
      queryParameters: data,
    );

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

  /// 智能表通电、合闸
  /// http://yapi.wisdomcity.com.cn/project/16/interface/api/5436
  @override
  Future<bool?> getIntelligetSwitch(data) async {
    var res = await HttpUtil.instance.get(
      '$prefix/intelligent/meterSwitch',
      queryParameters: data,
    );

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

  ///统计
  Future<ResModel<IntelligentTotalModel>> getTotalDetail(data) async {
    var res = await HttpUtil.instance.get(
      '$prefix/intelligent/total',
      queryParameters: data,
    );

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

  Future<ResModel<PagingModel<IntelligentDetailListModel>>> getDetailPage(
      data) async {
    var res = await HttpUtil.instance.get(
      '$prefix/intelligent/meterMarginRecords/list',
      queryParameters: data,
    );

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

  ///智能表拉闸通断电
  /// http://yapi.wisdomcity.com.cn/project/16/interface/api/9878

  Future<bool?> getMeterOnAndOff(data) async {
    var res = await HttpUtil.instance.get(
      '$prefix/intelligent/switchOpenAndClose',
      queryParameters: data,
    );

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

  /// 小区预存款配置
  /// http://yapi.wisdomcity.com.cn/project/40/interface/api/20042
  Future<ResModel<CommunityConfModel>> getCommunityConf(data) async {
    var res = await HttpUtil.instance.get(
      '/app/admin/communityConfiguration/${data['communityId']}',
    );

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