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.NlpWisenutEntityService;
import com.icomsys.main_vm.biz.rcp.conversation.nlp.wisenut.service.NlpWisenutService;
import com.icomsys.main_vm.biz.rcp.conversation.nlp.wisenut.util.RestTemplateUtil;
import com.icomsys.main_vm.biz.rcp.conversation.nlp.wisenut.vo.NlpWisenutResult;
import com.icomsys.main_vm.db.mybatis.mapper.NlpWisenutApiMapper;
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;

/**
 * @Name NlpWisenutEntityServiceImpl
 * @Description Entity API
 * @Author dongheejang
 * @CreateDate 2022. 06. 20.
 */

@Service("nlpWisenutEntityService")
public class NlpWisenutEntityServiceImpl  implements NlpWisenutEntityService {

    @Autowired
    NlpWisenutApiMapper nlpWisenutApiMapper;

    @Autowired
    NlpWisenutService nlpWisenutService;

//    @Autowired
//    private ActiveTypeUtil activeTypeUtil;


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

    @Value("${feign.url.cinnamon}")
    private String cinnamonApi;

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

    /**
     * @Name entityInsertApi
     * @Description 엔티티 추가 API
     * @Author dongheejang
     * @CreateDate 2022. 06. 20.
     *
     * @param eMap
     * @return NlpWisenutResult
     *
     * @ChangeDescription
     */
    @Override
    public NlpWisenutResult entityInsertApi(HashMap eMap) {

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

        NlpWisenutResult nlpWisenutResult = new NlpWisenutResult();

        boolean result = true;
        long mappingId = 0;
        String returnMsg = "";
        String returnCode = "";

        try {
            long projectId = nlpWisenutApiMapper.selectProjectId(eMap); // projectId DB에서 가져오기

            nMap.put("projectId",projectId);
            nMap.put("titleWord",eMap.get("repWord"));
            nMap.put("tag","");
            nMap.put("synonyms",eMap.get("list"));

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

            // api request
            String responseBody = RestTemplateUtil.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;
            } else {
                double id = (double) responseMap.get("id");
                mappingId = (long) id;
            }

        } catch (Exception ex) {
            returnMsg = ex.getMessage();
            result = false;
        }

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

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

    /**
     * @Name entityUpdateApi
     * @Description 엔티티 수정 API
     * @Author dongheejang
     * @CreateDate 2022. 06. 20.
     *
     * @param eMap
     * @return NlpWisenutResult
     *
     * @ChangeDescription
     */
    @Override
    public NlpWisenutResult entityUpdateApi(HashMap eMap) {

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

        NlpWisenutResult nlpWisenutResult = new NlpWisenutResult();

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

        try {
            long projectId = nlpWisenutApiMapper.selectProjectId(eMap); // projectId DB에서 가져오기
            long entityId = nlpWisenutApiMapper.selectEntityId(eMap);

            List<String> sList = new ArrayList<>();
            //sList에 synonyms들 추가

            nMap.put("projectId",projectId);
            nMap.put("id",entityId);
            nMap.put("titleWord",eMap.get("repWord"));
            nMap.put("tag","");
            nMap.put("synonyms",eMap.get("list"));

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

            // api request
            String responseBody = RestTemplateUtil.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();
        }
    }

    /**
     * @Name entityDeleteApi
     * @Description 엔티티 삭제 API
     * @Author dongheejang
     * @CreateDate 2022. 06. 20.
     *
     * @param eMap
     * @return NlpWisenutResult
     *
     * @ChangeDescription
     */
    @Override
    public NlpWisenutResult entityDeleteApi(HashMap eMap) {

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

        NlpWisenutResult nlpWisenutResult = new NlpWisenutResult();

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

        try {
            Map pMap = nlpWisenutApiMapper.selectEntityIdForDelete(eMap);

            nMap.put("projectId",pMap.get("projectId"));
            nMap.put("id",pMap.get("entityId"));

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

            // api request
            String responseBody = RestTemplateUtil.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();
        }
    }

}
