/*
 * @Descripttion: 
 * @version: 
 * @Author: wy
 * @Date: 2020-07-30 14:10:44
 * @LastEditors: wangyang 1147192855@qq.com
 * @LastEditTime: 2022-09-28 18:20:11
 */

class UpdateAppEvent {
  final String version;
  final String title;
  final String description;
  final String link;
  final bool upgrade;
  final String deviceInfo;
  final String appName;
  final String packageName;
  final Equipment equipment;
  num? v;
  num? lv;

  UpdateAppEvent({
    required this.version,
    required this.title,
    required this.description,
    required this.link,
    required this.upgrade,
    required this.deviceInfo,
    required this.appName,
    required this.packageName,
    this.v,
    this.lv,
    int type = 0,
  }) : equipment = Equipment.values[type];

  factory UpdateAppEvent.fromJson(Map json, String localVersion, String deviceInfo, String appName, String packageName,
      {String keyStr = "version", String typeName = "packageNameType"}) {
    String version = json[keyStr]; // 版本号
    String? descriptionStr = json["description"];

    String newDescription = '系统有更新，请立即下载';
    bool upgrade = false;
    num? v;
    num? lv;
    // 版本号
    if (version != '') {
      v = num.parse(version.replaceAll(".", ""));
      lv = num.parse(localVersion.replaceAll(".", ""));

      // if (lv < v) upgrade = true;
      //当前版本不等于线上版本更新为线上版本
      if (lv != v) upgrade = true;
    }

    //  升级说明
    if (descriptionStr != null && descriptionStr.isNotEmpty) {
      newDescription = descriptionStr;
    }

    return UpdateAppEvent(
      version: version, // 版本号
      title: json["title"] ?? "发现新版本", // title
      description: newDescription, //说明
      link: json["downloadPath"]??'', // 链接地址
      upgrade: upgrade,
      deviceInfo: deviceInfo,
      appName: appName,
      packageName: packageName,
      type: json[typeName] ?? 0,
      v: v,
      lv: lv,
    );
  }

  @override
  String toString() {
    return "UpdateAppEvent { version: $version, title: $title, description: $description,  link: $link}";
  }
}

// 设备枚举
enum Equipment { other, android, ios, windows }
