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

export default class HandlerOfMessageSentByPopup {
    private _application : Interface4Popup;

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

    public attachCommunicationService(extensionCommunicationService : ExtensionCommunicationService): void {
        extensionCommunicationService.addOnMessageListener(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.error(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) => {
                        console.log("error", 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 "setCreateNewWindowOnConnect": {
                this._application.setShouldCreateNewWindowsOnConnect(msg.shouldCreateNewWindowOnConnect);
                sendResponse(this._application.getStateForPopup());
                return true;
            }

            case "setCloseWindowOnConnect": {
                this._application.setShouldCloseWindowOnDisconnect(msg.shouldCloseWindowOnDisconnect);
                sendResponse(this._application.getStateForPopup());
                return true;
            }

            case "setIsTabScriptDiplayingUserView": {
				this._application.setIsTabScriptDisplayingUserView(msg.isTabScriptDisplayingUserView);
                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();
                sendResponse("ok");
                return true;
            }

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

            case "setRecordMediaStatus" : {
                this._application.setRecordMedia(msg.recordMediaStatus)
                .then(() => {
                    sendResponse("ok");
                })
                .catch( (e) => {
                    console.log(e);
                    sendResponse("notok");
                })
                return true;
            }

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

        }
    }



}