package com.its.api.its.model.entity.vms;

import com.its.api.its.model.dto.vms.TbVmsSymbLibDto;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.*;

import javax.imageio.ImageIO;
import javax.persistence.*;
import java.awt.*;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.Serializable;

/**
 * VMS 심벌 라이브러리 Entity Class
 */
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Builder
@AllArgsConstructor
@ApiModel("VMS 심벌 라이브러리")
@Entity
@Table(name = "TB_VMS_SYMB_LIB")
public class TbVmsSymbLib implements Serializable {
    private static final long serialVersionUID = 1L;

    @ApiModelProperty("심벌라이브러리 번호")  // N NUMBER(4)
    @Id
    @Column(name = "SYMBLIB_NMBR", nullable = false, columnDefinition = "NUMBER", length = 4)
    private Integer symbLibNmbr;

    @ApiModelProperty("등록 일시")  // Y VARCHAR(14)
    @Column(name = "RGST_DT", length = 14)
    private String rgstDt;

    @ApiModelProperty("RED 데이터")  // Y BLOB
    @Lob
    @Column(name = "RED_DATA")
    private byte[] redData;

    @ApiModelProperty("GREEN 데이터")  // Y BLOB
    @Lob
    @Column(name = "GREEN_DATA")
    private byte[] greenData;

    @ApiModelProperty("심벌 설명")  // Y VARCHAR(200)
    @Column(name = "SYMB_EXPL", length = 200)
    private String symbExpl;

    @ApiModelProperty("이미지 데이터")  // Y BLOB
    @Lob
    @Column(name = "IMAG_DATA")
    private byte[] imagData;

    @ApiModelProperty("VMS 유형 코드")  // Y VARCHAR(7)
    @Column(name = "VMS_TYPE_CD", length = 7)
    private String vmsTypeCd;

    @ApiModelProperty("심벌 이미지 유형")  // Y VARCHAR(7)
    @Column(name = "SYMB_IMAG_TYPE", length = 7)
    private String symbImagType;

    @ApiModelProperty("심벌 파일 명")  // Y VARCHAR(40)
    @Column(name = "SYMB_FILE_NM", length = 40)
    private String symbFileNm;

    public Image getImage() {
        if (this.imagData == null || this.imagData.length == 0) return null;
        ByteArrayInputStream bis = new ByteArrayInputStream(this.imagData);
        try {
            return ImageIO.read(bis);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public TbVmsSymbLibDto toDto() {
        return TbVmsSymbLibDto.builder()
                .symbLibNmbr(this.symbLibNmbr)
                .rgstDt(this.rgstDt)
                .redData(this.redData)
                .greenData(this.greenData)
                .symbExpl(this.symbExpl)
                .imagData(this.imagData)
                .vmsTypeCd(this.vmsTypeCd)
                .symbImagType(this.symbImagType)
                .symbFileNm(this.symbFileNm)
                .build();
    }

    public TbVmsSymbLib(Integer symbLibNmbr) {
        this.symbLibNmbr = symbLibNmbr;
    }

    public void updateInfo(TbVmsSymbLibDto.TbVmsSymbLibUpdReq req) {
        this.rgstDt = req.getRgstDt();
        this.redData = req.getRedData();
        this.greenData = req.getGreenData();
        this.symbExpl = req.getSymbExpl();
        this.imagData = req.getImagData();
        this.vmsTypeCd = req.getVmsTypeCd();
        this.symbImagType = req.getSymbImagType();
        this.symbFileNm = req.getSymbFileNm();
    }

}
