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) => {
                        console.log("compatibilityCheck:",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("nok");
                    return true;
                }
                if (!msg.connectionCode.includes('$')) {
                    sendResponse("nok");
                    return true;
                }
                const [sessionId, modelId] = msg.connectionCode.split("$");
                this._application
                    .connect(sessionId, modelId)
                    .then(() => sendResponse("ok"))
                    .catch((error) => {
                        sendResponse("nok")
                    });
                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(() => {
                        console.log('stopped');
                        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 { type, value } = msg;
                this._application.addCommentToExploration(type, value);
                sendResponse("ok");
                return true;
            }

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

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

            case "setRecordMediaStatus" : {
                //this._application.setRecordMediaStatus(msg.recordMediaStatus);
                return true;
            }

        }
    }
    


}