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.intentAnalysis.service.IntentAnalysisService;
import com.icomsys.main_vm.biz.rcp.monitoring.req.IntentAnalysisListVoReq;
import com.icomsys.main_vm.biz.rcp.monitoring.service.MonitoringConsultingService;
import com.icomsys.main_vm.biz.rcp.monitoring.vo.IntentAnalysisVO;
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.MonitoringIntentAnalysisVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.DecimalFormat;
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 MonitoringIntentAnalysisController {

    private final IntentAnalysisService intentAnalysisService;

    private final MonitoringConsultingService monitoringConsultingService;
    private final ExcelService excelService;

    private final LogService logService;

    @GetMapping("/intentAnalysis/manage.do")
    public String intentAnalysis(@RequestParam(value = "oprMngCode", required = false, defaultValue = "")String oprMngCode,
            @RequestParam(value = "callInitDateFrom", required = false, defaultValue = "")String callInitDateFrom,
            @RequestParam(value = "callInitDateTo", required = false, defaultValue = "")String callInitDateTo,
            @RequestParam(value = "intentId", required = false, defaultValue = "")String intentId, Model model){

        model.addAttribute("oprMngCode", oprMngCode);
        model.addAttribute("callInitDateFrom", callInitDateFrom);
        model.addAttribute("callInitDateTo", callInitDateTo);
        model.addAttribute("intentId", intentId);

        return "/adm/rcp/monitoring/intentAnalysis";
    }

    @RequestMapping(value = "/intentAnalysis/intentnamelist.do", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public List<HashMap> selectIntentNameList(@RequestBody MonitoringIntentAnalysisVO vo) {
        if (vo.getServiceGroup() != null) {
            if (!"all".equals(vo.getOprMngCode())) {
                String serviceGroupUpper = intentAnalysisService.selectServiceGroupUpper(vo);

                if (!serviceGroupUpper.equals(vo.getServiceGroup())) {
                    List<String> oprList = new ArrayList<>();

                    vo.setServiceGroup(serviceGroupUpper);
                    String commonOpr = intentAnalysisService.selectOprByServiceCode(vo);

                    oprList.add(vo.getOprMngCode());
                    oprList.add(commonOpr);

                    vo.setOprMngCodeList(oprList);
                    vo.setOprMngCode("all");
                }
            }
        }

        List<HashMap> result = intentAnalysisService.selectIntentNameList(vo);

        return result;
    }

    @RequestMapping(value = "/intentAnalysis/list.do", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public DslDataTableRes<HashMap> intentAnalysisList(@RequestBody MonitoringIntentAnalysisVO vo) {
        vo.setLimit(vo.getPage() * vo.getPageSize());
        vo.setOffset(vo.getPageSize() * 10);
        List<HashMap> result = intentAnalysisService.selectIntentAnalysisList(vo);
        // int resultCnt = intentAnalysisService.selectIntentAnalysisListCnt(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 = "/intentAnalysis/chklistcnt.do", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public int chkListCnt(@RequestBody MonitoringIntentAnalysisVO vo) {
        return intentAnalysisService.selectIntentAnalysisListCnt(vo);
    }

    @ResponseBody
    @Transactional
    @PostMapping("/intentAnalysis/exceldownload.do")
    public void downloadExcel(HttpServletResponse response, @ModelAttribute IntentAnalysisListVoReq req) throws IOException {

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

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

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

        List<HashMap> result = intentAnalysisService.selectIntentAnalysisList(req.getMonitoringIntentAnalysisVO());

        List<IntentAnalysisVO> intentAnalysisVOList = new ArrayList<>();

        if (result.size() > 0) {
            for (Map e : result) {
                IntentAnalysisVO vo = new IntentAnalysisVO();
                vo.setSeq(String.valueOf(new DecimalFormat("#").format(e.get("rownum"))));
                vo.setTalkText(String.valueOf(e.get("userText")));
                vo.setIntentName(String.valueOf(e.get("intentName")));

                if ("-".equals(e.get("score"))) {
                    vo.setScore(String.valueOf(e.get("score")));
                } else {
                    vo.setScore(e.get("score") + "%");
                }

                vo.setIntentResult(String.valueOf(e.get("intentResultCodeName")));
                vo.setCallInitDate(String.valueOf(e.get("callInitDate")));

                intentAnalysisVOList.add(vo);
            }
        } else {
            IntentAnalysisVO vo = new IntentAnalysisVO();
            vo.setSeq("");
            vo.setTalkText("");
            vo.setIntentName("");
            vo.setScore("");
            vo.setIntentResult("");
            vo.setCallInitDate("");

            intentAnalysisVOList.add(vo);
        }

        monitoringConsultingService.ExcelDownload("인텐트_분석_관리" + nowTime, intentAnalysisVOList, req.getTitleList());
//        excelService.ExcelDownload("인텐트_분석_관리" + nowTime, intentAnalysisVOList, req.getTitleList());

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

}
