import ExtensionCommunicationService from "./ExtensionCommunicationService";
import Interface4Background from "../application/Interface4Background";

export default class HandlerOfMessageSentByBackground  {

    private _tabScriptService : Interface4Background;
    
    constructor(tabScriptApplication: Interface4Background, extensionCommunicationService : ExtensionCommunicationService) {
        this._tabScriptService = tabScriptApplication;
        extensionCommunicationService.addOnMessageListener(this.handleMessage.bind(this));
    }

    handleMessage(msg, sender, sendResponse) : boolean {
        switch (msg.kind) {
            case "explorationStarted": 
                this._tabScriptService.explorationStarted();
                sendResponse("ok");
                return true;

            case "explorationStopped":
                this._tabScriptService.explorationStopped();
                sendResponse("ok");
                return true;

            case "reload":
                this._tabScriptService.reload();
                sendResponse("ok");
                return true;

            case "toggleUserView":
                this._tabScriptService.toggleUserView(msg.visible)
                sendResponse("ok");
                return true;

            case "checkRunning":
                sendResponse("ok");
                return true;

            default:
                console.log("unhandled message ", msg);
                sendResponse("ok");
                return true;
            }
    }
}