package com.izouma.nineth.domain;

import com.izouma.nineth.annotations.Searchable;
import com.izouma.nineth.converter.FileObjectListConverter;
import com.izouma.nineth.converter.PrivilegeListConverter;
import com.izouma.nineth.converter.PropertyListConverter;
import com.izouma.nineth.enums.AssetStatus;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.persistence.*;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.List;


@Data
@Entity
@AllArgsConstructor
@NoArgsConstructor
@Builder
@ApiModel("资产")
public class Asset extends BaseEntity {

    @ApiModelProperty("用户ID")
    private Long userId;

    @ApiModelProperty("持有者")
    private String ownerName;

    @ApiModelProperty("藏品ID")
    private Long collectionId;

    @ApiModelProperty("订单ID")
    private Long orderId;

    @ApiModelProperty("编号")
    private int number;

    @ApiModelProperty("名称")
    @Searchable
    private String name;

    @ApiModelProperty("详情")
    @Column(columnDefinition = "TEXT")
    private String detail;

    @Column(columnDefinition = "TEXT")
    @Convert(converter = PropertyListConverter.class)
    @ApiModelProperty("特性")
    private List<CollectionProperty> properties;

    @Column(columnDefinition = "TEXT")
    @Convert(converter = PrivilegeListConverter.class)
    @ApiModelProperty("特权")
    private List<Privilege> privileges;

    @ApiModelProperty("分类")
    private String category;

    @ApiModelProperty("是否可转售")
    private boolean canResale;

    @ApiModelProperty("版税比例")
    private int royalties;

    @ApiModelProperty("手续费比例")
    private int serviceCharge;

    @ApiModelProperty("铸造者")
    @Searchable
    private String minter;

    @ApiModelProperty("铸造者ID")
    private Long minterId;

    @ApiModelProperty("铸造者头像")
    private String minterAvatar;

    @ApiModelProperty("持有者")
    @Searchable
    private String owner;

    @ApiModelProperty("持有者ID")
    private Long ownerId;

    @ApiModelProperty("持有者头像")
    private String ownerAvatar;

    @ApiModelProperty("图片")
    @Convert(converter = FileObjectListConverter.class)
    @Column(columnDefinition = "TEXT")
    private List<FileObject> pic;

    @ApiModelProperty("tokenId")
    private String tokenId;

    @ApiModelProperty("购买hash")
    private String txHash;

    @ApiModelProperty("消耗gas")
    private BigInteger gasUsed;

    @ApiModelProperty("区块高度")
    private BigInteger blockNumber;

    @ApiModelProperty("ipfs地址")
    private String ipfsUrl;

    @ApiModelProperty("购买价格")
    @Column(precision = 10, scale = 2)
    private BigDecimal price;

    @ApiModelProperty("转让价格")
    @Column(precision = 10, scale = 2)
    private BigDecimal sellPrice;

    @ApiModelProperty("转让hash")
    private String outTxHash;

    @ApiModelProperty("转让订单ID")
    private Long outOrderId;

    @ApiModelProperty("状态")
    @Enumerated(EnumType.STRING)
    private AssetStatus status;

    @ApiModelProperty("是否公开展示")
    private boolean publicShow;

    @ApiModelProperty("是否寄售")
    private boolean consignment;

    private Long publicCollectionId;

    private int likes;

    public static Asset create(Collection collection, User user) {
        return Asset.builder()
                .userId(user.getId())
                .collectionId(collection.getId())
                .minter(collection.getMinter())
                .minterId(collection.getMinterId())
                .minterAvatar(collection.getMinterAvatar())
                .name(collection.getName())
                .detail(collection.getDetail())
                .pic(collection.getPic())
                .properties(collection.getProperties())
                .privileges(collection.getPrivileges())
                .category(collection.getCategory())
                .canResale(collection.isCanResale())
                .royalties(collection.getRoyalties())
                .serviceCharge(collection.getServiceCharge())
                .price(collection.getPrice())
                .status(AssetStatus.NORMAL)
                .owner(user.getNickname())
                .ownerId(user.getId())
                .ownerAvatar(user.getAvatar())
                .build();
    }

    public static Asset create(BlindBoxItem item, User user) {
        return Asset.builder()
                .userId(user.getId())
                .collectionId(item.getCollectionId())
                .minter(item.getMinter())
                .minterId(item.getMinterId())
                .minterAvatar(item.getMinterAvatar())
                .name(item.getName())
                .detail(item.getDetail())
                .pic(item.getPic())
                .properties(item.getProperties())
                .privileges(item.getPrivileges())
                .category(item.getCategory())
                .canResale(item.isCanResale())
                .royalties(item.getRoyalties())
                .serviceCharge(item.getServiceCharge())
                .price(item.getPrice())
                .status(AssetStatus.NORMAL)
                .owner(user.getNickname())
                .ownerId(user.getId())
                .ownerAvatar(user.getAvatar())
                .build();
    }
}
