package com.its.vms.dto;

import com.its.vms.entity.TbVmsSymbLib;
import com.its.vms.domain.eVmsImageType;
import lombok.Builder;
import lombok.Data;
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;

/**
 *  DTO Class
 */
@Slf4j
@Data
@Builder
public class TbVmsSymbLibDto 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[] imageData;
    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, TbVmsSymbIfscDto> cellMap = new HashMap<>();


    public TbVmsSymbLib toEntity() {
        // 소통정보이미지 때문에 이미지번호에 "0" 을 추가하여 이미지번호로 사용함
        return TbVmsSymbLib.builder()
                .symbLibNmbr(Integer.parseInt(this.symbLibNmbr))
                .rgstDt(this.rgstDt)
                .redData(this.redData)
                .greenData(this.greenData)
                .symbExpl(this.symbExpl)
                .imagData(this.imageData)
                .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)
                .build();
    }

    public void setSymbInfo() {
        setImagType(eVmsImageType.vms_image_type_bmp);

        String symbFileName = "";
        if ("SBT3".equals(getSymbType())) {
            symbFileName = getSymbFileNm();
            setImagType(eVmsImageType.vms_image_type_video);
        }
        else {
            switch(getImagType()) {
                case vms_image_type_bmp: symbFileName = getSymbLibNmbr()+".bmp"; break;
                case vms_image_type_gif: symbFileName = getSymbLibNmbr()+".gif"; break;
                case vms_image_type_jpg: symbFileName = getSymbLibNmbr()+".jpg"; break;
                case vms_image_type_png: symbFileName = getSymbLibNmbr()+".png"; break;
                default:
                    if (getSymbFileNm() == null || getSymbFileNm().trim().length() == 0) {
                        symbFileName = getSymbLibNmbr()+".avi";
                    }
                    else {
                        symbFileName = getSymbFileNm().trim();
                    }
                    break;
            }
        }
        setSymbFileNm(symbFileName);
        setLocalFileName(symbFileName);
    }

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

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

    public TbVmsSymbLibDto clone(String symbLibNmbr) {
        TbVmsSymbLibDto obj = TbVmsSymbLibDto.builder()
                .symbLibNmbr(symbLibNmbr)
                .orgSymbLibNmbr(this.orgSymbLibNmbr)
                .rgstDt(this.rgstDt)
                .redData(this.redData)
                .greenData(this.greenData)
                .symbExpl(this.symbExpl)
                .imageData(this.imageData)
                .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();

        obj.setSymbInfo();

        return obj;
    }
}
