/*
 * @Author: WuWei
 * @Date: 2020-06-01 15:14:29
 * @LastEditors  : WuWei
 * @LastEditTime : 2023-06-15 15:45:11
 * @Description:
 */

import 'package:app_package/app_package.dart';
import 'package:flutter/widgets.dart';
import 'package:search_package/src/impl/searchindex_service_impl.dart';
import 'package:wisdom_cli/wisdom_cli.dart';
import '../models/search_resident_detail.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';

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

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

  final scorllViewKey = GlobalKey<WisScrollViewState>();

  /// 住户信息详情[residentDetail]
  SearchResidentDetailModel? residentDetail;

  /// 住户绑定微信[residentWechat]
  List<SearchResidentWechatModel>? residentWechat;

  /// 住户绑定的其他房屋 [residentHouse]
  List<SearchResidentHouseModel>? residentHouse;

  /// 住户绑定的其他车辆 [residentCar]
  List<SearchResidentCarModel>? residentCar;

  /// 住户绑定车位 [residentPark]
  List<SearchResidentParkModel>? residentPark;

  Future getResidentDetail({id, bool isLoad = true}) async {
    late Map<String, dynamic> params = {
      'residentId': id,
      // 'communityId': currentCommunity.id,
    };
    var res =
        await SearchIndexServiceImpl.instance!.getResidentDetail({...params});
    residentDetail = res.data ?? SearchResidentDetailModel();
    notifyListeners();
  }

  ///打电话
  callNum(phone) async {
    var url = 'tel:$phone';
    launch(url);
  }

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

  Future getResidentHouse({required id, bool isLoad = true}) async {
    if (id == null) {
      return;
    }
    Map<String, dynamic> params = {
      'residentId': id,
    };
    var res = await SearchIndexServiceImpl.instance?.getResidentHouse(params);

    residentHouse = (res?.data != null ? SearchResidentHouseModel() : [])
        as List<SearchResidentHouseModel>?;
    notifyListeners();
  }

  Future getResidentCar({id, bool isLoad = true}) async {
    // if (!id) {return;};
    Map<String, dynamic> params = {
      'residentId': id,
    };
    var res =
        await SearchIndexServiceImpl.instance!.getResidentCar({...params});

    residentCar =
        res.data ?? SearchResidentCarModel() as List<SearchResidentCarModel>?;
    notifyListeners();
  }

  Future getResidentPark({id, bool isLoad = true}) async {
    // if (!id) {return;};
    Map<String, dynamic> params = {
      'residentId': id,
    };
    var res = await SearchIndexServiceImpl.instance!.getResidentPark(params);

    residentPark =
        res.data ?? SearchResidentParkModel() as List<SearchResidentParkModel>?;
    notifyListeners();
  }

  /// 跳转编辑
  toEdit() async {
    await Navigator.pushNamed(context, 'search/add_resident', arguments: {
      "isEdit": true,
      "id": args['id'],
    });
    getResidentDetail(
      id: args['id'],
    );
  }
}
