package com.its.op.service.its.rse;

import com.its.op.dao.mapper.its.rse.RseOdStatHhMapper;
import com.its.op.dao.repository.its.rse.TbRseCtlrRepository;
import com.its.op.dto.its.rse.RseOdStatDto;
import com.its.op.dto.its.rse.RseOdStatListDataDto;
import com.its.op.dto.its.rse.RseOdStatListDto;
import com.its.op.entity.its.rse.TbRseCtlr;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Slf4j
@RequiredArgsConstructor
@Service
public class TbRseOdStatHhService {

    private final TbRseCtlrRepository repo;
    private final RseOdStatHhMapper mapper;

    /**
     * RSE OD 교통량 통계(RSE 기준 출발-도착 교통량)
     * @param fromDt
     * @param toDt
     * @return
     */
    @Transactional(readOnly = true)
    public List<RseOdStatDto> findAllOdListByDateRange(String fromDt, String toDt) {
        Map<String, String> param = new HashMap<>();
        param.put("FROM_DT", fromDt);
        param.put("TO_DT", toDt);
        return this.mapper.findAllOdListByDateRange(param);
    }

    /**
     * RSE OD 교통량 통계(출발 RSE, 도착 RSE 교통량)
     * @param fromDt
     * @param toDt
     * @return
     */
    @Transactional(readOnly = true)
    public List<RseOdStatListDto> findAllListByDateRange(String fromDt, String toDt) {
        Map<String, String> param = new HashMap<>();
        param.put("FROM_DT", fromDt);
        param.put("TO_DT", toDt);
        return this.mapper.findAllListByDateRange(param);
    }

    /**
     * RSE OD 교통량 통계(출발 RSE, 도착 RSE 교통량 - 목록으로 만들어서 리턴)
     * @param fromDt
     * @param toDt
     * @return
     */
    @Transactional(readOnly = true)
    public List<RseOdStatListDataDto> findAllListDataByDateRange(String fromDt, String toDt) {
        List<RseOdStatListDataDto> result = new ArrayList<>();
        Map<Long, RseOdStatListDataDto> resultMap = new HashMap<>();
        List<TbRseCtlr> lists = this.repo.findAllListOrder();
        log.info("{}", lists);

        lists.forEach(obj -> {
            RseOdStatListDataDto dto = RseOdStatListDataDto.makeRseList(obj.getRseCtlrNmbr(), obj.getRseNm(), lists);
            resultMap.put(obj.getRseCtlrNmbr(), dto);
        });

        Map<String, String> param = new HashMap<>();
        param.put("FROM_DT", fromDt);
        param.put("TO_DT", toDt);
        List<RseOdStatListDto> data = this.mapper.findAllListByDateRange(param);
        data.forEach(obj -> {
            RseOdStatListDataDto dto = resultMap.get(obj.getF_RSE_CTLR_NMBR());
            if (dto != null) {
                RseOdStatListDataDto.RseOdTraf vo = dto.getMap().get(obj.getT_RSE_CTLR_NMBR());
                if (vo != null) {
                    vo.setTFVL(obj.getTFVL());
                }
            }
        });

        for (Long key : resultMap.keySet()) {
            // 맵을 리스트로 변환
            resultMap.get(key).mapToList();
        }

        // 키로 정렬
//        Object[] keys = resultMap.keySet().toArray();
//        Arrays.sort(keys);
//        for (Long key : resultMap.keySet()) {
//            result.add(resultMap.get(key));
//        }
        result = new ArrayList<>(resultMap.values());
//        JSONArray jArray = new JSONArray(result);
//        JSONObject jObject = new JSONObject();
//        JSONObject jObject = new JSONObject(new TreeMap());
//        jObject.put("list", jArray);
//        System.out.println(jObject.toString());

//        List<RseOdStatListDataDto> result = resultMap.values();
//        Collections.sort(result, RseOdStatListDataDto.RseOdStatListDataDtoSort);
//        Collections.sort(result);
        return result;
    }
}
