package com.its.op.dto.its.dashboard;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Builder;
import lombok.Data;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 *  DTO Class
 */
@Data
@Builder
//@JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.ANY)
@ApiModel("TrafPrcnStatDto(소통등급갯수상태)")
public class TrafPrcnStatDto implements Serializable {
    private static final long serialVersionUID = 1L;

    @ApiModelProperty("구분(0:현재,1:어제,2:지난주)")
    @JsonProperty("prcn_type")
    private Integer PRCN_TYPE;

    @ApiModelProperty("현재 가공시각")
    @JsonProperty("prcn_tm")
    private String PRCN_TM;

    @ApiModelProperty("정보제공구간전체개수")
    @JsonProperty("total")
    private Integer TOTAL;

    @ApiModelProperty("현재 원활비율")
    @JsonProperty("rate_ltc1")
    private Double rateLtc1;

    @ApiModelProperty("현재 지체비율")
    @JsonProperty("rate_ltc2")
    private Double rateLtc2;

    @ApiModelProperty("현재 정체비율")
    @JsonProperty("rate_ltc3")
    private Double rateLtc3;

    @ApiModelProperty("현재 정보없음비율")
    @JsonProperty("rate_ltc0")
    private Double rateLtc0;

    @ApiModelProperty("가공시간대별 소통등급 개수")
    @JsonProperty("traf")
    private List<TrafPrcnStatVo> traf;

    //@ApiModelProperty("운영자개입")
    //@JsonProperty("oper01")
    @JsonIgnore
    private Integer oper01;

    //@ApiModelProperty("UTIS1")
    //@JsonProperty("utis01")
    @JsonIgnore
    private Integer utis01;

    //@ApiModelProperty("UTIS2")
    //@JsonProperty("utis02")
    @JsonIgnore
    private Integer utis02;

    @ApiModelProperty("VDS")
    @JsonProperty("vds01")
    private Integer vds01;

    @ApiModelProperty("DSRC")
    @JsonProperty("dsrc01")
    private Integer dsrc01;

    @ApiModelProperty("경기도")
    @JsonProperty("gits01")
    private Integer gits01;

    @ApiModelProperty("민간정보")
    @JsonProperty("moct01")
    private Integer moct01;

    @ApiModelProperty("가공시간대별 수집원 개수")
    @JsonProperty("clct")
    private List<TrafClctStatVo> clct;

    public void calcGradeRate(String PRCN_TM, int ltc1, int ltc2, int ltc3) {
        this.PRCN_TM = PRCN_TM;
        if (this.TOTAL == 0 || (ltc1+ltc2+ltc3) == 0) {
            this.rateLtc0 = 100.;
            this.rateLtc1 = 0.;
            this.rateLtc2 = 0.;
            this.rateLtc3 = 0.;
            return;
        }
        int ltc0 = Math.max(0, this.TOTAL - ltc1 - ltc2 - ltc3);
        float total = (float)this.TOTAL;
        double tLtc0 = Double.parseDouble(String.format("%.1f", ltc0 / total * 100.0));
        double tLtc1 = Double.parseDouble(String.format("%.1f", ltc1 / total * 100.0));
        double tLtc2 = Double.parseDouble(String.format("%.1f", ltc2 / total * 100.0));
        double tLtc3 = Double.parseDouble(String.format("%.1f", ltc3 / total * 100.0));
        double fSum = tLtc0 + tLtc1 + tLtc2 + tLtc3;
        double[] rates = { tLtc0, tLtc1, tLtc2, tLtc3 };
        if (fSum > 100.0) {
            for (int ii = 0; ii < 4; ii++) {
                if (rates[ii] > 0.0) {
                    rates[ii] -= (fSum - 100.0);
                    break;
                }
            }
        }
        if (fSum < 100.0) {
            for (int ii = 0; ii < 4; ii++) {
                if (rates[ii] > 0.0) {
                    rates[ii] += (100.0 - fSum);
                    break;
                }
            }
        }
        this.rateLtc0 = rates[0];
        this.rateLtc1 = rates[1];
        this.rateLtc2 = rates[2];
        this.rateLtc3 = rates[3];
    }

    public static TrafPrcnStatDto initBuilder(Integer PRCN_TYPE, Integer TOTAL) {
        return TrafPrcnStatDto.builder()
                .PRCN_TYPE(PRCN_TYPE)
                .TOTAL(TOTAL)
                .rateLtc0(0.)
                .rateLtc1(0.)
                .rateLtc2(0.)
                .rateLtc3(0.)
                .PRCN_TM("")
                .traf(new ArrayList<>())
                .oper01(0)
                .utis01(0)
                .utis02(0)
                .vds01(0)
                .dsrc01(0)
                .gits01(0)
                .moct01(0)
                .clct(new ArrayList<>())
                .build();
    }

    @Data
    @Builder
    public static class TrafPrcnStatVo implements Serializable {
        private static final long serialVersionUID = 1L;

        //@ApiModelProperty("구분(0:현재,1:어제,2:지난주)")
        //@JsonProperty("prcn_type")
        @JsonIgnore
        private Integer PRCN_TYPE;

        @ApiModelProperty("가공시각")
        @JsonProperty("prcn_tm")
        private String PRCN_TM;

        @ApiModelProperty("정보없음갯수")
        @JsonProperty("ltc0")
        private Integer LTC0;

        @ApiModelProperty("원활갯수")
        @JsonProperty("ltc1")
        private Integer LTC1;

        @ApiModelProperty("지체갯수")
        @JsonProperty("ltc2")
        private Integer LTC2;

        @ApiModelProperty("정체갯수")
        @JsonProperty("ltc3")
        private Integer LTC3;
    }

    @Data
    @Builder
    public static class TrafClctStatVo implements Serializable {
        private static final long serialVersionUID = 1L;

        @ApiModelProperty("가공시각")
        @JsonProperty("prcn_tm")
        private String PRCN_TM;

        //@ApiModelProperty("운영자개입")
        //@JsonProperty("oper01")
        @JsonIgnore
        private Integer oper01;

        //@ApiModelProperty("UTIS1")
        //@JsonProperty("utis01")
        @JsonIgnore
        private Integer utis01;

        //@ApiModelProperty("UTIS2")
        //@JsonProperty("utis02")
        @JsonIgnore
        private Integer utis02;

        @ApiModelProperty("VDS")
        @JsonProperty("vds01")
        private Integer vds01;

        @ApiModelProperty("DSRC")
        @JsonProperty("dsrc01")
        private Integer dsrc01;

        @ApiModelProperty("경기도")
        @JsonProperty("gits01")
        private Integer gits01;

        @ApiModelProperty("민간정보")
        @JsonProperty("moct01")
        private Integer moct01;

    }

}
