package egovframework.com.its.web.server.service;

import egovframework.com.its.web.server.mapper.CommonMapper;
import egovframework.com.its.web.server.mapper.TrafficStatsMapper;
import egovframework.com.its.web.server.vo.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Base64;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RequiredArgsConstructor
@Service
public class CommonService {

    private final CommonMapper commonMapper;
    private final TrafficStatsService statsService;

    public WeatherInfoVO getWeatherInfo() {
        return this.commonMapper.getWeatherInfo();
    }

    public List<RoadVertexArrVO> getVertexArr() {
        return this.commonMapper.getVertexArr();
    }

    public List<VmsCtlrVO> getVmsCtlr() {
        return this.commonMapper.getVmsCtlr();
//        List<VmsCtlrVO> vmsPhaseImgList = this.commonMapper.getVmsPhaseImgList();
//        return vmsCtlrVO;
    }

    public List<CctvCtlrVO> getCctvCtlr() {
        return this.commonMapper.getCctvCtlr();
    }

    public CctvCtlrVO getOneCctvCtlr(String nmbr) {
        return this.commonMapper.getOneCctvCtlr(nmbr);
    }

    public List<WcamCtlrVO> getWcamCtlr() {
        return this.commonMapper.getWcamCtlr();
    }

    public List<IncdVO> getIncdOcrr() {
        return this.commonMapper.getIncdOcrr();
    }

    public List<AtrdVO> getAtrdNm() {
        return this.commonMapper.getAtrdNm();
    }

    @Transactional(rollbackFor = Exception.class)
    public void addUpdateLogWeb(Map<String, Object> paramMap) {
        this.commonMapper.addUpdateHourLogWeb(paramMap);
        this.commonMapper.addUpdateDateLogWeb(paramMap);
        this.commonMapper.addUpdateMonthLogWeb(paramMap);
    }

    public List<RoadVertexArrVO> getLinkVertexArr() {

        return this.commonMapper.getLinkVertexArr();
    }
//	public List<RoadVertexArrVO> getLinkVertexArr(Map<String, Object> paramMap) {
//
//		return this.commonMapper.getLinkVertexArr(paramMap);
//	}

    
    public List<RoadVertexArrVO> getIfscVertexArr() {

        return this.commonMapper.getIfscVertexArr();
    }
//	public List<RoadVertexArrVO> getIfscVertexArr(Map<String, Object> paramMap) {
//
//		return this.commonMapper.getIfscVertexArr(paramMap);
//	}

    
    public List<RoadVertexArrVO> getRoadVertexArr() {

        return this.commonMapper.getRoadVertexArr();
    }
//	public List<RoadVertexArrVO> getRoadVertexArr(Map<String, Object> paramMap) {
//
//		return this.commonMapper.getRoadVertexArr(paramMap);
//	}


    
    public List<VmsCtlrVO> getVmsImgTxt(String vms_ctlr_nmbr) {
        return this.commonMapper.getVmsImgTxt(vms_ctlr_nmbr);
    }

    
    public VisitCountVO getVisitCount() {
        return this.commonMapper.getVisitCount();
    }

//    public Map<String, String> downExcel(String[] ixrIds, String from, String to, String type) throws IOException {
//        Map<String, String> resultMap = new HashMap<>();
//        List<ScIxrTrafficVO> list = statsService.getIxrTrafficStat(ixrIds, from, to, type);
//        String success = "F";
//        String message = "교차로 교통량 통계 정보를 확인 할 수 없습니다.";
//        if (list != null && list.size() > 0) {
//            Workbook workbook = new SXSSFWorkbook();
//            Sheet sheet = workbook.createSheet("교차로 교통량 통계"); // 시트명 지정
//            Row headerRow = sheet.createRow(0);
//            String[] columns = { "ID", "교차로명", "카메라방향", "방향", "통계시각", "좌회전(소형)", "좌회전(중형)", "좌회전(대형)", "직진(소형)",
//                    "직진(중형)", "직진(대형)", "우회전(소형)", "우회전(중형)", "우회전(대형)", "유턴(소형)", "유턴(중형)", "유턴(대형)", "합계"};
//            for (int i = 0; i < columns.length; i++) {
//                Cell cell = headerRow.createCell(i);
//                cell.setCellValue(columns[i]);
//            }
//            int rowNum = 1;
//            for (ScIxrTrafficVO vo : list) {
//                Row row = sheet.createRow(rowNum++);
//                String clctDt = vo.getClctDt();
//                if (type.equals("MN")) {
//                    DateTimeFormatter inputFormatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
//                    DateTimeFormatter outputFormatter = DateTimeFormatter.ofPattern("yyyy년 MM월");
//                    LocalDate dateTime = LocalDate.parse(clctDt, inputFormatter);
//                    clctDt = dateTime.format(outputFormatter);
//                }
//                else if (type.equals("DD")) {
//                    DateTimeFormatter inputFormatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
//                    DateTimeFormatter outputFormatter = DateTimeFormatter.ofPattern("yyyy/MM/dd");
//                    LocalDate dateTime = LocalDate.parse(clctDt, inputFormatter);
//                    clctDt = dateTime.format(outputFormatter);
//                }
//                else {
//                    DateTimeFormatter inputFormatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
//                    DateTimeFormatter outputFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
//                    LocalDateTime dateTime = LocalDateTime.parse(clctDt, inputFormatter);
//                    clctDt = dateTime.format(outputFormatter);
//                }
//
//                row.createCell(0).setCellValue(vo.getIxrId());
//                row.createCell(1).setCellValue(vo.getIxrNm());
//                row.createCell(2).setCellValue(vo.getCmraNm());
//                row.createCell(3).setCellValue(vo.getDrctDvsnCdR());
//                row.createCell(4).setCellValue(clctDt);
//                row.createCell(5).setCellValue(vo.getSmalLeftTfvl());
//                row.createCell(6).setCellValue(vo.getMddlLeftTfvl());
//                row.createCell(7).setCellValue(vo.getLrgLeftTfvl());
//                row.createCell(8).setCellValue(vo.getSmalGoTfvl());
//                row.createCell(9).setCellValue(vo.getMddlGoTfvl());
//                row.createCell(10).setCellValue(vo.getLrgGoTfvl());
//                row.createCell(11).setCellValue(vo.getSmalRghtTfvl());
//                row.createCell(12).setCellValue(vo.getMddlRghtTfvl());
//                row.createCell(13).setCellValue(vo.getLrgRghtTfvl());
//                row.createCell(14).setCellValue(vo.getSmalUturnTfvl());
//                row.createCell(15).setCellValue(vo.getMddlUturnTfvl());
//                row.createCell(16).setCellValue(vo.getLrgUturnTfvl());
//                row.createCell(17).setCellValue(vo.getTotTfvl());
//            }
//            try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
//                workbook.write(outputStream);
//                workbook.close();
//
//                // Base64 인코딩
//                String base64Encoded = Base64.getEncoder().encodeToString(outputStream.toByteArray());
//
//                success = "S";
//                message = "엑셀 파일 전송 완료.";
//                resultMap.put("success", success);
//                resultMap.put("message", message);
//                resultMap.put("base64", base64Encoded);
//            } catch (IOException e) {
//                resultMap.put("success", "F");
//                resultMap.put("message", e.getMessage());
//            }
//        }
//        else {
//
//            resultMap.put("success", success);
//            resultMap.put("message", message);
//        }
//
//        return resultMap;
//    }
}