package com.red.circle.order.inner.model.dto.inapp;

import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import com.red.circle.order.inner.model.enums.inapp.InAppPurchaseReceiptType;
import com.red.circle.order.inner.model.enums.inapp.InAppPurchaseStatus;
import com.red.circle.tool.core.collection.CollectionUtils;
import java.io.Serial;
import java.io.Serializable;
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import lombok.Data;
import lombok.experimental.Accessors;

/**
 * 计划后期替换 com.sugartime.app.pojo.order.OrderPurchaseHistory
 * </p>
 * 内购详情.
 *
 * @author pengliang on 2022/10/24
 */
@Data
@Accessors(chain = true)
public class InAppPurchaseDetailsDTO implements Serializable {

  @Serial
  private static final long serialVersionUID = 1L;

  /**
   * 内部订单id.
   */
  private String id;

  /**
   * 单据类型(PAYMENT：付款，RECEIPT：收款)
   */
  private InAppPurchaseReceiptType receiptType;

  /**
   * 跟踪id.
   */
  private String trackId;

  /**
   * 商品类型.
   */
  private String trackCommodityType;

  /**
   * 来源系统
   */
  private String sysOrigin;

  /**
   * 接收用户.
   */
  private Long acceptUserId;

  /**
   * 订阅，用于订阅产品id.
   */
  private String subscribeId;

  /**
   * 订单id.
   */
  private String orderId;

  /**
   * 环境 （TEST,PROD）.
   */
  private String env;

  /**
   * 厂商信息.
   */
  private InAppPurchaseFactoryDTO factory;

  /**
   * 购买产品.
   */
  private List<InAppPurchaseProductDTO> products;

  /**
   * 商品描述.
   */
  private String productDescriptor;

  /**
   * 国家code.
   */
  private String countryCode;

  /**
   * 如果国家code是IA强制变更ID.适配印尼分国家
   */
  public void setCountryCode(String countryCode) {
    if (Objects.nonNull(countryCode) && Objects.equals(countryCode, "IA")) {
      this.countryCode = "ID";
    } else if (Objects.nonNull(countryCode) && Objects.equals(countryCode, "EY")) {
      this.countryCode = "EG";
    } else if (Objects.nonNull(countryCode) && Objects.equals(countryCode, "AB")) {
      this.countryCode = "AE";
    } else if (Objects.nonNull(countryCode) && Objects.equals(countryCode, "MB")) {
      this.countryCode = "MA";
    } else if (Objects.nonNull(countryCode) && Objects.equals(countryCode, "SP")) {
      this.countryCode = "SA";
    } else {
      this.countryCode = countryCode;
    }
  }

  /**
   * 货币.
   */
  private String currency;

  /**
   * 金额.
   */
  private BigDecimal amount;

  /**
   * 金额 usd.
   */
  private BigDecimal amountUsd;

  /**
   * 订阅, 是否处于免费试用期的指标.
   */
  private Boolean trialPeriod;

  /**
   * CREATE：创建付款意图
   * </p>
   * SUCCESS：付款成功
   * </p>
   * FAIL: 付款失败
   * </p>
   * CANCEL: 取消付款
   * </p>
   * REFUND: 退款
   * </p>
   * 订单状态.
   */
  private InAppPurchaseStatus status;

  /**
   * 付款失败 or 取消原因.
   */
  private String reason;

  /**
   * 购买时间， 来自支付平台.
   */
  private Timestamp purchaseDateMs;

  /**
   * 创建时间.
   */
  private Timestamp createTime;

  /**
   * 修改时间
   */
  private Timestamp updateTime;

  /**
   * 创建用户.
   */
  @JsonSerialize(using = ToStringSerializer.class)
  private Long createUser;

  /**
   * 状态步骤.
   */
  private List<InAppPurchaseStatusStepDTO> statusSteps;

  /**
   * 支付通知.
   */
  private List<InAppPurchaseEventNoticeDTO> payNotices;

  /**
   * 扩展数据.
   */
  private Map<String, String> metadata;

  /**
   * 乐观锁版本.
   */
  private Integer version;

  public Long longId() {
    return Long.valueOf(id);
  }

  public InAppPurchaseProductDTO firstProduct() {
    return CollectionUtils.isEmpty(products) ? null : products.get(0);
  }

  public InAppPurchaseProductDTO firstGoldProduct() {
    return CollectionUtils.isEmpty(products) ? null
        : products.stream().filter(product ->
                Objects.equals(product.getName(), "CANDY")
                    || Objects.equals(product.getName(), "GOLD"))
            .findFirst().orElse(null);
  }

  public BigDecimal firstProductContentToBigDecimal() {
    return Objects.isNull(firstProduct())
        ? BigDecimal.ZERO
        : new BigDecimal(firstProduct().getContent());
  }

  public boolean statusEqCreate() {
    return Objects.equals(status, InAppPurchaseStatus.CREATE);
  }

  public boolean receiptTypeEqReceipt() {
    return Objects.equals(InAppPurchaseReceiptType.RECEIPT, receiptType);
  }

  public boolean receiptTypeEqPayment() {
    return Objects.equals(InAppPurchaseReceiptType.PAYMENT, receiptType);
  }

  /**
   * 是否退款.
   */
  public boolean checkRefundStatus() {
    return Objects.equals(status, InAppPurchaseStatus.REFUND);
  }

}
