import 'package:json_annotation/json_annotation.dart';
import 'package:keyboard/utils/common_expand.dart';

part 'goods_info.g.dart';

@JsonSerializable()
class GoodsInfo {
  @JsonKey(name: "id")
  late int id;
  @JsonKey(name: "sort")
  late int sort;
  @JsonKey(name: "name")
  late String name;
  @JsonKey(name: "subscribable")
  late int subscribable;
  @JsonKey(name: "amount")
  late int amount;
  @JsonKey(name: "originalAmount")
  late int originalAmount;
  @JsonKey(name: "auth")
  late String auth;
  @JsonKey(name: "subscriptionMillis")
  late int subscriptionMillis;
  @JsonKey(name: "priceDesc")
  late String priceDesc;
  @JsonKey(name: 'payOptionIds')
  List<int>? payOptionIds;
  @JsonKey(name: 'description')
  String? description;
  @JsonKey(name: 'mostDesc')
  String? mostDesc;
  @JsonKey(name: 'selectDesc')
  String? selectDesc;
  @JsonKey(name: 'timeLimitDesc')
  String? timeLimitDesc;
  @JsonKey(name: 'code')
  late String code;

  GoodsInfo({
    required this.id,
    required this.sort,
    required this.name,
    required this.subscribable,
    required this.amount,
    required this.originalAmount,
    required this.auth,
    required this.subscriptionMillis,
    required this.priceDesc,
    required this.code,
    this.payOptionIds,
    this.description,
    this.mostDesc,
    this.selectDesc,
    this.timeLimitDesc,
  });

  get amountText => (amount / 100).toFormattedString(2);

  get originalAmountText => "${(originalAmount / 100).toFormattedString(2)}元";

  get priceDescNumber => priceDesc.extractAmount();

  get priceDescUnit => priceDesc.extractUnit();

  factory GoodsInfo.fromJson(Map<String, dynamic> json) =>
      _$GoodsInfoFromJson(json);

  Map<String, dynamic> toJson() => _$GoodsInfoToJson(this);

  GoodsInfo copyWith() {
    return GoodsInfo(
      id: id,
      sort: sort,
      name: name,
      subscribable: subscribable,
      amount: amount,
      originalAmount: originalAmount,
      auth: auth,
      subscriptionMillis: subscriptionMillis,
      priceDesc: priceDesc,
      payOptionIds: payOptionIds,
      description: description,
      mostDesc: mostDesc,
      selectDesc: selectDesc,
      timeLimitDesc: timeLimitDesc,
      code: code,
    );
  }
}
