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

import com.icomsys.main_vm.biz.advice.excep.CustomNotFoundException;
import com.icomsys.main_vm.biz.advice.excep.CustomBadRequestException;
import com.icomsys.main_vm.biz.common.system.vo.DslDataTableRes;
import com.icomsys.main_vm.biz.common.system.vo.SystemCommoncodeRes;
import com.icomsys.main_vm.biz.rcp.learningTest.service.LearningTestSimulatorService;
import com.icomsys.main_vm.biz.rcp.learningTest.vo.LearningTestDummyApiVO;
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.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

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

    private final LearningTestSimulatorService learningTestSimulatorService;

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

    /**
     * @Name callSimapi
     * @Description 시뮬레이터 API 호출
     * @Author 문다운
     * @CreateDate 2022. 6. 20.
     */
    @RequestMapping(value = "/simulator/call-api.do", method = RequestMethod.GET)
    @ResponseBody
    public ModelAndView callSimapi(HttpServletRequest request, @RequestParam String oprMngCode, String callId, String token, String userTalk, String methodType, String dnis) {

        return learningTestSimulatorService.callSimulator(oprMngCode, callId, token, userTalk, methodType, dnis);
    }


    /**
     * @Name custCallNumList
     * @Description 전화 목록 가져오기
     * @Author dongheejang
     * @CreateDate 2023. 3. 14.
     */
    @RequestMapping(value = "/simulator/custCallNumlist.do", method = RequestMethod.POST)
    @ResponseBody
    public DslDataTableRes<SystemCommoncodeRes> custCallNumList (HttpServletRequest request, @RequestBody Map<Object, String> dataMap) {

        LoginVO resultVO = (LoginVO) request.getSession().getAttribute("LoginVO");
        QueryResults<SystemCommoncodeRes> result = learningTestSimulatorService.selectCustCallNum(resultVO, dataMap);
        return new DslDataTableRes<SystemCommoncodeRes>(result);
    }

    @GetMapping("/simulator/select/dummy")
    @ResponseBody
    public LearningTestDummyApiVO selectDummyApi(@RequestParam(name = "seq", required = true)Long apiSeq) throws CustomNotFoundException {

        return learningTestSimulatorService.selectDummyApi(apiSeq);
    }

    @PostMapping("/simulator/save/dummy")
    @ResponseBody
    public int saveDummyApi(@RequestBody LearningTestDummyApiVO dto) throws CustomNotFoundException, CustomBadRequestException {

        return learningTestSimulatorService.saveDummyApi(dto);
    }
}
