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.login.service.LoginService;
import com.icomsys.main_vm.biz.common.system.vo.DslDataTableRes;
import com.icomsys.main_vm.biz.rcp.monitoring.req.ConversationMarkListVoReq;
import com.icomsys.main_vm.biz.rcp.monitoring.service.MonitoringConsultingService;
import com.icomsys.main_vm.biz.rcp.monitoring.service.MonitoringConversationMarkService;
import com.icomsys.main_vm.biz.rcp.monitoring.vo.ConversationMarkVO;
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.MonitoringConversationMarkVO;
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.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 MonitoringConversationMarkController {

    private final LoginService loginService;

    private final LogService logService;

    private final MonitoringConsultingService monitoringConsultingService;
    private final ExcelService excelService;

    private final MonitoringConversationMarkService monitoringConversationMarkService;

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

    @RequestMapping(value = "/conversationMark/list.do", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public DslDataTableRes<HashMap> conversationMarkList(@RequestBody MonitoringConversationMarkVO vo) {
        vo.setLimit(vo.getPage() * vo.getPageSize());
        vo.setOffset(vo.getPageSize() * 10);

        List<HashMap> result = monitoringConversationMarkService.selectBookmarkList(vo);

//        int resultCnt = monitoringConversationMarkService.selectBookmarkListCnt(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 = "/conversationMark/insert.do", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public int insertConversationMark(@RequestBody MonitoringConversationMarkVO vo) {
        int result = monitoringConversationMarkService.insertBookmark(vo);

        return result;
    }

    @RequestMapping(value = "/conversationMark/update.do", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public int updateConversationMark(@RequestBody MonitoringConversationMarkVO vo) {
        vo.setUpdateId(loginService.getUserVo().getUserId());

        int result = monitoringConversationMarkService.updateBookmark(vo);

        return result;
    }

    @RequestMapping(value = "/conversationMark/checkbookmark.do", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public HashMap checkBookmark(@RequestBody MonitoringConversationMarkVO vo) {
        HashMap result = monitoringConversationMarkService.checkBookmarkCnt(vo);

        return result;
    }

    @RequestMapping(value = "/conversationMark/chklistcnt.do", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public int chkListCnt(@RequestBody MonitoringConversationMarkVO vo) {
        return monitoringConversationMarkService.selectBookmarkListCnt(vo);
    }

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

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

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

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

        List<HashMap> result = monitoringConversationMarkService.selectBookmarkList(req.getMonitoringConversationMarkVO());

        List<ConversationMarkVO> conversationMarkVOList = new ArrayList<>();

        if (result.size() > 0) {
            for (Map e : result) {
                ConversationMarkVO vo = new ConversationMarkVO();
                vo.setSeq(String.valueOf(new DecimalFormat("#").format(e.get("rownum"))));
                vo.setCallInitDate(String.valueOf(e.get("callInitDate")));
                vo.setErrCodeType(String.valueOf(e.get("bookmarkErrCodeName")));
                vo.setMeasureType(String.valueOf(e.get("measureTypeName")));
                vo.setTalkText(String.valueOf(e.get("talkText")));

                conversationMarkVOList.add(vo);
            }
        } else {
            ConversationMarkVO vo = new ConversationMarkVO();
            vo.setSeq("");
            vo.setCallInitDate("");
            vo.setErrCodeType("");
            vo.setMeasureType("");
            vo.setTalkText("");

            conversationMarkVOList.add(vo);
        }

        monitoringConsultingService.ExcelDownload("대화책갈피" + nowTime, conversationMarkVOList, req.getTitleList());
//        excelService.ExcelDownload("대화책갈피" + nowTime, conversationMarkVOList, req.getTitleList());

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

    //인텐트 예문 추가 팝업//////////////////////////////////////////////////////
    @GetMapping(value = "/intent/addexamplepopup.do")
    public String addIntentExamplePopup() {
        return "adm/include/addIntentExample";
    }

}
