/*
 * @Author       : WuWei
 * @LastEditors  : WuWei
 * @Date         : 2022-03-16 11:08:46
 * @LastEditTime : 2023-05-19 16:44:13
 * @Description  : Do not edit
 */
import 'package:app_package/app_package.dart';
import 'package:app_package/src/api/root.dart';
import 'package:app_package/src/models/notice_detail.dart';
import 'package:flutter/material.dart';
import 'package:wisdom_cli/wisdom_cli.dart';

class NoticeStore with ChangeNotifier {
  final BuildContext context;
  late Map<String, dynamic> routingArgs;
  NoticeStore(this.context) {
    routingArgs = (ModalRoute.of(context)?.settings.arguments ??
            new Map<String, dynamic>()) as Map<String, dynamic>? ??
        {};
  }
  bool get isList => routingArgs['isList'] == true ? true : false;

  String get id => routingArgs['id'];
  final appGlobalStore = GetIt.I.get<AppGlobalStore>();
  final scorllViewKey = GlobalKey<WisScrollViewState>();
  final communityGlobalStore = GetIt.I.get<CommunityGlobalStore>();

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

  ///去选择小区
  toCommunityPick() async {
    await Navigator.pushNamed(context, 'community');
    notifyListeners();
  }

  onInit() async {
    if (isList == true) {
      getNoticeList(isLoad: false);
    } else {
      getDetail();
    }
  }

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

  List noticeList = [];
  getNoticeList({bool isLoad = true}) async {
    var args = {
      ...params,
      "orderByStick": true,
    };
    if (searchValue != '' && searchValue != null) {
      args["title"] = searchValue;
    }
    var res = await RootApi.getNoticeList(args);
    final _data = res.data;
    final _list = _data?.list ?? [];
    if ((_data?.total ?? 0) == 0) {
      scorllViewKey.currentState?.isEmpty();
    } else if ((_data?.pages ?? 0) <= (_data?.pageNum ?? 0)) {
      scorllViewKey.currentState?.isLast();
    } else {
      params['pageNum']++;
    }
    isLoad ? noticeList.addAll(_list) : noticeList = _list;
    notifyListeners();
  }

  NoticeDetailModel? notice;
  routeToDetail(item) async {
    await Navigator.pushNamed(
      context,
      'notice_detail',
      arguments: {
        'isList': false,
        'id': item.id,
      },
    );
    params['pageNum'] = 1;
    noticeList = [];
    notifyListeners();
    scorllViewKey.currentState?.initTrigger();
  }

  Future getDetail() async {
    var res = await RootApi.getNoticeDetail(
      {'id': id},
    );
    notice = res.data;
    notifyListeners();
  }

  String? searchValue;
  toSearchList(text) {
    searchValue = text;
    params['pageNum'] = 1;
    noticeList = [];
    notifyListeners();
    scorllViewKey.currentState?.initTrigger();
  }

  /// 预览文件
  route2perview(url, name) async {
    await WisPreviewUtil.show(
      url: url,
      name: name,
    );
  }

  /// 预览详情图片
  void onShowImage(image) {
    List<ImageProvider> images = [];
    images.add(CachedNetworkImageProvider(AppParseUtil.image(image)));
    WisPhotoViewUtil.show(images, 0);
  }
}
