package com.its.vms.dto;

import com.its.vms.service.VmsSymbService;
import com.its.vms.xnettcp.vms.protocol.enums.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 memSymbLibNmbr;
    private Integer symbLibNmbr;
    private Integer dnldSymbLibNmbr;
    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;   // 소통정보 이미지 인 경우 X
    private Integer posY;   // 소통정보 이미지 인 경우 Y

    private eVmsImageType imagType;

    private String localFileName;
    private String ftpFileName;

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

    public eVmsImageType getBitmapType() {
        if ("BMP".equalsIgnoreCase(this.symbImagType)) {
            return eVmsImageType.VMS_IMAGE_TYPE_BMP;
        }
        if ("GIF".equalsIgnoreCase(this.symbImagType)) {
            return eVmsImageType.VMS_IMAGE_TYPE_GIF;
        }
        if ("JPG".equalsIgnoreCase(this.symbImagType)) {
            return eVmsImageType.VMS_IMAGE_TYPE_JPG;
        }
        if ("PNG".equalsIgnoreCase(this.symbImagType)) {
            return eVmsImageType.VMS_IMAGE_TYPE_PNG;
        }
        return eVmsImageType.VMS_IMAGE_TYPE_VIDEO;
    }

    public void setSymbInfo() {

        this.dnldSymbLibNmbr = VmsSymbService.getDnldSymbNmbr(this.memSymbLibNmbr);
        this.imagType = eVmsImageType.VMS_IMAGE_TYPE_BMP;

        String symbFileName = "";
        if ("SBT3".equals(getSymbType())) {
            symbFileName = this.symbFileNm;
            this.imagType = eVmsImageType.VMS_IMAGE_TYPE_VIDEO;
        }
        else {
            switch(getImagType()) {
                case VMS_IMAGE_TYPE_BMP: symbFileName = this.dnldSymbLibNmbr+".bmp"; break;
                case VMS_IMAGE_TYPE_GIF: symbFileName = this.dnldSymbLibNmbr+".gif"; break;
                case VMS_IMAGE_TYPE_JPG: symbFileName = this.dnldSymbLibNmbr+".jpg"; break;
                case VMS_IMAGE_TYPE_PNG: symbFileName = this.dnldSymbLibNmbr+".png"; break;
                default:
                    if (this.symbFileNm == null || this.symbFileNm.trim().length() == 0) {
                        symbFileName = this.dnldSymbLibNmbr+".avi";
                    }
                    else {
                        symbFileName = this.symbFileNm.trim();
                    }
                    break;
            }
        }
        this.symbFileNm = symbFileName;
        this.localFileName = symbFileName;
    }

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

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

    public TbVmsSymbLibDto clone(String symbLibNmbr) {
        TbVmsSymbLibDto obj = TbVmsSymbLibDto.builder()
                .memSymbLibNmbr(symbLibNmbr)
                .symbLibNmbr(this.symbLibNmbr)
                .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;
    }
}
