package com.oqpo.api.web.dto.response.stinfo.item;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.oqpo.api.entity.oper.FileEntity;
import com.oqpo.api.entity.stinfo.BrandEntity;
import com.oqpo.api.entity.stinfo.ItemEntity;
import io.swagger.annotations.ApiModelProperty;
import lombok.*;

import java.util.List;
import java.util.stream.Collectors;


@Getter
@Setter
@Builder
@AllArgsConstructor(access = AccessLevel.PROTECTED)
@JsonInclude(JsonInclude.Include.ALWAYS)
public class ItemInfoResponse {

    @ApiModelProperty(value = "브랜드아이디")
    private String brandId;

    @ApiModelProperty(value = "품목아이디")
    private String itemId;

    @ApiModelProperty(value = "구매단위")
    private String pchUnit;

    @ApiModelProperty(value = "구매단위명")
    private String pchUnitNm;

    @ApiModelProperty(value = "무게")
    private Float weight;

    @ApiModelProperty(value = "부피")
    private Float volume;

    @ApiModelProperty(value = "가로")
    private Float width;

    @ApiModelProperty(value = "세로")
    private Float height;

    @ApiModelProperty(value = "높이")
    private Float high;

    @ApiModelProperty(value = "유통기한여부")
    private String expryYn ;

    @ApiModelProperty(value = "반품구분코드")
    private String rtnDvsn ;

    @ApiModelProperty(value = "반품구분명")
    private String rtnDvsnNm ;

    @ApiModelProperty(value = "수입구분코드")
    private String impDvsn ;

    @ApiModelProperty(value = "수입구분명")
    private String impDvsnNm ;

    @ApiModelProperty(value = "자산정보")
    private String assetInfo ;

    @ApiModelProperty(value = "재고단위")
    private String stckUnit ;

    @ApiModelProperty(value = "발주가능구분")
    private String podrPssblDvsn;

    @ApiModelProperty(value = "발주가능명")
    private String podrPssblDvsnNm;

    @ApiModelProperty(value = "구매품목여부")
    private String pchItemYn;

    @ApiModelProperty(value = "판매품목여부")
    private String saleItemYn;

    @ApiModelProperty(value = "상태코드")
    private String itemStCd;

    @ApiModelProperty(value = "상태명")
    private String itemStNm;

    @ApiModelProperty(value = "품목유형")
    private String itemType;

    @ApiModelProperty(value = "품목유형명")
    private String itemTypeNm;

    @ApiModelProperty(value = "단위")
    private String saleUnit;

    @ApiModelProperty(value = "단위명")
    private String saleUnitNm;

    @ApiModelProperty(value = "단위")
    private String unit;

    @ApiModelProperty(value = "단위명")
    private String unitNm;

    @ApiModelProperty(value = "반품가능여부")
    private String rtnPssblYn;

    @ApiModelProperty(value = "품목이미지파일번호")
    private String itemImageFileNo;

    @ApiModelProperty(value = "1박스수량")
    private Integer oneBoxQty;

    @ApiModelProperty(value = "품목분류번호")
    private String itemClassMgntNo;

    @ApiModelProperty(value = "품목분류명")
    private String itemClassNm;

    @ApiModelProperty(value = "품목분류 class1")
    private String itemClass1;

    @ApiModelProperty(value = "품목분류 class2")
    private String itemClass2;

    @ApiModelProperty(value = "품목분류 class3")
    private String itemClass3;

    @ApiModelProperty(value = "품목분류 class4")
    private String itemClass4;

    @ApiModelProperty(value = "품목이지미 파일 정보")
    private List<ItemInfoResponse.FileLogInfo> imageFileNoList;

    public static ItemInfoResponse toDTO(ItemEntity entity, List<FileEntity>  fileImageInfoList) {
        if (entity == null) return null;
        return ItemInfoResponse.builder()
                .brandId(entity.getBrandId())
                .itemId(entity.getItemId())
                .pchUnit(entity.getPchUnit() == null ? "" :entity.getPchUnit())
                .pchUnitNm(entity.getPchUnitNm() == null ? "" :entity.getPchUnitNm())
                .weight(entity.getWeight() == null ? 0 :entity.getWeight())
                .volume(entity.getVolume() == null ? 0 :entity.getVolume())
                .width(entity.getWidth() == null ? 0 :entity.getWidth())
                .height(entity.getHeight() == null ? 0 :entity.getHeight())
                .high(entity.getHigh() == null ? 0 :entity.getHigh())
                .expryYn (entity.getExpryYn() == null ? "" :entity.getExpryYn())
                .rtnDvsn (entity.getRtnDvsn() == null ? "" :entity.getRtnDvsn())
                .rtnDvsnNm (entity.getRtnDvsnNm() == null ? "" :entity.getRtnDvsnNm())
                .impDvsn (entity.getImpDvsn() == null ? "" :entity.getImpDvsn())
                .impDvsnNm (entity.getImpDvsn() == null ? "" :entity.getImpDvsn())
                .assetInfo (entity.getAssetInfo() == null ? "" :entity.getAssetInfo())
                .stckUnit (entity.getStckUnit() == null ? "" :entity.getStckUnit())
                .podrPssblDvsn(entity.getPodrPssblDvsn() == null ? "" :entity.getPodrPssblDvsn())
                .podrPssblDvsnNm(entity.getPodrPssblDvsnNm() == null ? "" :entity.getPodrPssblDvsnNm())
                .pchItemYn(entity.getPchItemYn() == null ? "" :entity.getPchItemYn())
                .saleItemYn(entity.getSaleItemYn() == null ? "" :entity.getSaleItemYn())
                .itemStCd(entity.getItemStCd() == null ? "" :entity.getItemStCd())
                .itemStNm(entity.getItemStNm() == null ? "" :entity.getItemStNm())
                .itemType(entity.getItemType() == null ? "" :entity.getItemType())
                .itemTypeNm(entity.getItemTypeNm() == null ? "" :entity.getItemTypeNm())
                .saleUnit(entity.getSaleUnit() == null ? "" :entity.getSaleUnit())
                .saleUnitNm(entity.getSaleUnitNm() == null ? "" :entity.getSaleUnitNm())
                .unit(entity.getUnit() == null ? "" :entity.getUnit())
                .unitNm(entity.getUnitNm() == null ? "" :entity.getUnitNm())
                .rtnPssblYn(entity.getRtnPssblYn() == null ? "" :entity.getRtnPssblYn())
                .itemImageFileNo(entity.getItemImageFileNo() == null ? "" :entity.getItemImageFileNo() )
                .imageFileNoList(fileImageInfoList.stream()
                        .map(m -> ItemInfoResponse.FileLogInfo.builder()
                                .fileNo(m.getFileNo())
                                .fileSeq(m.getFileSeq())
                                .filePath(m.getFilePath())
                                .fileNm(m.getFileNm())
                                .fileOrgNm(m.getFileOrgNm())
                                .build())
                        .collect(Collectors.toList()))
                .oneBoxQty(entity.getOneBoxQty() == null ? 0 :entity.getOneBoxQty())
                .itemClassMgntNo(entity.getItemClassInfo() == null ? "" : entity.getItemClassInfo().getItemClassMgntNo() == null ? "" : entity.getItemClassInfo().getItemClassMgntNo())
                .itemClassNm(entity.getItemClassMngInfo() == null ? "" : entity.getItemClassMngInfo().getItemClassNm() == null ? "" : entity.getItemClassMngInfo().getItemClassNm())
                .itemClass1(entity.getItemClassInfo() == null ? "" : entity.getItemClassInfo().getItemClass1() == null ? "" : entity.getItemClassInfo().getItemClass1())
                .itemClass2(entity.getItemClassInfo() == null ? "" : entity.getItemClassInfo().getItemClass2() == null ? "" : entity.getItemClassInfo().getItemClass2())
                .itemClass3(entity.getItemClassInfo() == null ? "" : entity.getItemClassInfo().getItemClass3() == null ? "" : entity.getItemClassInfo().getItemClass3())
                .itemClass4(entity.getItemClassInfo() == null ? "" : entity.getItemClassInfo().getItemClass4() == null ? "" : entity.getItemClassInfo().getItemClass4())
                .build();
    }

    @Getter
    @Builder
    @AllArgsConstructor(access = AccessLevel.PROTECTED)
    static class FileLogInfo {
        @ApiModelProperty(value = "파일번호")
        private String fileNo;

        @ApiModelProperty(value = "파일순번")
        private Integer fileSeq;

        @ApiModelProperty(value = "파일경로")
        private String filePath;

        @ApiModelProperty(value = "파일명")
        private String fileNm;

        @ApiModelProperty(value = "원파일명")
        private String fileOrgNm;
    }
}
