import Interface4TabScript from "../application/Interface4TabScript";
import Question from "../domain/Question";
import ExtensionCommunicationService from "./ExtensionCommunicationService";

export default class HandlerOfMessageSentByTabScript {
    private _application : Interface4TabScript;

    constructor(application: Interface4TabScript) {
        this._application = application;
    }

    public attachCommunicationService(extensionCommunicationService : ExtensionCommunicationService): void {
        extensionCommunicationService.addOnMessageListener(this.handleMessage.bind(this));
    }


    private handleMessage(msg, sender, sendResponse): boolean {
        switch (msg.kind) {

			case "getStateForTabScript": {
				sendResponse(this._application.getStateForTabScript());
				return true;
            }

            case "getProbabilities": {
                this._application.getProbabilities()
                    .then((response) => {
                        if (!response) {
                            sendResponse();
                            return true;
                        }
                        sendResponse({
                            state: response.state,
                            probabilityMap: JSON.parse(response.probabilityMap)
                        });
                    })
                    .catch((error) => {
                        console.log('error:',error);
                        sendResponse();
                    });
                return true;
            }

            case "getComments": {
                this._application.getComments()
                    .then((response) => {
                        if (!response) {
                            sendResponse();
                            return true;
                        }
                        sendResponse({
                            state: response.state,
                            commentDistributionList: JSON.parse(response.commentDistributionList)
                        });
                    })
                    .catch((error) => {
                        console.log(error);
                        sendResponse();
                    });
                    return true;
            }

            case "getEvaluation":
                this._application.getExplorationEvaluation().then(evaluation => {
                    if (!evaluation) {
                        sendResponse();
                        return true;
                    }
                    sendResponse({
                        validatedStepList: evaluation.validatedStepList,
                        notValidatedStepList: evaluation.notValidatedStepList,
                        enteringInteractionList: evaluation.enteringInteractionList.map(action => action.toString()),
                        continuingActionList: evaluation.continuingActionList.map(action => action.toString()),
                        finishingInteractionList: evaluation.finishingInteractionList.map(action => action.toString()),
                        questionList: evaluation.questionList.map(question => question.toString()),
                    });

                })
                return true;

            case "pushAction": {
                const action = msg.action;
                this._application.processNewAction(action.prefix, action.suffix)
                .then(() => {
                    sendResponse("ok");
                })
                return true;
            }

            case "upComment": {
                this._application.upComment(msg.type, msg.value);
                sendResponse("ok");
                return true;
            }

            case "setPopupCommentPosition": {
                this._application.setPopupCommentPosition(msg.popupCommentPosition);
                sendResponse("ok");
                return true;
            }

            case "pushAnswer": {
                const question = new Question(msg.question.text);
                // console.log(msg.value, msg.value === true)
                this._application.addAnswerToExploration(question, msg.value);
                sendResponse("ok");
                return true;
            }

        }
    }
}