package cn.com.poc.agent_application.convert;

import cn.com.poc.agent_application.domain.*;
import cn.com.poc.agent_application.dto.AgentApplicationInfoDto;
import cn.com.poc.agent_application.entity.BizAgentApplicationInfoEntity;
import cn.com.poc.agent_application.entity.DigitalhumanConfig;
import cn.com.poc.agent_application.entity.Variable;
import cn.com.poc.agent_application.entity.VoiceConfig;
import cn.com.poc.agent_application.model.BizAgentApplicationInfoModel;
import cn.com.poc.agent_application.query.AgentApplicationInfoQueryItem;
import cn.com.poc.agent_application.query.PublishAgentApplicationQueryItem;
import cn.com.poc.common.constant.CommonConstant;
import cn.com.poc.common.utils.JsonUtils;
import cn.com.poc.expose.dto.SearchAgentApplicationDto;
import cn.hutool.core.util.ObjectUtil;
import com.fasterxml.jackson.core.type.TypeReference;
import com.tencent.core.utils.JsonUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class AgentApplicationInfoConvert {

    public static SearchAgentApplicationDto itemToSearchDto(PublishAgentApplicationQueryItem item) {
        SearchAgentApplicationDto dto = new SearchAgentApplicationDto();
        dto.setAgentId(item.getAgentId());
        dto.setAgentTitle(item.getAgentTitle());
        dto.setAgentDesc(item.getAgentDesc());
        dto.setAgentAvatar(item.getAgentAvatar());
        dto.setPublishedTime(item.getModifiedTime());
        dto.setCreator(item.getNickName());
        return dto;
    }

    public static BizAgentApplicationInfoEntity modelToEntity(BizAgentApplicationInfoModel model) {
        BizAgentApplicationInfoEntity entity = new BizAgentApplicationInfoEntity();
        entity.setId(model.getId());
        entity.setMemberId(model.getMemberId());
        entity.setAgentId(model.getAgentId());
        entity.setAgentTitle(model.getAgentTitle());
        entity.setAgentDesc(model.getAgentDesc());
        entity.setAgentAvatar(model.getAgentAvatar());
        entity.setAgentSystem(model.getAgentSystem());
        entity.setAgentPublishStatus(model.getAgentPublishStatus());
        entity.setPreamble(model.getPreamble());
        entity.setPublishTime(model.getPublishTime());
        entity.setIsLongMemory(model.getIsLongMemory());
        if (StringUtils.isNotBlank(model.getVariableStructure())) {
            entity.setVariableStructure(JsonUtils.deSerialize(model.getVariableStructure(), new TypeReference<List<Variable>>() {
            }.getType()));
        }
        if (StringUtils.isNotBlank(model.getFeaturedQuestions())) {
            entity.setFeaturedQuestions(JsonUtils.deSerialize(model.getFeaturedQuestions(), String[].class));
        }
        entity.setCommunicationTurn(model.getCommunicationTurn());
        entity.setContinuousQuestionStatus(model.getContinuousQuestionStatus());
        entity.setContinuousQuestionSystem(model.getContinuousQuestionSystem());
        entity.setContinuousQuestionTurn(model.getContinuousQuestionTurn());
        if (StringUtils.isNotBlank(model.getKnowledgeIds())) {
            entity.setKnowledgeIds(JsonUtils.deSerialize(model.getKnowledgeIds(), Integer[].class));
        }
        if (StringUtils.isNotBlank(model.getKnowledgeDatabaseIds())) {
            entity.setKnowledgeDatabaseIds(JsonUtils.deSerialize(model.getKnowledgeDatabaseIds(), Integer[].class));
        }

        entity.setKnowledgeSearchType(model.getKnowledgeSearchType());
        entity.setKnowledgeSimilarity(model.getKnowledgeSimilarity());
        entity.setKnowledgeNResult(model.getKnowledgeNResult());
        entity.setKnowledgeResponseType(model.getKnowledgeResponseType());
        entity.setKnowledgeCustomResponse(StringUtils.isNotBlank(model.getKnowledgeCustomResponse()) ? JsonUtils.deSerialize(model.getKnowledgeCustomResponse(), String[].class) : null);

        entity.setIsDocumentParsing(model.getIsDocumentParsing());
        entity.setLargeModel(model.getLargeModel());
        entity.setTopP(model.getTopP());
        entity.setTemperature(model.getTemperature());
        if (StringUtils.isNotBlank(model.getUnitIds())) {
            entity.setUnitIds(JsonUtils.deSerialize(model.getUnitIds(), String[].class));
        }
        if (StringUtils.isNotBlank(model.getVoiceConfig())) {
            VoiceConfig voiceConfig = JsonUtils.deSerialize(model.getVoiceConfig(), VoiceConfig.class);
            entity.setVoiceConfig(voiceConfig);
        }
        if (StringUtils.isNotBlank(model.getDigitalhumanConfig())) {
            DigitalhumanConfig digitalhumanConfig = JsonUtils.deSerialize(model.getDigitalhumanConfig(), DigitalhumanConfig.class);
            entity.setDigitalhumanConfig(digitalhumanConfig);
        }
        entity.setIsDeleted(model.getIsDeleted());
        entity.setCreator(model.getCreator());
        entity.setCreatedTime(model.getCreatedTime());
        entity.setModifier(model.getModifier());
        entity.setModifiedTime(model.getModifiedTime());
        entity.setSysVersion(model.getSysVersion());
        return entity;
    }

    public static BizAgentApplicationInfoModel entityToModel(BizAgentApplicationInfoEntity entity) {
        BizAgentApplicationInfoModel model = new BizAgentApplicationInfoModel();
        model.setId(entity.getId());
        model.setMemberId(entity.getMemberId());
        model.setAgentId(entity.getAgentId());
        model.setAgentTitle(entity.getAgentTitle());
        model.setAgentAvatar(entity.getAgentAvatar());
        model.setAgentDesc(entity.getAgentDesc());
        model.setAgentSystem(entity.getAgentSystem());
        model.setAgentPublishStatus(entity.getAgentPublishStatus());
        model.setPreamble(entity.getPreamble());
        model.setPublishTime(entity.getPublishTime());
        model.setIsLongMemory(entity.getIsLongMemory());
        model.setIsDocumentParsing(entity.getIsDocumentParsing());
        if (CollectionUtils.isNotEmpty(entity.getVariableStructure())) {
            model.setVariableStructure(JsonUtils.serialize(entity.getVariableStructure()));
        }
        if (ArrayUtils.isNotEmpty(entity.getFeaturedQuestions())) {
            model.setFeaturedQuestions(JsonUtils.serialize(entity.getFeaturedQuestions()));
        }
        model.setCommunicationTurn(entity.getCommunicationTurn());
        model.setContinuousQuestionStatus(entity.getContinuousQuestionStatus());
        model.setContinuousQuestionSystem(entity.getContinuousQuestionSystem());
        model.setContinuousQuestionTurn(entity.getContinuousQuestionTurn());
        if (ArrayUtils.isNotEmpty(entity.getKnowledgeIds())) {
            model.setKnowledgeIds(JsonUtils.serialize(entity.getKnowledgeIds()));
        }

        if (ArrayUtils.isNotEmpty(entity.getKnowledgeDatabaseIds())) {
            model.setKnowledgeDatabaseIds(JsonUtils.serialize(entity.getKnowledgeDatabaseIds()));
        }

        model.setKnowledgeSearchType(entity.getKnowledgeSearchType());
        model.setKnowledgeSimilarity(entity.getKnowledgeSimilarity());
        model.setKnowledgeNResult(entity.getKnowledgeNResult());
        model.setKnowledgeResponseType(entity.getKnowledgeResponseType());
        model.setKnowledgeCustomResponse(ArrayUtils.isNotEmpty(entity.getKnowledgeCustomResponse()) ? JsonUtils.serialize(entity.getKnowledgeCustomResponse()) : null);

        model.setLargeModel(entity.getLargeModel());
        model.setTopP(entity.getTopP());
        model.setTemperature(entity.getTemperature());
        if (ArrayUtils.isNotEmpty(entity.getUnitIds())) {
            model.setUnitIds(JsonUtil.toJson(entity.getUnitIds()));
        }
        if (ObjectUtil.isNotEmpty(entity.getVoiceConfig())) {
            model.setVoiceConfig(JsonUtils.serialize(entity.getVoiceConfig()));
        }
        if (ObjectUtil.isNotEmpty(entity.getDigitalhumanConfig())) {
            model.setDigitalhumanConfig(JsonUtils.serialize(entity.getDigitalhumanConfig()));
        }
        model.setIsDeleted(entity.getIsDeleted());
        model.setCreator(entity.getCreator());
        model.setCreatedTime(entity.getCreatedTime());
        model.setModifier(entity.getModifier());
        model.setModifiedTime(entity.getModifiedTime());
        model.setSysVersion(entity.getSysVersion());
        return model;
    }

    public static AgentApplicationInfoDto entityToDto(BizAgentApplicationInfoEntity entity) {
        AgentApplicationBaseInfo baseInfo = new AgentApplicationBaseInfo();
        baseInfo.setMemberId(entity.getMemberId());
        baseInfo.setAgentId(entity.getAgentId());
        baseInfo.setAgentTitle(entity.getAgentTitle());
        baseInfo.setAgentAvatar(entity.getAgentAvatar());
        baseInfo.setAgentDesc(entity.getAgentDesc());
        baseInfo.setAgentSystem(entity.getAgentSystem());
        baseInfo.setAgentPublishStatus(entity.getAgentPublishStatus());
        baseInfo.setPublishTime(entity.getPublishTime());

        AgentApplicationCommConfig commConfig = new AgentApplicationCommConfig();
        commConfig.setPreamble(entity.getPreamble());
        commConfig.setFeaturedQuestions(entity.getFeaturedQuestions());
        commConfig.setContinuousQuestionStatus(entity.getContinuousQuestionStatus());
        commConfig.setContinuousQuestionSystem(entity.getContinuousQuestionSystem());
        commConfig.setContinuousQuestionTurn(entity.getContinuousQuestionTurn());
        commConfig.setVariableStructure(entity.getVariableStructure());
        commConfig.setIsLongMemory(entity.getIsLongMemory());


        AgentApplicationKnowledgeConfig knowledgeConfig = new AgentApplicationKnowledgeConfig();
        knowledgeConfig.setKnowledgeIds(entity.getKnowledgeIds());
        knowledgeConfig.setIsDocumentParsing(entity.getIsDocumentParsing());
        knowledgeConfig.setKnowledgeSearchType(entity.getKnowledgeSearchType());
        knowledgeConfig.setKnowledgeSimilarity(entity.getKnowledgeSimilarity());
        knowledgeConfig.setKnowledgeNResult(entity.getKnowledgeNResult());
        knowledgeConfig.setKnowledgeResponseType(entity.getKnowledgeResponseType());
        knowledgeConfig.setKnowledgeCustomResponse(entity.getKnowledgeCustomResponse());

        AgentApplicationDatabaseConfig databaseConfig = new AgentApplicationDatabaseConfig();
        databaseConfig.setIds(entity.getKnowledgeDatabaseIds());

        AgentApplicationCommModelConfig commModelConfig = new AgentApplicationCommModelConfig();
        commModelConfig.setLargeModel(entity.getLargeModel());
        commModelConfig.setTopP(entity.getTopP());
        commModelConfig.setTemperature(entity.getTemperature());
        commModelConfig.setCommunicationTurn(entity.getCommunicationTurn());

        AgentApplicationVoiceConfig voiceConfig = new AgentApplicationVoiceConfig();
        if (ObjectUtil.isNotEmpty(entity.getVoiceConfig())) {
            voiceConfig.setDefaultOpen(entity.getVoiceConfig().getDefaultOpen());
            voiceConfig.setTimbreId(entity.getVoiceConfig().getTimbreId());
        } else {
            voiceConfig.setDefaultOpen(CommonConstant.YOrN.N);
            voiceConfig.setTimbreId(StringUtils.EMPTY);
        }

        DigitalhumanConfig digitalhumanConfig = new DigitalhumanConfig();
        if (ObjectUtil.isNotEmpty(entity.getDigitalhumanConfig())) {
            digitalhumanConfig.setEnable(entity.getDigitalhumanConfig().getEnable());
            digitalhumanConfig.setTimbreId(entity.getDigitalhumanConfig().getTimbreId());
            digitalhumanConfig.setAppId(entity.getDigitalhumanConfig().getAppId());
            digitalhumanConfig.setAppKey(entity.getDigitalhumanConfig().getAppKey());
            digitalhumanConfig.setFigureId(entity.getDigitalhumanConfig().getFigureId());
        } else {
            digitalhumanConfig.setEnable(CommonConstant.YOrN.N);
            digitalhumanConfig.setTimbreId(StringUtils.EMPTY);
            digitalhumanConfig.setFigureId(StringUtils.EMPTY);
            digitalhumanConfig.setAppKey(StringUtils.EMPTY);
            digitalhumanConfig.setAppId(StringUtils.EMPTY);
        }

        AgentApplicationInfoDto dto = new AgentApplicationInfoDto();
        dto.setBaseInfo(baseInfo);
        dto.setCommConfig(commConfig);
        dto.setKnowledgeConfig(knowledgeConfig);
        dto.setCommModelConfig(commModelConfig);
        dto.setVoiceConfig(voiceConfig);
        dto.setDigitalhumanConfig(digitalhumanConfig);
        dto.setUnitIds(entity.getUnitIds());
        dto.setDatabaseConfig(databaseConfig);
        dto.setCreator(entity.getCreator());
        dto.setCreatedTime(entity.getCreatedTime());
        dto.setModifiedTime(entity.getModifiedTime());
        return dto;
    }

    public static BizAgentApplicationInfoEntity dtoToEntity(AgentApplicationInfoDto dto) {
        BizAgentApplicationInfoEntity entity = new BizAgentApplicationInfoEntity();
        if (ObjectUtil.isNotEmpty(dto.getBaseInfo())) {
            entity.setAgentId(dto.getBaseInfo().getAgentId());
            entity.setMemberId(dto.getBaseInfo().getMemberId());
            entity.setAgentAvatar(dto.getBaseInfo().getAgentAvatar());
            entity.setMemberId(dto.getBaseInfo().getMemberId());
            entity.setAgentTitle(dto.getBaseInfo().getAgentTitle());
            entity.setAgentDesc(dto.getBaseInfo().getAgentDesc());
            entity.setAgentSystem(dto.getBaseInfo().getAgentSystem());
            entity.setAgentPublishStatus(dto.getBaseInfo().getAgentPublishStatus());
            entity.setPublishTime(dto.getBaseInfo().getPublishTime());
        }

        if (ObjectUtil.isNotEmpty(dto.getCommConfig())) {
            entity.setPreamble(dto.getCommConfig().getPreamble());
            entity.setFeaturedQuestions(dto.getCommConfig().getFeaturedQuestions());
            entity.setContinuousQuestionStatus(dto.getCommConfig().getContinuousQuestionStatus());
            entity.setContinuousQuestionSystem(dto.getCommConfig().getContinuousQuestionSystem());
            entity.setContinuousQuestionTurn(dto.getCommConfig().getContinuousQuestionTurn());
            if (CollectionUtils.isNotEmpty(dto.getCommConfig().getVariableStructure())) {
                entity.setVariableStructure(dto.getCommConfig().getVariableStructure());
            }
            entity.setIsLongMemory(dto.getCommConfig().getIsLongMemory());
        }

        if (ObjectUtil.isNotEmpty(dto.getKnowledgeConfig())) {
            entity.setKnowledgeIds(dto.getKnowledgeConfig().getKnowledgeIds());
            entity.setIsDocumentParsing(dto.getKnowledgeConfig().getIsDocumentParsing());
            entity.setKnowledgeSearchType(dto.getKnowledgeConfig().getKnowledgeSearchType());
            entity.setKnowledgeSimilarity(dto.getKnowledgeConfig().getKnowledgeSimilarity());
            entity.setKnowledgeNResult(dto.getKnowledgeConfig().getKnowledgeNResult());
            entity.setKnowledgeResponseType(dto.getKnowledgeConfig().getKnowledgeResponseType());
            entity.setKnowledgeCustomResponse(dto.getKnowledgeConfig().getKnowledgeCustomResponse());
        }

        if (ObjectUtil.isNotEmpty(dto.getDatabaseConfig())) {
            entity.setKnowledgeDatabaseIds(dto.getDatabaseConfig().getIds());
        }

        if (ObjectUtil.isNotEmpty(dto.getCommModelConfig())) {
            entity.setLargeModel(dto.getCommModelConfig().getLargeModel());
            entity.setTopP(dto.getCommModelConfig().getTopP());
            entity.setTemperature(dto.getCommModelConfig().getTemperature());
            entity.setCommunicationTurn(dto.getCommModelConfig().getCommunicationTurn());
        }

        VoiceConfig voiceConfig = new VoiceConfig();
        if (ObjectUtil.isNotEmpty(dto.getVoiceConfig())) {
            voiceConfig.setDefaultOpen(dto.getVoiceConfig().getDefaultOpen());
            voiceConfig.setTimbreId(dto.getVoiceConfig().getTimbreId());
        } else {
            voiceConfig.setDefaultOpen(CommonConstant.YOrN.N);
            voiceConfig.setTimbreId(StringUtils.EMPTY);
        }
        entity.setVoiceConfig(voiceConfig);

        DigitalhumanConfig digitalhumanConfig = new DigitalhumanConfig();
        if (ObjectUtil.isNotEmpty(dto.getDigitalhumanConfig())) {
            digitalhumanConfig.setEnable(dto.getDigitalhumanConfig().getEnable());
            digitalhumanConfig.setTimbreId(dto.getDigitalhumanConfig().getTimbreId());
            digitalhumanConfig.setAppId(dto.getDigitalhumanConfig().getAppId());
            digitalhumanConfig.setAppKey(dto.getDigitalhumanConfig().getAppKey());
            digitalhumanConfig.setFigureId(dto.getDigitalhumanConfig().getFigureId());
        } else {
            digitalhumanConfig.setEnable(CommonConstant.YOrN.N);
            digitalhumanConfig.setTimbreId(StringUtils.EMPTY);
            digitalhumanConfig.setFigureId(StringUtils.EMPTY);
            digitalhumanConfig.setAppId(StringUtils.EMPTY);
            digitalhumanConfig.setAppKey(StringUtils.EMPTY);
        }
        entity.setDigitalhumanConfig(digitalhumanConfig);

        entity.setUnitIds(dto.getUnitIds());
        entity.setCreator(dto.getCreator());
        entity.setCreatedTime(dto.getCreatedTime());
        entity.setModifiedTime(dto.getModifiedTime());
        return entity;
    }


    public static BizAgentApplicationInfoEntity queryItemToEntity(AgentApplicationInfoQueryItem infoQueryItem) {
        BizAgentApplicationInfoEntity entity = new BizAgentApplicationInfoEntity();
        entity.setId(infoQueryItem.getId());
        entity.setMemberId(infoQueryItem.getMemberId());
        entity.setAgentId(infoQueryItem.getAgentId());
        entity.setAgentTitle(infoQueryItem.getAgentTitle());
        entity.setAgentDesc(infoQueryItem.getAgentDesc());
        entity.setAgentAvatar(infoQueryItem.getAgentAvatar());
        entity.setAgentSystem(infoQueryItem.getAgentSystem());
        entity.setAgentPublishStatus(infoQueryItem.getAgentPublishStatus());
        entity.setPreamble(infoQueryItem.getPreamble());
        entity.setPublishTime(infoQueryItem.getPublishTime());
        entity.setIsLongMemory(infoQueryItem.getIsLongMemory());
        entity.setIsDocumentParsing(infoQueryItem.getIsDocumentParsing());

        if (StringUtils.isNotBlank(infoQueryItem.getVariableStructure())) {
            entity.setVariableStructure(JsonUtils.deSerialize(infoQueryItem.getVariableStructure(), new TypeReference<List<Variable>>() {
            }.getType()));
        }

        if (StringUtils.isNotBlank(infoQueryItem.getFeaturedQuestions())) {
            entity.setFeaturedQuestions(JsonUtils.deSerialize(infoQueryItem.getFeaturedQuestions(), String[].class));
        }
        entity.setCommunicationTurn(infoQueryItem.getCommunicationTurn());
        entity.setContinuousQuestionStatus(infoQueryItem.getContinuousQuestionStatus());
        entity.setContinuousQuestionSystem(infoQueryItem.getContinuousQuestionSystem());
        entity.setContinuousQuestionTurn(infoQueryItem.getContinuousQuestionTurn());
        if (StringUtils.isNotBlank(infoQueryItem.getKnowledgeIds())) {
            entity.setKnowledgeIds(JsonUtils.deSerialize(infoQueryItem.getKnowledgeIds(), Integer[].class));
        }
        if (StringUtils.isNotBlank(infoQueryItem.getKnowledgeDatabaseIds())) {
            entity.setKnowledgeDatabaseIds(JsonUtils.deSerialize(infoQueryItem.getKnowledgeDatabaseIds(), Integer[].class));
        }

        entity.setKnowledgeSearchType(infoQueryItem.getKnowledgeSearchType());
        entity.setKnowledgeSimilarity(infoQueryItem.getKnowledgeSimilarity());
        entity.setKnowledgeNResult(infoQueryItem.getKnowledgeNResult());
        entity.setKnowledgeResponseType(infoQueryItem.getKnowledgeResponseType());
        if (StringUtils.isNotBlank(infoQueryItem.getKnowledgeCustomResponse())) {
            entity.setKnowledgeCustomResponse(JsonUtils.deSerialize(infoQueryItem.getKnowledgeCustomResponse(), String[].class));
        }
        entity.setLargeModel(infoQueryItem.getLargeModel());
        entity.setTopP(infoQueryItem.getTopP());
        entity.setTemperature(infoQueryItem.getTemperature());
        if (StringUtils.isNotBlank(infoQueryItem.getUnitIds())) {
            entity.setUnitIds(JsonUtils.deSerialize(infoQueryItem.getUnitIds(), String[].class));
        }
        if (StringUtils.isNotBlank(infoQueryItem.getVoiceConfig())) {
            entity.setVoiceConfig(JsonUtils.deSerialize(infoQueryItem.getVoiceConfig(), VoiceConfig.class));
        }
        if (StringUtils.isNotBlank(infoQueryItem.getVoiceConfig())) {
            entity.setDigitalhumanConfig(JsonUtils.deSerialize(infoQueryItem.getDigitalhumanConfig(), DigitalhumanConfig.class));
        }
//        if (StringUtils.isNotBlank(infoQueryItem())) {
//            entity.setVoiceConfig(JsonUtils.deSerialize(infoQueryItem.getVoiceConfig(), VoiceConfig.class));
//        }
        entity.setIsDeleted(infoQueryItem.getIsDeleted());
        entity.setCreator(infoQueryItem.getCreator());
        entity.setCreatedTime(infoQueryItem.getCreatedTime());
        entity.setModifier(infoQueryItem.getModifier());
        entity.setModifiedTime(infoQueryItem.getModifiedTime());
        entity.setSysVersion(infoQueryItem.getSysVersion());
        return entity;

    }
}