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

import com.its.api.bis.model.dto.BitStatusHistoryDto;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.*;

import javax.persistence.*;
import java.io.Serializable;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 *  Entity Class
 */
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Builder
@AllArgsConstructor
@ApiModel("BITSTATUS_HISTORY")
@Entity
@Table(name = "BITSTATUS_HISTORY")
@IdClass(BitStatusHistoryKey.class)
public class BitStatusHistory implements Serializable {
    private static final long serialVersionUID = 1L;

    @ApiModelProperty("RECORDDATE")  // N DATE(0)
    @Id
    @Column(name = "RECORDDATE", nullable = false)
    private LocalDateTime recorddate;

    @ApiModelProperty("RECORDTIMESTAMP")  // N TIMESTAMP(6)(0)
    @Id
    @Column(name = "RECORDTIMESTAMP", nullable = false)
    private LocalDateTime recordtimestamp;

    @ApiModelProperty("BITID")  // N VARCHAR(10)
    @Id
    @Column(name = "BITID", nullable = false, length = 10)
    private String bitid;

    @ApiModelProperty("TEMPERATURE")  // Y NUMBER(5,2)
    @Column(name = "TEMPERATURE", columnDefinition = "NUMBER", length = 5, precision = 2)
    private Double temperature;

    @ApiModelProperty("HUMIDITY")  // Y NUMBER(3)
    @Column(name = "HUMIDITY", columnDefinition = "NUMBER", length = 3)
    private Integer humidity;

    @ApiModelProperty("FANSTATUS")  // Y CHAR(1)
    @Column(name = "FANSTATUS", columnDefinition = "CHAR", length = 1)
    private String fanstatus;

    @ApiModelProperty("FANOPSTATUS")  // Y CHAR(1)
    @Column(name = "FANOPSTATUS", columnDefinition = "CHAR", length = 1)
    private String fanopstatus;

    @ApiModelProperty("WCOMMSTATUS")  // Y CHAR(1)
    @Column(name = "WCOMMSTATUS", columnDefinition = "CHAR", length = 1)
    private String wcommstatus;

    @ApiModelProperty("WLCOMMSTATUS")  // Y CHAR(1)
    @Column(name = "WLCOMMSTATUS", columnDefinition = "CHAR", length = 1)
    private String wlcommstatus;

    @ApiModelProperty("HEATERSTATUS")  // Y CHAR(1)
    @Column(name = "HEATERSTATUS", columnDefinition = "CHAR", length = 1)
    private String heaterstatus;

    @ApiModelProperty("HEATEROPSTATUS")  // Y CHAR(1)
    @Column(name = "HEATEROPSTATUS", columnDefinition = "CHAR", length = 1)
    private String heateropstatus;

    @ApiModelProperty("CAMERASTATUS")  // Y CHAR(1)
    @Column(name = "CAMERASTATUS", columnDefinition = "CHAR", length = 1)
    private String camerastatus;

    @ApiModelProperty("DOORSTATUS")  // Y CHAR(1)
    @Column(name = "DOORSTATUS", columnDefinition = "CHAR", length = 1)
    private String doorstatus;

    @ApiModelProperty("MONITORSTATUS")  // Y CHAR(1)
    @Column(name = "MONITORSTATUS", columnDefinition = "CHAR", length = 1)
    private String monitorstatus;

    @ApiModelProperty("MEMORYCAPACITY")  // Y NUMBER(5)
    @Column(name = "MEMORYCAPACITY", columnDefinition = "NUMBER", length = 5)
    private Integer memorycapacity;

    @ApiModelProperty("LCDLIGHT")  // Y CHAR(1)
    @Column(name = "LCDLIGHT", columnDefinition = "CHAR", length = 1)
    private String lcdlight;

    @ApiModelProperty("ILLUMINATION")  // Y NUMBER(4)
    @Column(name = "ILLUMINATION", columnDefinition = "NUMBER", length = 4)
    private Integer illumination;

    @ApiModelProperty("SOUNDVOLUME")  // Y NUMBER(2)
    @Column(name = "SOUNDVOLUME", columnDefinition = "NUMBER", length = 2)
    private Integer soundvolume;

    public BitStatusHistoryDto toDto() {
        BitStatusHistoryDto dto = BitStatusHistoryDto.builder()
                //.recorddate(this.recorddate)
                .recordtimestamp(this.recordtimestamp)
                .bitid(this.bitid)
                .temperature(this.temperature)
                .humidity(this.humidity)
                .fanstatus(this.fanstatus)
                .fanopstatus(this.fanopstatus)
                .wcommstatus(this.wcommstatus)
                .wlcommstatus(this.wlcommstatus)
                .heaterstatus(this.heaterstatus)
                .heateropstatus(this.heateropstatus)
                .camerastatus(this.camerastatus)
                .doorstatus(this.doorstatus)
                .monitorstatus(this.monitorstatus)
                .memorycapacity(this.memorycapacity)
                .lcdlight(this.lcdlight)
                .illumination(this.illumination)
                .soundvolume(this.soundvolume)
                .build();
        if (this.recorddate != null) {
            String collDt = DateTimeFormatter.ofPattern("yyyyMMddHHmmss").format(this.recorddate);
            dto.setRecorddate(collDt);
        }
        else {
            dto.setRecorddate("19700101000000");
        }
        return dto;
    }

}
