package com.izouma.nineth.domain;

import com.izouma.nineth.annotations.Searchable;
import com.izouma.nineth.converter.FileObjectConverter;
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.time.LocalDateTime;
import java.util.List;


@Data
@Entity
@Table(indexes = {
        @Index(columnList = "userId"),
        @Index(columnList = "tokenId"),
        @Index(columnList = "collectionId"),
        @Index(columnList = "minterId"),
        @Index(columnList = "ownerId"),
})
@AllArgsConstructor
@NoArgsConstructor
@Builder
@ApiModel("资产")
public class Asset extends BaseEntity {

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

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

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

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

    @ApiModelProperty("编号")
    private Integer 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;

    @Column(columnDefinition = "TEXT")
    @Convert(converter = FileObjectConverter.class)
    private FileObject model3d;

    @ApiModelProperty("tokenId")
    private String tokenId;

    @ApiModelProperty("tokenId")
    private String chainTokenId;

    @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;

    private Long fromAssetId;

    @ApiModelProperty("交易时间")
    private LocalDateTime outTime;

    private int projectId;

    @ApiModelProperty("盲盒是否自动打开，默认自动打开，true用户手动打开")
    private boolean variableType ;

    @ApiModelProperty("盲盒ID")
    private Long blindBoxId;

    /**
     * 搜索集合 为一级菜单ID,二级菜单ID,三级菜单ID 这样存
     */
    private String categories;

    public Asset(Long id, Long userId,  int projectId) {
        this.userId = userId;
        super.setId(id);
        this.projectId = projectId;
    }

//    @JsonIgnore
//    @ApiModelProperty("可变资产ID")
//    private Long variableCollectionId ;

    /**
     * 组合名称，根据编号转换而来
     */
    private String composeName;

    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())
                .model3d(collection.getModel3d())
                .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())
                .projectId(collection.getProjectId())
                .variableType(collection.isVariableType())
                .categories(collection.getCategories())
                .build();
    }

    public static Asset create(Collection collection,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())
                .model3d(item.getModel3d())
                .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())
                .projectId(item.getProjectId())
                .blindBoxId(item.getBlindBoxId())
                .categories(collection.getCategories())
                .build();
    }
}
