import 'dart:io';

import 'package:guru_app/analytics/data/analytics_model.dart';
import 'package:guru_utils/datetime/datetime_utils.dart';
import 'package:json_annotation/json_annotation.dart';

part 'orders_model.g.dart';

/// Created by Haoyi on 2022/7/27

@JsonSerializable()
class OrderUserInfo {
  @JsonKey(name: 'level', defaultValue: "0")
  final String level;

  OrderUserInfo(this.level);

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

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

class OrderType {
  static const inapp = 0;
  static const subs = 1;
}

@JsonSerializable()
class OrdersReport {
  // android
  @JsonKey(name: 'orderType', defaultValue: 0)
  int? orderType;
  @JsonKey(name: 'packageName')
  String? packageName;
  @JsonKey(name: 'productId')
  String? productId;
  @JsonKey(name: 'subscriptionId')
  String? subscriptionId;
  @JsonKey(name: 'token')
  String? token;
  @JsonKey(name: 'offerId')
  String? offerId;

  @JsonKey(name: 'basePlanId')
  String? basePlanId;

  // ios
  @JsonKey(name: 'bundleId')
  String? bundleId;
  @JsonKey(name: 'receipt')
  String? receipt;
  @JsonKey(name: 'sku')
  String? sku;
  @JsonKey(name: 'country')
  String? countryCode;

  // general
  @JsonKey(name: 'price')
  String? price;
  @JsonKey(name: 'currency')
  String? currency;

  @JsonKey(name: 'userInfo')
  OrderUserInfo? orderUserInfo;

  @JsonKey(name: "eventConfig")
  UserIdentification? userIdentification;

  @JsonKey(name: "orderId")
  String? orderId;

  @JsonKey(name: "transactionDate")
  int? transactionDate;

  OrdersReport(
      {this.orderType,
      this.token,
      this.packageName,
      this.productId,
      this.subscriptionId,
      this.bundleId,
      this.receipt,
      this.price,
      this.currency,
      this.sku,
      this.countryCode,
      this.orderUserInfo,
      this.userIdentification,
      this.offerId,
      this.basePlanId,
      this.orderId,
      this.transactionDate});

  @override
  String toString() {
    final StringBuffer sb = StringBuffer();
    sb.writeln("[OrdersReport]");
    sb.writeln("  productId: $productId");
    sb.writeln("  price: $price");
    sb.writeln("  currency: $currency");
    sb.writeln("  userIdentification: $userIdentification");
    sb.writeln("  orderId: $orderId");
    if (Platform.isAndroid) {
      sb.writeln("  orderType: $orderType");
      sb.writeln("  packageName: $packageName");
      sb.writeln("  subscriptionId: $subscriptionId");
      sb.writeln("  token: $token");
      sb.writeln("  offerId: $offerId");
      sb.writeln("  basePlanId: $basePlanId");
    } else if (Platform.isIOS) {
      sb.writeln("  bundleId: $bundleId");
      sb.writeln("  receipt: $receipt");
      sb.writeln("  sku: $sku");
      sb.writeln("  countryCode: $countryCode");
    }
    return sb
        .toString(); //'OrdersReport{orderType: $orderType, packageName: $packageName, productId: $productId, subscriptionId: $subscriptionId, token: $token, bundleId: $bundleId, receipt: $receipt, price: $price, currency: $currency, introductoryPrice: $introductoryPrice}';
  }

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

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

@JsonSerializable()
class OrdersResponse {
  @JsonKey(name: 'usdPrice', defaultValue: 0.0)
  final double usdPrice;

  @JsonKey(name: 'test', defaultValue: false)
  final bool test;

  bool get isTestOrder => test;

  OrdersResponse(this.usdPrice, this.test);

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

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

  @override
  String toString() {
    return 'OrdersResponse{usdPrice:$usdPrice, test:$test}';
  }
}
