/*
 * @Author: WuWei
 * @Date: 2020-06-05 11:28:52
 * @LastEditors: XianKaiQun
 * @LastEditTime: 2020-10-28 17:26:36
 * @Description:
 */
import 'package:app_package/app_package.dart';
import 'package:flutter/widgets.dart';
import 'package:search_package/src/models/search_house_bill.dart';
import 'package:wisdom_cli/wisdom_cli.dart';
import '../impl/searchindex_service_impl.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_meter.dart';

class SearchHouseDetailStore with ChangeNotifier {
  final BuildContext context;
  late Map args;
  SearchHouseDetailStore(this.context) {
    args =
        ModalRoute.of(context)!.settings.arguments as Map<dynamic, dynamic>? ??
            {};
  }

  final scorllViewKey = GlobalKey<WisScrollViewState>();
  final communityGlobalStore = GetIt.I.get<CommunityGlobalStore>();

  ///从储存中取出的当前小区 拿出来可能是空
  CommunityModel? get currentCommunity => communityGlobalStore.data;

  final Map residentType = {
    'selfOwner': '独立产权人',
    'jointOwner': '共有产权人',
    'tenant': '租户',
    'relatives': '亲属',
    'enterprise': '企事业单位'
  };

  final Map meterType = {
    'waterMeter': '水表m³',
    'electricMeter': '电表KW·H',
    'intelligentWaterMeter': '智能水表',
    'intelligentElectricMeter': '智能电表'
  };

  final Map meterColor = {
    'waterMeter': Color(0xFF00C7FF),
    'electricMeter': Color(0xFFE02020),
    'intelligentWaterMeter': Color(0xFF00C7FF),
    'intelligentElectricMeter': Color(0xFFE02020)
  };

  /// 房屋信息详情[houseDetail]
  SearchHouseDetailModel? houseDetail;

  /// 房屋绑定当前住户[houseResident]
  List<SearchHouseResidentModel> houseResident = [];

  /// 房屋绑定历史住户 [houseHistoryResident]
  List<SearchHouseResidentModel> houseHistoryResident = [];

  /// 房屋所有住户[houseAllResident]
  List<SearchHouseResidentModel> houseAllResident = [];

  /// 房屋绑定车辆
  List<SearchHouseCarModel> houseCar = [];

  /// 房屋绑定车位
  List<SearchHouseParkModel> housePark = [];

  /// 房屋欠费信息
  List<SearchHouseBillModel> houseBill = [];

  /// 房屋抄表信息
  List<SearchHouseMeterModel> houseMeter = [];

  Future getHouseDetail({required id, bool isLoad = true}) async {
    if (id == null) return;
    late Map<String, dynamic> params = {'houseId': id};
    var res =
        await SearchIndexServiceImpl.instance!.getHouseDetail({...params});
    houseDetail = res.data ?? SearchHouseDetailModel();
    notifyListeners();
  }

  Future getHouseResident({required id, bool isLoad = true}) async {
    if (id == null) return;
    late Map params = {'houseId': id};
    houseResident = [];
    houseHistoryResident = [];
    var res =
        await SearchIndexServiceImpl.instance!.getHouseResident({...params});
    houseAllResident = res.data ?? [];
    // SearchHouseResidentModel() as List<SearchHouseResidentModel>;
    if (res.data == null || res.data!.length == 0) {
      houseResident = [];
      houseHistoryResident = [];
    } else {
      res.data!.forEach((item) {
        if (item.status != null && item.status!) {
          houseResident.add(item);
        } else if (item.status != null && !item.status!) {
          houseHistoryResident.add(item);
        }
      });
    }

    notifyListeners();
  }

  Future getHouseCar({required id, bool isLoad = true}) async {
    if (id == null) return;
    late Map<String, dynamic> params = {'houseId': id};
    var res = await SearchIndexServiceImpl.instance!.getHouseCar({...params});
    houseCar = res.data ?? SearchHouseCarModel() as List<SearchHouseCarModel>;
    notifyListeners();
  }

  /// 房屋绑定车位
  Future getHousePark({required id, bool isLoad = true}) async {
    if (id == null) return;
    late Map<String, dynamic> params = {'houseId': id};
    var res = await SearchIndexServiceImpl.instance!.getHousePark({...params});
    housePark =
        res.data ?? SearchHouseParkModel() as List<SearchHouseParkModel>;
    notifyListeners();
  }

  /// 欠费信息
  Future getHouseBill({required id, bool isLoad = true}) async {
    if (id == null) return;
    late Map<String, dynamic> params = {
      'houseId': id,
      'communityId': currentCommunity!.id,
    };
    var res = await SearchIndexServiceImpl.instance!.getHouseBill({...params});
    houseBill =
        res.data ?? SearchHouseBillModel() as List<SearchHouseBillModel>;
    notifyListeners();
  }

  /// 抄表信息
  Future getHouseMeter({required id, bool isLoad = true}) async {
    if (id == null) return;
    late Map<String, dynamic> params = {
      'houseId': id,
    };
    var res = await SearchIndexServiceImpl.instance!.getHouseMeter({...params});
    houseMeter =
        res.data ?? SearchHouseMeterModel() as List<SearchHouseMeterModel>;
    notifyListeners();
  }

  /// 跳转住户详情
  /// params [item] 所选中列表项（住户）
  void route2ResidentDetail(item) {
    Navigator.pushNamed(context, 'search/resident_detail',
        arguments: item.toJson());
  }
}
