import 'package:app_package/app_package.dart';
import 'package:flutter/material.dart';
import 'package:wisdom_cli/wisdom_cli.dart';
import '../models/sd_list_model.dart';
import '../service/sd_service.dart';
import '../service/impl/sd_impl.dart';
// import '../components/showStatusPicker.dart';

class PrivateSdIndexStore with ChangeNotifier {
  final BuildContext context, providerContext;
  late Map<String, dynamic> routingArgs;
  PrivateSdIndexStore(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 communityGlobalStore = GetIt.I.get<CommunityGlobalStore>();

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

  final scorllViewListKey = GlobalKey<WisScrollViewState>();

  final SmartDevicesService _smartDevicesService = SmartDevicesServiceImpl();

  Map<String, dynamic> params = {
    'pageNum': 1,
    'pageSize': 20,
  };

  String nameLike = '';

  String status = '';

  String selectStatus = '';

  String meterType = '';

  ///智能表列表
  List<SmartDevicesListModel> smartDevicesList = [];

  /// 智能表请求列表
  Future getSmartDevicesList({bool isLoad = true}) async {
    if (!isLoad) params['pageNum'] = 1;
    if (currentCommunity.id == null) {
      WToastUtil.show('小区不能为空！');
      return;
    }

    params['communityId'] = currentCommunity.id;
    if (meterType != '') {
      params['meterType'] = meterType;
    } else {
      params.remove('meterType');
    }
    var res = await _smartDevicesService.getSmartDevicesList({
      ...params,
      'nameLike': nameLike,
    });
    final _data = res.data;
    final _list = _data?.list ?? [];
    if ((_data?.total ?? 0) == 0) {
      scorllViewListKey.currentState?.isEmpty();
    } else if ((_data?.pages ?? 0) <= (_data?.pageNum ?? 0)) {
      scorllViewListKey.currentState?.isLast();
    } else {
      params['pageNum']++;
    }
    isLoad ? smartDevicesList.addAll(_list) : smartDevicesList = _list;
    notifyListeners();
  }

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

  void onNameInput(value) {
    nameLike = value;
    this.onNameSearchTrigger();
  }

  /// 智能设备-操作
  Future handleDeviceOperate(item, behavior) async {
    if (authGlobalStore.info?.id == null) {
      return;
    }
    WLoadingUtil.show();
    var res = await _smartDevicesService.getDeviceOnAndOff(
      {
        'controllerId': item['controllerId'],
        'behavior': behavior,
      },
    );

    WLoadingUtil.hide();

    if (res) {
      WToastUtil.show('操作成功！');
      smartDevicesList = [];
      scorllViewListKey.currentState?.initTrigger();
    } else {
      WToastUtil.show('操作失败！');
    }
    notifyListeners();
  }

  /// 修改对应智能设备的值
  handleChangeListData(value) {
    for (var i = 0; i < smartDevicesList.length; i++) {
      if (smartDevicesList[i].id == value['id']) {
        smartDevicesList[i].status = value['status'];
      }
    }
    notifyListeners();
  }

  ///智能设备详情页
  routeToDetail(SmartDevicesListModel item) async {
    await Navigator.pushNamed(
      context,
      'smart_devices/detail',
      arguments: {
        'item': item.toJson(),
      },
    ).then((value) {
      handleChangeListData(value);
      notifyListeners();
    });
  }
}
