package com.seer.teach.mall.response;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

@Getter
@Setter
@ToString
@Schema(description = "商品详情 Response")
public class GoodSpuDetailResp {

    @Schema(description = "商品id")
    private Integer id;

    @Schema(description = "商品名称")
    private String name;

    @Schema(description = "商品库存")
    private Long stock;;

    @Schema(description = "商品所属分类的ID")
    private Integer categoryId;

    @Schema(description = "商品图片")
    private String primaryPicUrl;

    @Schema(description = "商品销量")
    private Long salesCount;

    @Schema(description = "商品价格")
    private BigDecimal price;

    @Schema(description = "商品在列表中展示的图片链接")
    private List<String> listPicUrl;

    @Schema(description = "商品收藏数量")
    private Integer collectCount;

    @Schema(description = "商品简要描述")
    private String goodsBrief;

    @Schema(description = "商品详细描述")
    private String goodsDesc;

    @Schema(description = "是否收藏 0未收藏 1收藏")
    private Integer isCollect;

    @Schema(description = "收藏时间")
    private LocalDateTime collecteTime;

    @Schema(name = "属性数组")
    private Collection<Attribute> attributes;

    @Schema(name = "sku map")
    private Map<String,Item> skuItemMap;

    @Schema(description = "商品详情 sku")
    @Getter
    @Setter
    @ToString
    public static class Item {

        private int skuId;

        @Schema(description = "条形码")
        private String barCode;

        @Schema(description = "商品库存")
        private Long stock;

        @Schema(name = "销售价格，单位：分", example = "1999")
        private BigDecimal price;

        @Schema(description = "商品图片")
        private String picUrl;

    }

    @Schema(description = "商品详情 属性")
    @Getter
    @Setter
    @ToString
    public static class Attribute {
        private int attributeId;
        private String attributeName;
        private List<Value> values;

        public Attribute(int attributeId, String attributeName,int valueId, String valueName ) {
            this.attributeId = attributeId;
            this.attributeName = attributeName;
            values = new ArrayList<>();
            values.add(new Value(valueId, valueName));
        }

        public void addValue(Value value) {
            if (values.stream().noneMatch(v -> v.getValueId() == value.getValueId())) {
                values.add(value);
            }
        }
    }

    @Getter
    @Setter
    @ToString
    public static class Value {

        private int valueId;

        private String valueName;

        public Value(int valueId, String valueName) {
            this.valueId = valueId;
            this.valueName = valueName;
        }

    }

}
