package com.izouma.nineth.domain;

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

import javax.persistence.*;
import java.time.LocalDateTime;
import java.util.List;

@Data
@Entity
@AllArgsConstructor
@NoArgsConstructor
@Builder
@Table(indexes = {
        @Index(columnList = "userId")
})
public class UserAssetSummary {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY, generator = "hibernate_sequence")
    private Long id;

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

    @ApiModelProperty("资产总数")
    @Column(columnDefinition = "int default 0")
    private int num;

    @ApiModelProperty("拍卖中数量")
    @Column(columnDefinition = "int default 0")
    private int auctioningNum;

    @ApiModelProperty("寄售数量")
    @Column(columnDefinition = "int default 0")
    private int consignmentNum;

    @ApiModelProperty("仅展示数量")
    @Column(columnDefinition = "int default 0")
    private int openShowNum;

    @ApiModelProperty("未展示数量")
    @Column(columnDefinition = "int default 0")
    private int closeShowNum;

//    @ApiModelProperty("市场交易完成数量")
//    @Column(columnDefinition = "int default 0")
//    private int transferredNum;
//
//    @ApiModelProperty("拍卖完成数量")
//    @Column(columnDefinition = "int default 0")
//    private int auctionedNum;

    @ApiModelProperty("系列名称")
    private String prefixName;

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

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

    private Long assetId;

    @Column(columnDefinition = "tinyint unsigned default 1")
    private boolean opened = true;

    @ApiModelProperty("类型")
    @Enumerated(EnumType.STRING)
    private CollectionType type;

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

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

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

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

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

    @ApiModelProperty("最新创建时间")
    private LocalDateTime createdAt;

    public UserAssetSummary(Asset asset) {
        this.assetId = asset.getId();
        this.userId = asset.getUserId();
        this.minter = asset.getMinter();
        this.pic = asset.getPic();
        this.prefixName = asset.getPrefixName();
        this.consignment = asset.isConsignment();
        this.publicShow = asset.isPublicShow();
        this.name = asset.getName();
        this.number = asset.getNumber();
        this.status = asset.getStatus();
        this.type = asset.getType();
        this.opened = asset.isOpened();
        this.createdAt = asset.getCreatedAt();
        if (CollectionType.BLIND_BOX.equals(asset.getType()) && !asset.isOpened()) {
            this.num = 1;
            if (AssetStatus.AUCTIONING.equals(asset.getStatus())) {
                this.auctioningNum = 1;
            }
            if (asset.isConsignment()) {
                this.consignmentNum = 1;
            } else {
                if (asset.isPublicShow()) {
                    this.openShowNum = 1;
                } else {
                    this.closeShowNum = 1;
                }
            }
        }
    }
}
