package egovframework.service.impl;

import egovframework.service.TrafficStatsService;
import egovframework.service.mapper.TrafficStatsMapper;
import egovframework.vo.VdsStatsVO;
import lombok.RequiredArgsConstructor;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.egovframe.rte.fdl.cmmn.EgovAbstractServiceImpl;
import org.springframework.core.io.Resource;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequiredArgsConstructor
@Service
public class TrafficStatsServiceImpl extends EgovAbstractServiceImpl implements TrafficStatsService {

    private final TrafficStatsMapper trafficStatsMapper;

    @Override
    public List<Map<String, Object>> getVertexHistoryByRoadText(Map<String, String> paramMap) {
        return this.trafficStatsMapper.getVertexHistoryByRoadText(paramMap);
    }

    @Override
    public List<Map<String, Object>> getAtrdNm() {
        return this.trafficStatsMapper.getAtrdNm();
    }

    @Override
    public List<Map<String, Object>> getVdsCtlrList() {
        return this.trafficStatsMapper.getVdsCtlrList();
    }

    @Override
    public List<VdsStatsVO> getVdsStatsInfo(HashMap<String, String> paramMap) {
        List<VdsStatsVO> result = this.trafficStatsMapper.getVdsStatsInfo(paramMap);

        for (VdsStatsVO vdsVo : result) {
            String[] statDay = vdsVo.getStat_day().split(",");
            String[] tfvl = vdsVo.getTfvl().split(",");

            int[] tfvlArr = new int[24];
            //0으로 초기화..
            Arrays.fill(tfvlArr, 0);

            int tfvlSum = 0;
            for (int ii = 0; ii < statDay.length; ii++) {
                int dyNum = Integer.parseInt(statDay[ii]);
                tfvlArr[dyNum] = Integer.parseInt(tfvl[ii]);
                tfvlSum += Integer.parseInt(tfvl[ii]);
            }

            vdsVo.setTfvlArr(tfvlArr);
            vdsVo.setTfvlSum(tfvlSum);
        }

        return result;
    }

    @Override
    public ResponseEntity<Resource> exportExcel(Map<String, String> paramMap) {
        if (paramMap.get("type") != null) {
            String exportType = paramMap.get("type");
            List<?> result;
            String[] colNames;
            String fileName;
            if (exportType.equals("info")) {
                List<Map<String, Object>> resultMap = this.trafficStatsMapper.getVertexHistoryByRoadText(paramMap);
                colNames = new String[]{"도로명", "구간", "통계시각", "속도", "통행시간"};
                fileName = (String)resultMap.get(0).get("ROAD_NAME");
            }
            else if (exportType.equals("traffic")) {
                result = this.trafficStatsMapper.getVdsStatsInfo(paramMap);
                colNames = new String[25];
                colNames[0] = "구분";
                for (int ii = 0; 24 < ii; ii++) {
                    colNames[(ii+1)] = ii < 10 ? ("0" + ii) : String.valueOf(ii);
                }
                colNames[25] = "합계";
            }

        }
        return null;
    }

    public void exportInfoExcel(Map<String, String> paramMap) {
        List<Map<String, Object>> resultMap = this.trafficStatsMapper.getVertexHistoryByRoadText(paramMap);
        final String[] colNames = {"도로명", "구간", "통계시각", "속도", "통행시간"};
        String fileName = (String)resultMap.get(0).get("ROAD_NAME");
        final int[] colWidths = {3000, 5000, 3000, 3000, 3000};

        XSSFWorkbook workbook = new XSSFWorkbook();
        XSSFSheet sheet = null;
        XSSFCell cell = null;
        XSSFRow row = null;

        Font fontHeader = workbook.createFont();
        fontHeader.setFontName("맑은 고딕");
        fontHeader.setFontHeight((short)(9 * 20));	//사이즈
        Font font9 = workbook.createFont();
        font9.setFontName("맑은 고딕");	//글씨체
        font9.setFontHeight((short)(9 * 20));	//사이즈

        int rowCnt = 0;
        int cellCnt = 0;
        int listCount = resultMap.size();
        String date = paramMap.get("begin")  + " ~ " + paramMap.get("end");
        sheet = workbook.createSheet("소통통계_" + date);
        row = sheet.createRow(rowCnt++);
    }

}
