package com.its.api.utis.model.dto;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.its.api.its.global.CodeManager;
import com.its.api.utils.ItsUtils;
import com.its.api.utis.model.entity.Rsemst;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.*;

import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@ApiModel("UtisRseStateDto(RSE 시설물 상태 정보)")
public class UtisRseStateDto {

    @ApiModelProperty("시설물유형(RSE)")
    @JsonProperty("type")
    private String type;
    @ApiModelProperty("전체")
    @JsonProperty("total")
    private int total;
    @ApiModelProperty("유선정상")
    @JsonProperty("normal")
    private int normal;
    @ApiModelProperty("유선장애")
    @JsonProperty("error")
    private int error;

    @ApiModelProperty("RSE 개별 상태 정보")
    @JsonProperty("lists")
    List<RseCommSttsDto> lists = new ArrayList<>();

    @Getter
    @Setter
    @ToString
    @ApiModel("RseCommSttsDto(RSE 개별 상태 정보)")
    public static class RseCommSttsDto {

        @ApiModelProperty("RSE ID")
        @JsonProperty("rse_id")
        private String rseId;
        @ApiModelProperty("설치위치")
        @JsonProperty("location")
        private String location;

        @ApiModelProperty("장애일시")
        @JsonProperty("coll_dt")
        private String collDt;

        @ApiModelProperty("통신상태(0:정상, 1:이상")
        @JsonProperty("comm_cd")
        private String commCd;

        @ApiModelProperty("장애일시")
        @JsonProperty("comm_desc")
        private String commDesc;

        @Builder
        public RseCommSttsDto(String rseId, String location, String collDt, String commCd, String commDesc) {
            this.rseId = rseId;
            this.location = location;
            this.collDt = collDt;
            this.commCd = commCd;
            this.commDesc = commDesc;
        }
    }

    public static RseCommSttsDto toSttsDto(Rsemst rse) {

        RseCommSttsDto stts = RseCommSttsDto.builder()
                .rseId(rse.getRseid())
                .location(rse.getLocation())
                .commCd("CMS1")
                .build();
        if (rse.getState() != null) {
            String collDt = DateTimeFormatter.ofPattern("yyyyMMddHHmmss").format(rse.getState().getColldt());
            stts.setCollDt(collDt);
        }
        else {
            stts.setCollDt("19700101000000");
        }
        stts.setCommCd(ItsUtils.getRseCommStts(stts.getCollDt()));
        stts.setCommDesc(CodeManager.getCodeDescShort(CodeManager.CMNC_STTS_CD, stts.getCommCd()));       // 통신상태
        /*if (("0").equals(stts.getCommCd())) {
            stts.setCommDesc("정상");
        }
        else {
            stts.setCommDesc("이상");
        }*/
        return stts;
    }
}
