from bottle import route, get, static_file, run
from bottle.ext.websocket import GeventWebSocketServer
from bottle.ext.websocket import websocket
from os.path import join as pathjoin
from statemachine import InitState, RegisteringState, IdleState, SchelltState, AcceptingState, CallTerminatingState, ForgottenState, BusyBeepingState, CallRunningState, WecktState, ConnectingState, DialingState
import threading

class WebInitState(InitState):
    def __init__(self, web):
        self.web = web
        super().__init__(web.controller)

class WebRegisteringState(RegisteringState):
    def __init__(self, web):
        self.web = web
        super().__init__(web.controller)

class WebIdleState(IdleState):
    def __init__(self, web):
        self.web = web
        super().__init__(web.controller)

class WebSchelltState(SchelltState):
    def __init__(self, web):
        self.web = web
        super().__init__(web.controller)

class WebAcceptingState(AcceptingState):
    def __init__(self, web):
        self.web = web
        super().__init__(web.controller)

class WebCallTerminatingState(CallTerminatingState):
    def __init__(self, web):
        self.web = web
        super().__init__(web.controller)

class WebForgottenState(ForgottenState):
    def __init__(self, web):
        self.web = web
        super().__init__(web.controller)

class WebBusyBeepingState(BusyBeepingState):
    def __init__(self, web):
        self.web = web
        super().__init__(web.controller)

class WebCallRunningState(CallRunningState):
    def __init__(self, web):
        self.web = web
        super().__init__(web.controller)

class WebWecktState(WecktState):
    def __init__(self, web):
        self.web = web
        super().__init__(web.controller)

class WebConnectingState(ConnectingState):
    def __init__(self, web):
        self.web = web
        super().__init__(web.controller)

class WebDialingState(DialingState):
    def __init__(self, web):
        self.web = web
        super().__init__(web.controller)

    def on_nummernschalter_input(self, num):
        print("calling super method")
        super().on_nummernschalter_input(num)
        print("local method got num: " + str(num))


class FeTapWeb():
    def __init__(self, config, statemachine_controller):
        self.config = config
        self.controller = statemachine_controller
        self.config = {"host": "0.0.0.0", "port": 8080}
        self.websockets = []
        route("/")(self.root)
        get("/ws", apply=[websocket])(self.ws_connect)
        route('/<path:path>')(self.statics)
        pass

    def root(self):
        return "<h1>Hello world!</h1>"

    def publish_status(self):
        print(self.controller.state.__class__.__name__)
        new_state = eval("Web" + self.controller.state.__class__.__name__)(self)
        self.controller.state = new_state
        for ws in self.websockets:
            ws.send("this is a message triggered from the server")
        return "ok"

    def ws_connect(self, ws):
        self.websockets.append(ws)
        while True:
            data = ws.receive()
            if data != None:
                print(data)
            else:
                break
        self.websockets.remove(ws)

    def statics(self, path):
        return static_file(path, root="./webinterface")

    def start(self):
        self.__running = True
        w = threading.Thread(target=self.__webthread)
        w.start()

    def __webthread(self):
        run(host=self.config["host"], port=self.config["port"], server=GeventWebSocketServer)