package com.its.op.entity.its.vms;

import com.its.op.dto.its.vms.TbVmsSymbLibDto;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.*;
import lombok.extern.slf4j.Slf4j;

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
 */
@Slf4j
@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 = "SYMB_LIB_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("심벌 파일 명")  // Y VARCHAR(40)
    @Column(name = "SYMB_FILE_NM", length = 40)
    private String symbFileNm;

    @ApiModelProperty("심벌 유형(SBT0:일반심벌이미지,SBT1:소통정보배경,SBT2:소통정보이미지,SBT3:동영상,SBT4:대기환경등급)")  // Y VARCHAR2(5)
    @Column(name = "SYMB_TYPE", length = 7)
    private String symbType;

    @ApiModelProperty("동영상인 경우 동영상 데이터")  // Y BLOB
    @Lob
    @Column(name = "AVI_DATA")
    private byte[] aviData;

    @ApiModelProperty("이미지 사이즈")  // Y NUMBER(10)
    @Column(name = "IMAG_SIZE", columnDefinition = "NUMBER", length = 10)
    private Long imagSize;

    @ApiModelProperty("동영상 플레이 시간")  // Y NUMBER(10)
    @Column(name = "PLAY_TM", columnDefinition = "NUMBER", length = 10)
    private Long playTm;

    @ApiModelProperty("삭제 여부(제공정보이력조회에 사용을 위해 실데이터를 삭제하지 않는다)")  // Y CHAR(1)
    @Column(name = "DEL_YN", columnDefinition = "CHAR", length = 1)
    private String delYn;

    @ApiModelProperty("갱신 일시")  // N VARCHAR2(14)
    @Column(name = "UPDT_DT", nullable = false, length = 14)
    private String updtDt;

    public Image getImage() {
        if (this.imagData == null || this.imagData.length == 0) return null;
        try (
            ByteArrayInputStream bis = new ByteArrayInputStream(this.imagData);
        ) {
            return ImageIO.read(bis);
        } catch (IOException e) {
            log.error("getImage: IOException");
        }
        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)
                .symbFileNm(this.symbFileNm)
                .symbType(this.symbType)
                .aviData(this.aviData)
                .imagSize(this.imagSize)
                .playTm(this.playTm)
                .delYn(this.delYn)
                .updtDt(this.updtDt)
                .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.symbFileNm = req.getSymbFileNm();
        this.symbType = req.getSymbType();
        this.aviData = req.getAviData();
        this.imagSize = req.getImagSize();
        this.playTm = req.getPlayTm();
        this.delYn = req.getDelYn();
        this.updtDt = req.getUpdtDt();
    }

}
