package com.icomsys.main_vm.biz.rcp.monitoring.controller;

import com.icomsys.main_vm.biz.common.common.service.ExcelService;
import com.icomsys.main_vm.biz.common.common.service.LogService;
import com.icomsys.main_vm.biz.common.common.service.LogVO;
import com.icomsys.main_vm.biz.common.system.vo.DslDataTableRes;
import com.icomsys.main_vm.biz.rcp.monitoring.req.OutScenarioListVoReq;
import com.icomsys.main_vm.biz.rcp.monitoring.service.MonitoringConsultingService;
import com.icomsys.main_vm.biz.rcp.monitoring.service.MonitoringOutscenarioService;
import com.icomsys.main_vm.biz.rcp.monitoring.vo.OutScenarioVO;
import com.icomsys.main_vm.biz.rcp.statistics.intent.req.IntentAnalysisItemListReq;
import com.icomsys.main_vm.common.code.enumresource.ActionResource;
import com.icomsys.main_vm.common.code.enumresource.MenuResource;
import com.icomsys.main_vm.db.mybatis.alias.MonitoringOutscenarioVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@Slf4j
@RequiredArgsConstructor
@RequestMapping("/admin/rcp/statistics/monitoring")
public class MonitoringOutscenarioController {

    private final MonitoringOutscenarioService monitoringOutscenarioService;

    private final MonitoringConsultingService monitoringConsultingService;
    private final ExcelService excelService;

    private final LogService logService;

    @GetMapping("/outscenario/manage.do")
    public String outscenario(){
        return "/adm/rcp/monitoring/outscenario";
    }

    @RequestMapping(value = "/outscenario/outscenariolist.do", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public DslDataTableRes<HashMap> outscenarioList(@RequestBody MonitoringOutscenarioVO vo) {
        vo.setLimit(vo.getPage() * vo.getPageSize());
        vo.setOffset(vo.getPageSize() * 10);
        List<HashMap> result = monitoringOutscenarioService.selectOutscenarioList(vo);

        // int resultCnt = monitoringOutscenarioService.selectOutscenarioListCnt(vo);

        return new DslDataTableRes<HashMap>(result, Long.valueOf(vo.getPage() * vo.getPageSize() + result.size()), Long.valueOf(vo.getPageSize()), Long.valueOf(vo.getPage() * vo.getPageSize()));
    }

    @RequestMapping(value = "/outscenario/scenarionamelist.do", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public List<HashMap> scenarioNameList(@RequestBody MonitoringOutscenarioVO vo) {
        List<HashMap> result = monitoringOutscenarioService.selectScenarioNameList(vo);

        return result;
    }

    @RequestMapping(value = "/outscenario/tokenlist.do", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public List<String> tokenlist(@RequestBody MonitoringOutscenarioVO vo) {
        List<String> result = monitoringOutscenarioService.selectTokenListByScenario(vo);

        return result;
    }

    @RequestMapping(value = "/outscenario/calllist.do", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public List<HashMap> calllist(@RequestBody MonitoringOutscenarioVO vo) {
        List<HashMap> result = monitoringOutscenarioService.selectCallListByScenario(vo);

        return result;
    }

    @RequestMapping(value = "/outscenario/chklistcnt.do", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public int chkListCnt(@RequestBody MonitoringOutscenarioVO vo) {
        return monitoringOutscenarioService.selectOutscenarioListCnt(vo);
    }

    @ResponseBody
    @Transactional
    @RequestMapping(value = "/outscenario/exceldownload.do")
    public void outscenarioExcelDownload(HttpServletResponse response, @ModelAttribute OutScenarioListVoReq req) throws IOException {

        SimpleDateFormat sdf = new SimpleDateFormat("yy-MM-dd/HH:mm:ss");

        String nowTime = sdf.format(System.currentTimeMillis());

        req.getMonitoringOutscenarioVO().setPage(-1);

        List<HashMap> result = monitoringOutscenarioService.selectOutscenarioList(req.getMonitoringOutscenarioVO());

        List<OutScenarioVO> outScenarioVOList = new ArrayList<>();

        if (result.size() > 0) {
            for (Map e : result) {
                OutScenarioVO vo = new OutScenarioVO();
                vo.setOprMngCodeName(String.valueOf(e.get("oprMngCodeName")));
                vo.setScenarioName(e.get("scenarioName") + " (" + e.get("scenarioCnt") + "건)");
                vo.setNodeName(e.get("nodeName") + " (" + e.get("nodeType") + ") (" + e.get("nodeCnt") + "건)");
                vo.setNodePercent(Math.round(Double.valueOf(String.valueOf(e.get("nodeCnt")))/Double.valueOf(String.valueOf(e.get("scenarioCnt")))*1000)/10.0 + "%");

                outScenarioVOList.add(vo);
            }
        } else {
            OutScenarioVO vo = new OutScenarioVO();
            vo.setOprMngCodeName("");
            vo.setScenarioName("");
            vo.setNodeName("");
            vo.setNodePercent("");

            outScenarioVOList.add(vo);
        }

        monitoringConsultingService.ExcelDownload("시나리오_이탈_노드_관리" + nowTime, outScenarioVOList, req.getTitleList());
//        excelService.ExcelDownload("시나리오_이탈_노드_관리" + nowTime, outScenarioVOList, req.getTitleList());

        logService.LogSave(LogVO.builder()
                .menuAuth(MenuResource.valueOf(req.getMenuVal()))
                .actionCode(String.valueOf((ActionResource.download)))
                .serviceGroup(req.getOprMngCode())
                .remark("엑셀 다운로드")
                .build());
    }

}
