import AnswerLogic from "./answerLogic";
import { initContentsManager } from "./contentsManager";


export default class ControLogic {
    constructor(param = {}) {
        //互动逻辑池 answerLogicMap<sessionId,AnswerLogic>
        this.config = param.config;
        this.answerLogicMap = new Map();
        this.contentsManager = initContentsManager(this.config);

    }

    createrLogic(sessionId, events) {
        var answerLogic = this.answerLogicMap.get(sessionId);
        if (!answerLogic) {
            answerLogic = new AnswerLogic(sessionId, { config: this.config?.queue || { start: "auto" } });
            //初始化 传入开场白
            answerLogic.init(events);
            answerLogic.onOutput = (event) => {
                this.onOutput(event);
            }
            this.answerLogicMap.set(sessionId, answerLogic);
        }
    }
    async setTask(sessionId, events, config) {
        var answerLogic = this.answerLogicMap.get(sessionId);
        if (answerLogic) {
            const data = await this.contentsManager.setData(events, config);
            return answerLogic.addData(data);
        }
    }

    async getAnswer(sessionId, answer, config) {
        var answerLogic = this.answerLogicMap.get(sessionId);
        if (answerLogic) {
            var events = this.analysisEvent(answer.data);
            events = await this.contentsManager.setData(events, config);
            return answerLogic.addData2head(events, answer.type === "interrupt");
        }
    }

    clear(sessionId) {
        var answerLogic = this.answerLogicMap.get(sessionId);
        if (answerLogic) {
            answerLogic.clear();
        }
    }

    start(sessionId) {
        var answerLogic = this.answerLogicMap.get(sessionId);
        if (answerLogic) {
            answerLogic.start();
        }
    }

    delay(sessionId, timeStamp) {
        var answerLogic = this.answerLogicMap.get(sessionId);
        if (answerLogic) {
            answerLogic.delay(timeStamp);
        }
    }

    addData(sessionId, events) {
        var answerLogic = this.answerLogicMap.get(sessionId);
        if (answerLogic) {
            answerLogic.addData(events);
        }
    }


    //会话结束后销毁
    destroy() {

    };
    //恢复会话
    recovery() {

    };
    //输出事件给表达控制器
    onOutput() { };

    //TODO分析响应事件 表达逻辑
    analysisEvent(data) {
        var events = [...data];
        //根据 botKey nluId 确定 表达逻辑 

        for (var i = 0; i < events.length; i++) {
            events[i].id = new Date().getTime();


        }
        return events;
    };
}
