import Interface4Popup from "../application/Interface4Popup";

export default class HandlerOfMessageSentByPopup {
    private _application : Interface4Popup;

    constructor(application: Interface4Popup) {
        this._application = application;
        chrome.runtime.onMessage.addListener(this.handleMessage.bind(this));
    }

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

            case "checkDeprecated": {
				this._application.makeCompatibilityCheck()
					.then((compatibilityCheck) => {
						sendResponse(compatibilityCheck);
					})
					.catch((error) => {
						console.log(error);
						sendResponse(error);
					});
				return true;
			}

			case "getStateForPopup": {
				sendResponse(this._application.getStateForPopup());
				return true;
			}
    
            case "connect": {
                if (!msg.connectionCode) {
                    sendResponse({ error: "connectionCode required" });
                    return;
                }
                const [sessionId, modelId] = msg.connectionCode.split("$");
                this._application
                    .connect(sessionId, modelId)
                    .then(() => sendResponse("ok"))
                    .catch((error) => console.error(error));
                return true;
            }

            case "disconnect": {
                this._application
                    .disconnect()
                    .then(() => {
                        sendResponse("ok");
                    })
                    .catch((error) => {
                        sendResponse({ error });
                        console.error(error);
                    });
                return true;
            }

            case "reloadWebSite": {
                this._application
                    .reloadWebsite()
                    .then(() => {
                        sendResponse("ok");
                    })
                    .catch((error) => {
                        console.log(error);
                        sendResponse(error);
                    });
                return true;
            }

            case "startExploration": {
                this._application
                    .startExploration()
                    .then(() => {
                        sendResponse(this._application.getStateForPopup());
                    })
                    .catch((error) => {
                        console.log(error);
                        sendResponse(error);
                    });
                return true;
            }

            case "stopExploration": {
                this._application
                    .stopExploration()
                    .then(() => {
                        sendResponse(this._application.getStateForPopup());
                    })
                    .catch((error) => {
                        console.log(error);
                        sendResponse(error);
                    });
                return true;
            }

            case "removeExploration": {
                this._application
                    .removeExploration()
                    .then(() => {
                        sendResponse(this._application.getStateForPopup());
                    }).catch((error) => {
                        console.log(error);
                        sendResponse(error);
                    });
                return true;
            }

            case "changeTesterName": {
				this._application.changeTesterName(msg.testerName);
                sendResponse(this._application.getStateForPopup());
				return true;
            }
            
            case "setIsTabScriptDiplayingComments": {
				this._application.setIsTabScriptDisplayingComments(msg.isTabScriptDisplayingComments);
                sendResponse(this._application.getStateForPopup());
				return true;
            }
            
            case "restartExploration": {
                this._application
                .restartExploration()
                .then(() => {
                    sendResponse(this._application.getStateForPopup());
                })
                .catch((error) => {
                    console.log(error);
                    sendResponse(error);
                });
                return true;
            }
            
            case "pushComment": {
                const { kind, value } = msg;
                this._application.addCommentToExploration(kind, value);
                sendResponse("ok");
                return true;
            }

            case "takeScreenshot": {
                this._application.takeScreenShot();
                return true;
            }

            case "openWindow": {
                this._application.drawAttention();
                return true;
            }

        }
    }
    


}