package com.ch.jedge.jbot.knbase;

import com.alibaba.fastjson.JSONArray;
import com.ch.jedge.jbot.dict.JedgeWordDict;
import com.ch.jedge.jbot.dict.JedgeWordItem;
import com.ch.jedge.jbot.knbase.domain.JedgeGdeDomainBlock;
import com.changhong.qlib.QData;
import com.changhong.qlib.util.StringUtils;

import java.util.HashMap;
import java.util.Map;

import static com.ch.jedge.jbot.agent.JedgeGdeConsts.sval_default_sys_role;
import static com.ch.jedge.jbot.JEJBotLLMConst.sval_modual_word_cut;

/**
 *  动态领域，
 *      · 在对话中，通过动态对话后对意图和词汇的识别，命中动态领域。
 *      · 领域中维护默认的知识、词库、意图等
 *      · 并且可动态向向量知识库中注册这些动态词库的名称
 *      ` 入口关键字等信息
 */
public class JedgeGptDomain {
    private final JedgeGptApp mGdeApp;
    protected final String domainName;
    private final String doMainFilePath;

    protected final JedgeWordDict mGlobalWordDict;

    protected final Map<String, String> mBlockIndex = new HashMap<>();
    //关键字词
    protected final Map<String, JedgeGdeDomainBlock> mContentBlocks = new HashMap<>();
    private String mDomainTitle = "";
    public String domainRole = sval_default_sys_role;

    public JedgeGptDomain(JedgeGptApp app, String name, String gdeFileName) {
        this.mGdeApp = app;
        this.domainName = name;
        this.mGlobalWordDict = new JedgeWordDict(this);
        this.doMainFilePath = gdeFileName;
    }

    public boolean isValid() {
        return true;
    }

    public String getDomainName() {
        return domainName;
    }

    public void addBlockContentOk(String currentBlockHash, int blockIndex, String currentBlockTitle, String blockContent) {
        JedgeGdeDomainBlock block = new JedgeGdeDomainBlock(doMainFilePath, blockIndex, currentBlockTitle, blockContent);
        synchronized (mContentBlocks) {
            mContentBlocks.put(currentBlockHash, block);
        }
    }

    public String getFilePath() {
        return doMainFilePath;
    }

    public void pushGlobalWord(String word, String blockVid, String blockTitle, boolean isGateItem) {
        String[] kl = word.split(":");
        JedgeWordItem wordItem;
        String freq = kl.length>1?kl[1]:"100";
        String type = kl.length > 2 ? kl[2] : "nr";
        wordItem = new JedgeWordItem(kl[0], type, freq, isGateItem);     //对话时优先适配专有名词
        if(JedgeGptApp.isDebugMode())
            mGdeApp.getMgModule().infoLog("[Keyword Def on %s]>>%s", blockTitle, word);
        mGlobalWordDict.appendWordItem(wordItem);
        //关键字到BlockTitle的映射
        synchronized (mContentBlocks) {
            String old = mBlockIndex.get(word);
            mBlockIndex.put(word, blockVid);
        }
        //建立word和Block之间的映射关系，加载时，导入到智能数据库中。
    }

    public void pushGateIntent(String def, String blockVid, String bIdx) {

    }

    public void clearWordDict(boolean gated) {
        JSONArray items = mGlobalWordDict.getWordItemsInJson(gated);
        if(!items.isEmpty()) {
            mGdeApp.getBotService().postServiceMessage(sval_modual_word_cut, "/cw/word", new QData().put("w", items).put("d", true));
        }
    }

    public void activeDomainWordDict(boolean gated) {
        JSONArray items = mGlobalWordDict.getWordItemsInJson(gated);
        if(!items.isEmpty()) {
            mGdeApp.getBotService().postServiceMessage(sval_modual_word_cut, "/cw/word", new QData().put("w", items));
        }
    }

    public String getKnItemContent(String word) {
        try {
            synchronized (mContentBlocks) {
                String blockVi = mBlockIndex.get(word);
                if(blockVi==null) return null;
                JedgeGdeDomainBlock b = mContentBlocks.get(blockVi);
                if(b!=null)
                    return b.blockContent;
            }
        }catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    //TODO::处理一个关键字映射到多个Block的情况
    public JedgeGdeDomainBlock getKnItemId(String word) {
        try {
            synchronized (mContentBlocks) {
                String idx = mBlockIndex.get(word);
                if(StringUtils.isValidStr(idx))
                    return mContentBlocks.get(idx);
            }
        }catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public String getSubjectTitle() {
        return mDomainTitle;
    }

    public void setSubjectTitle(String def) {
        mDomainTitle = def;
    }

    public JedgeWordDict getGlobalWordDict() {
        return mGlobalWordDict;
    }
}
