import 'package:app_package/app_package.dart';
import 'package:flutter/material.dart';
import 'package:wisdom_cli/wisdom_cli.dart';
import '../models/community_list_models.dart';
import '../service/community_service.dart';
import '../service/impl/community_impl.dart';

class PrivateCommunityIndexStore with ChangeNotifier {
  final BuildContext context, providerContext;
  late Map<String, dynamic> routingArgs;
  PrivateCommunityIndexStore(this.context, this.providerContext) {
    routingArgs = (ModalRoute.of(context)?.settings.arguments ??
            new Map<String, dynamic>()) as Map<String, dynamic>? ??
        {};
  }
  final authGlobalStore = GetIt.I.get<AuthGlobalStore>();

  final CommunityService _communityService = CommunityServiceImpl();

  final scorllViewListKey = GlobalKey<WisScrollViewState>();

  /// 小区所属的类型
  Map houseType = {
    'flat': {
      'name': '住宅',
      'color': Color(0xFF2D8BEF),
    },
    'villa': {
      'name': '别墅',
      'color': Color(0xFFFF8F00),
    },
    'apartment': {
      'name': '公寓',
      'color': Color(0xFFBB67F3),
    },
    'office': {
      'name': '写字楼',
      'color': Color(0xFFF54CA4),
    },
    'work': {
      'name': '办公楼',
      'color': Color(0xFF3EC636),
    },
    'factory': {
      'name': '厂房',
      'color': Color(0xFF1BBCDF),
    },
    'hospital': {
      'name': '医院',
      'color': Color(0xFFF95759),
    },
    'hotel': {
      'name': '酒店',
      'color': Color(0xFFFF6D31),
    },
    'school': {
      'name': '学校',
      'color': Color(0xFF00C5A6),
    }
  };

  List<CommunityListModel> communityList = [];

  String? communityName;

  /// 小区请求列表
  Future getCommunityList({bool isLoad = true}) async {
    if (authGlobalStore.info?.companyId == null) {
      WToastUtil.show('公司id不可为空，重新登录再试试哦~');
      return;
    }
    var res = await _communityService.getCommunityList({
      'companyId': authGlobalStore.info?.companyId,
      'communityName': communityName == '' ? null : communityName
    });

    communityList = res.data ?? [];
    notifyListeners();
  }

  /// 输入小区名称
  void onNameInput(value) {
    communityName = value;
    this.onNameSearchTrigger();
  }

  void onNameSearchTrigger() {
    communityList = [];
    scorllViewListKey.currentState?.initTrigger();
  }

  routeToDetail(CommunityListModel item) async {
    await Navigator.pushNamed(
      context,
      'community/detail',
      arguments: item.toJson(),
    );
  }
}
