package com.icomsys.main_vm.biz.rcp.conversation.nlp.wisenut.service.impl;

import com.google.gson.Gson;
import com.icomsys.main_vm.biz.rcp.conversation.nlp.wisenut.service.NlpWisenutProjectService;
import com.icomsys.main_vm.biz.rcp.conversation.nlp.wisenut.service.NlpWisenutService;
import com.icomsys.main_vm.biz.rcp.conversation.nlp.wisenut.vo.NlpWisenutResult;
import com.icomsys.main_vm.db.jpa.entity.system.TbServiceGrouop;
import com.icomsys.main_vm.db.jpa.repo.system.TbServiceGrouopRepo;
import com.icomsys.main_vm.db.mybatis.mapper.NlpWisenutApiMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.icomsys.main_vm.biz.rcp.conversation.nlp.wisenut.util.RestTemplateUtil.callNlpApi;

@Service("nlpWisenutProjectService")
@RequiredArgsConstructor
public class NlpWisenutProjectServiceImpl implements NlpWisenutProjectService {

    @Autowired
    NlpWisenutApiMapper nlpWisenutApiMapper;

    @Autowired
    NlpWisenutService nlpWisenutService;

    private final TbServiceGrouopRepo tbServiceGrouopRepo;

    @Value("${feign.url.nlp}")
    private String nlpApi;

    @Value("${nlpapi}")
    private boolean nlp;

    /**
     * @Name projectUpdateApi
     * @Description 프로젝트 수정 API
     * @Author dongheejang
     * @CreateDate 2022. 06. 20.
     *
     * @param eMap
     * @return NlpWisenutResult
     *
     * @ChangeDescription
     */
    @Override
    public NlpWisenutResult projectUpdateApi(HashMap eMap) {

        if (nlp) {
            Gson gson = new Gson();
            HashMap nMap = new HashMap();

            NlpWisenutResult nlpWisenutResult = new NlpWisenutResult();

            boolean result = true;
            String returnMsg = "";
            String returnCode = "";

            try {
                long projectId = nlpWisenutApiMapper.selectProjectIdforOprMngCode(eMap); // projectId DB에서 가져오기
//                HashMap pMap = nlpWisenutApiMapper.selectProjectName(eMap);
                TbServiceGrouop tbServiceGrouop = tbServiceGrouopRepo.findByCustCodeAndServiceGroup((String) eMap.get("custCode"), (String) eMap.get("serviceCode"));

                HashMap fMap = new HashMap();
                HashMap gMap = new HashMap();

                List<HashMap> fList = new ArrayList<>();
                List<HashMap> gList = new ArrayList<>();
                List<String> fmList = new ArrayList<>();
                List<String> gmList = new ArrayList<>();

                // 필요시 DB에서 data 가져오기
                nMap.put("id",projectId);
                nMap.put("name",tbServiceGrouop.getServiceGroupName());
                nMap.put("summary",tbServiceGrouop.getServiceGroupName());
                nMap.put("simThreshold",Double.parseDouble(eMap.get("similarityCriteria").toString()));
                nMap.put("confidentThreshold",Double.parseDouble(eMap.get("classCriteria").toString()));
                nMap.put("confidentThresholdGap",Double.parseDouble(eMap.get("classResultCriteria").toString()));

                fmList.add(eMap.get("fallbackMsg").toString());
                gmList.add(eMap.get("greetingMsg").toString());
                fMap.put("channelCode","default");
                fMap.put("messages",fmList);
                fList.add(fMap);
                gMap.put("channelCode","default");
                gMap.put("messages",gmList);
                gList.add(gMap);

                nMap.put("fallbackMessages",fList);
                nMap.put("greetingMessages",gList);

                // request param setting
                String path = nlpApi + "/proj/update";
                String body = gson.toJson(nMap);
                HttpMethod method = HttpMethod.POST;

                // api request
                String responseBody = callNlpApi(path, body, method);

                Map<String, Object> responseMap = new HashMap<String, Object>();
                responseMap = gson.fromJson(responseBody, responseMap.getClass());
                returnCode = responseMap.get("returnCode").toString();
                returnMsg = responseMap.get("returnMsg").toString();

                if (!returnCode.equals("000")) {
                    result = false;
                }
            } catch (Exception ex) {
                returnMsg = ex.getMessage();
                result = false;
            }

            nlpWisenutResult.setResult(result);
            nlpWisenutResult.setReturnCode(returnCode);
            nlpWisenutResult.setReturnMsg(returnMsg);

            return nlpWisenutResult;
        } else {
            return nlpWisenutService.defaultResult();
        }
    }
}
