package com.its.op.dto.its.rse;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.its.op.entity.its.rse.TbRseCtlr;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Builder;
import lombok.Data;

import java.io.Serializable;
import java.util.*;

/**
 * RSE OD 교통량 시간 통계 DTO Class
 */
@Data
@Builder
@ApiModel("RseOdStatListDataDto(RSE OD 교통량 시간 통계)")
public class RseOdStatListDataDto implements Serializable {
    private static final long serialVersionUID = 1L;

    @ApiModelProperty("RSE 제어기 번호")
    @JsonProperty("rse_ctlr_nmbr")
    private Long RSE_CTLR_NMBR;

    @ApiModelProperty("RSE 명")
    @JsonProperty("rse_nm")
    private String RSE_NM;

    @ApiModelProperty("TO RSE 교통량정보 목록")
    @JsonProperty("lists")
    private List<RseOdTraf> lists;

    @JsonIgnore
    private Map<Long, RseOdTraf> map;

    public void mapToList() {
        this.lists = new ArrayList<>(this.map.values());
//        if (this.lists == null) {
//            this.lists = new ArrayList<>();
//        }
//        // 키로 정렬
//        Object[] keys = this.map.keySet().toArray();
//        Arrays.sort(keys);
//        for (Long key : this.map.keySet()) {
//            this.lists.add(this.map.get(key));
//        }
    }

    public static RseOdStatListDataDto makeRseList(Long RSE_CTLR_NMBR, String RSE_NM, List<TbRseCtlr> lists) {
        RseOdStatListDataDto dto = RseOdStatListDataDto.builder()
                .RSE_CTLR_NMBR(RSE_CTLR_NMBR)
                .RSE_NM(RSE_NM)
                .build();
        dto.setLists(new ArrayList<>());
        dto.setMap(new HashMap<>());
        lists.forEach(obj -> {
            dto.getMap().put(obj.getRseCtlrNmbr(), RseOdTraf.builder()
                    .RSE_CTLR_NMBR(obj.getRseCtlrNmbr())
                    .RSE_NM(obj.getRseNm())
                    .TFVL(0)
                    .build());
        });
        return dto;
    }

    public static Comparator<RseOdStatListDataDto> RseOdStatListDataDtoSort = new Comparator<RseOdStatListDataDto>() {
        public int compare(RseOdStatListDataDto s1, RseOdStatListDataDto s2) {
            return (int) (s1.getRSE_CTLR_NMBR() - s2.getRSE_CTLR_NMBR());
        }
    };

    public static Comparator<RseOdStatListDataDto.RseOdTraf> RseOdTrafSort = new Comparator<RseOdStatListDataDto.RseOdTraf>() {
        public int compare(RseOdStatListDataDto.RseOdTraf s1, RseOdStatListDataDto.RseOdTraf s2) {
            return (int) (s1.getRSE_CTLR_NMBR() - s2.getRSE_CTLR_NMBR());
        }
    };

    @Data
    @Builder
    public static class RseOdTraf {
        @ApiModelProperty("RSE 제어기 번호")
        @JsonProperty("rse_ctlr_nmbr")
        private Long RSE_CTLR_NMBR;

        @ApiModelProperty("RSE 명")
        @JsonProperty("rse_nm")
        private String RSE_NM;

        @ApiModelProperty("교통량합")
        @JsonProperty("tfvl")
        private Integer TFVL;
    }
}
