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

export default class HandlerOfMessageSentByBackground  {

    private _tabScriptService : Interface4Background;
    
    constructor(tabScriptApplication: Interface4Background) {
        this._tabScriptService = tabScriptApplication;
        chrome.runtime.onMessage.addListener(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 "checkRunning":
                sendResponse("ok");
                return true;

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