package com.its.vms.entity;

import com.its.vms.dto.TbVmsSymbLibDto;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import javax.imageio.ImageIO;
import java.awt.*;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 *  Entity Class
 */
@Slf4j
@Data
@Builder
@NoArgsConstructor//(access = AccessLevel.PROTECTED)
@AllArgsConstructor
public class TbVmsSymbLib implements Serializable {
    private static final long serialVersionUID = 1L;

    private String symbLibNmbr;
    private Integer orgSymbLibNmbr;
    private String rgstDt;
    private byte[] redData;
    private byte[] greenData;
    private String symbExpl;
    private byte[] imagData;
    private String vmsTypeCd;
    private String symbImagType;
    private String symbFileNm;
    private String symbType;
    private byte[] aviData;
    private Long imagSize;
    private Long playTm;
    private String delYn;
    private String updtDt;
    private Integer posX;
    private Integer posY;

    private eVmsImageType imagType;
    private String localFileName;
    private String ftpFileName;

    private List<String> gradSymbLibNmbrList = new ArrayList<>();
    private HashMap<Integer, TbVmsSymbIfsc> cellMap = new HashMap<>();

    public Image getImage() {
        if (this.imagData == null || this.imagData.length == 0) {
            log.error("getImage: Image Data null: {}", this.orgSymbLibNmbr);
            return null;
        }

        try (
                ByteArrayInputStream bis = new ByteArrayInputStream(this.imagData);
        ) {
            return ImageIO.read(bis);
        } catch (IOException e) {
            log.error("getImage: IOException: {}", this.orgSymbLibNmbr);
        }
        return null;
    }

    public TbVmsSymbLib clone(String symbLibNmbr) {
        TbVmsSymbLib obj = TbVmsSymbLib.builder()
                .symbLibNmbr(symbLibNmbr)
                .orgSymbLibNmbr(this.orgSymbLibNmbr)
                .rgstDt(this.rgstDt)
                .redData(this.redData)
                .greenData(this.greenData)
                .symbExpl(this.symbExpl)
                .imagData(this.imagData)
                .vmsTypeCd(this.vmsTypeCd)
                .symbImagType(this.symbImagType)
                .symbFileNm(this.symbFileNm)
                .symbType(this.symbType)
                .aviData(this.aviData)
                .imagSize(this.imagSize)
                .playTm(this.playTm)
                .delYn(this.delYn)
                .updtDt(this.updtDt)
                .posX(this.posX)
                .posY(this.posY)
                .imagType(this.imagType)
                .localFileName(this.localFileName)
                .ftpFileName(this.ftpFileName)
                .gradSymbLibNmbrList( new ArrayList<>())
                .cellMap(new HashMap<>())
                .build();

        TbVmsSymbLibDto.setSymbInfo(obj);

        return obj;
    }

}
