/*
 * @Author: XianKaiQun
 * @Date: 2020-09-11 09:33:59
 * @LastEditors  : WuWei
 * @LastEditTime : 2023-09-20 17:26:37
 * @Description: 
 */
import 'dart:convert';

import 'package:app_package/app_package.dart';
import 'package:dynamic_package/src/api/api.dart';
import 'package:flutter/material.dart';
import 'package:wisdom_cli/wisdom_cli.dart';

class PrivateEditStore with ChangeNotifier {
  final BuildContext context, providerContext;
  late Map<String, dynamic> routingArgs;
  PrivateEditStore(this.context, this.providerContext) {
    routingArgs = (ModalRoute.of(context)?.settings.arguments ??
            new Map<String, dynamic>()) as Map<String, dynamic>? ??
        {};
    id = routingArgs['id'];
    if (routingArgs['content'] != null) {
      content = routingArgs['content'];
    }
    if (routingArgs['photos'] != null) {
      photos = routingArgs['photos'];
    }
    if (routingArgs['classId'] != null) {
      type = routingArgs['classId'];
    }
  }
  String? id;
  final communityGlobalStore = GetIt.I.get<CommunityGlobalStore>();

  ///如果传入了id那就说明是修改动态，不传入就说明是新增动态
  bool get isEdit => id != null;

  String? get communityId => communityGlobalStore.data.id;

  String? content = '';

  String? type;

  /// 选择类型
  chooseType(data) {
    type = data;
    notifyListeners();
  }

  List classList = [];

  /// 获取类型列表
  getClassList() async {
    var res = await DynamicServiceImpl().getClassIdList();
    classList = jsonDecode(jsonEncode(res.data)) ?? [];
    notifyListeners();
  }

  void contentChange(String value) {
    this.content = value;
    notifyListeners();
  }

  ///图片
  List<String>? photos = [];

  List<ImageProvider> get images => photos!
      .map((e) => CachedNetworkImageProvider(AppParseUtil.image(e)))
      .toList();

  void onDelete(i) {
    photos!.removeAt(i);
    notifyListeners();
  }

  Future<void> filesChanged(List<FileEntity> files) async {
    final futures =
        files.map((e) => GlobalApi.postUploadImage(e.file!)).toList();

    try {
      WLoadingUtil.show();
      final res = await Future.wait(futures);
      photos!.addAll(res.map((e) => e!.data!).toList());
      notifyListeners();
    } finally {
      WLoadingUtil.hide();
    }
  }

  /// 保存到草稿
  Future<void> submitDraft() async {
    // if (type == null) {
    //   WToastUtil.show('请选择服务动态类型');
    //   return;
    // }
    if ((content ?? '') == '') {
      WToastUtil.show('请输入服务内容');
      return;
    }

    ResModel<int> res;

    if (id != null) {
      res = await DynamicServiceImpl().postDynamicTextEdit({
        'id': id,
        'communityId': communityId,
        'title': content,
        'urls': photos,
        'classId': type,
      });
    } else {
      res = await DynamicServiceImpl().postDynamicTextAdd({
        'communityId': communityId,
        'title': content,
        'urls': photos,
        'classId': type,
      });
    }

    if (res.data != null) {
      await WModalUtil.showSync('已成功保存至草稿箱', canCancel: false);

      ///始终返回到草稿箱
      WNavUtil.instance!.pop(true);
    }
  }

  ///表单提交
  Future<void> submit() async {
    // if (type == null) {
    //   WToastUtil.show('请选择服务动态类型');
    //   return;
    // }
    if ((content ?? '') == '') {
      WToastUtil.show('请输入服务内容');
      return;
    }
    ResModel<int?> res;
    if (id != null) {
      res = await DynamicServiceImpl().postDynamicDraftpush({
        'id': id,
        'communityId': communityId,
        'title': content,
        'urls': photos,
        'classId': type,
      });
    } else {
      res = await DynamicServiceImpl().postDynamicAdd({
        'communityId': communityId,
        'title': content,
        'urls': photos,
        'classId': type,
      });
    }
    if (res.data != null) {
      await WModalUtil.showSync('服务动态发布成功~', canCancel: false);
      WNavUtil.instance!.pop(false);
    }
  }
}
