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

import com.icomsys.main_vm.biz.common.login.res.UserVo;
import com.icomsys.main_vm.biz.common.system.vo.DslDataTableRes;
import com.icomsys.main_vm.biz.common.system.vo.SystemCommoncodeReq;
import com.icomsys.main_vm.biz.common.system.vo.SystemCommoncodeRes;
import com.icomsys.main_vm.biz.rcp.learningTest.SttCode;
import com.icomsys.main_vm.biz.rcp.learningTest.UploadUtil;
import com.icomsys.main_vm.biz.rcp.learningTest.service.LearningTestInSttService;
import com.icomsys.main_vm.db.jpa.entity.system.TbBotCommonCode;
import com.icomsys.main_vm.db.jpa.repo.oprManage.TbBotTtsTagRepo;
import com.icomsys.main_vm.db.jpa.repo.system.TbBotCommonCodeRepo;
import com.icomsys.main_vm.db.mybatis.alias.LoginVO;
import com.querydsl.core.QueryResults;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.Part;
import java.io.IOException;
import java.util.List;


@Controller
@Slf4j
@RequiredArgsConstructor
@RequestMapping("/admin/rcp/learningTest")
public class LearningTestInSttController {

    @Value("${feign.url.selvas}")
    private String sttServer;

    @Value("${feign.url.sttport}")
    private int sttPort;

    @Resource
    MappingJackson2JsonView ajaxMainView;

    private final LearningTestInSttService learningTestInSttService;
    private final TbBotTtsTagRepo tbBotTtsTagRepo;
    private final TbBotCommonCodeRepo tbBotCommonCodeRepo;

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

    @ResponseBody
    @RequestMapping(value = "/stttest/sttproduct.do",method = RequestMethod.GET)
    public DslDataTableRes<SystemCommoncodeRes> sttProduct(HttpServletRequest request, @RequestParam String custCode, String serviceGroup) {

        LoginVO resultVO = (LoginVO) request.getSession().getAttribute("LoginVO");
        UserVo userVo = new UserVo();
        userVo.setRoleVal(resultVO.getAuthCode());
        SystemCommoncodeReq dto = new SystemCommoncodeReq();
        dto.setUserVo(userVo);
        dto.setServiceGroup(serviceGroup);
        dto.setOprmngCode(custCode + "_RCP_" + serviceGroup);
        dto.setCodeType("STT_PRODUCT");
        QueryResults<SystemCommoncodeRes> result = tbBotCommonCodeRepo.SystemCommoncodeProductList(dto);

        return new DslDataTableRes<SystemCommoncodeRes>(result);
    }

    @RequestMapping(value = "/stttest/call-stt.do", method = RequestMethod.POST)
    //@ResponseBody
    public ModelAndView callSttapi(HttpServletRequest request) throws ServletException, IOException {
        ModelAndView mv = new ModelAndView(ajaxMainView);

        request.setCharacterEncoding("UTF-8");

        UploadUtil uploadUtil = UploadUtil.create(request.getServletContext());

        List<Part> parts = (List<Part>) request.getParts();

        String fileName = null;

        for(Part part : parts) {

            if(!part.getName().equals("upFile")) continue; //f로 들어온 Part가 아니면 스킵
            if(part.getSubmittedFileName().equals("")) continue; //업로드 된 파일 이름이 없으면 스킵

            fileName = part.getSubmittedFileName();

            uploadUtil.saveFiles(part, uploadUtil.createFilePath());

        }

//        String result = learningTestInSttService.doTestSvc("172.31.34.52",9999,fileName, 0 );
        String result = learningTestInSttService.doTestSvc(sttServer,sttPort,fileName, 0 );
        SttCode stt = learningTestInSttService.sttCode;
        mv.addObject("code", stt.getResultCode());

        if(stt == SttCode.SUCCESS){
            mv.addObject("result", result);
        }else{
            mv.addObject("result", stt.getResultMessage());
        }

        log.info(String.valueOf(stt.getResultCode()));
        log.info(String.valueOf(stt.getResultMessage()));

        return mv;
    }
}
