package uk.co.rossbinden.transferencevoid

import com.badlogic.gdx.Game
import com.badlogic.gdx.assets.AssetManager
import com.badlogic.gdx.maps.tiled.TiledMap
import com.badlogic.gdx.maps.tiled.TmxMapLoader
import org.eclipse.jetty.server.Server
import org.eclipse.jetty.server.session.SessionHandler
import org.eclipse.jetty.servlet.ServletContextHandler
import org.eclipse.jetty.servlet.ServletHolder
import uk.co.rossbinden.transferencevoid.controller.ControllerHtmlServlet
import uk.co.rossbinden.transferencevoid.controller.ControllerJsServlet
import uk.co.rossbinden.transferencevoid.controller.ControllerWebSocketServlet
import uk.co.rossbinden.transferencevoid.screen.MainScreen
import uk.co.rossbinden.transferencevoid.screen.StartScreen

object TransferenceVoid : Game() {

    lateinit var assetManager: AssetManager
    lateinit var webServer: Server

    override fun create() {
        webServer = Server(30507)
        val servletContextHandler = ServletContextHandler()
        servletContextHandler.addServlet(ServletHolder(ControllerHtmlServlet()), "/controller")
        servletContextHandler.addServlet(ServletHolder(ControllerJsServlet()), "/js/*")
        servletContextHandler.addServlet(ServletHolder(ControllerWebSocketServlet()), "/ws")
        servletContextHandler.sessionHandler = SessionHandler()
        webServer.handler = servletContextHandler
        webServer.start()

        assetManager = AssetManager()
        assetManager.setLoader(TiledMap::class.java, TmxMapLoader())
        setScreen(StartScreen)
    }

    override fun dispose() {
        webServer.stop()
        StartScreen.dispose()
        MainScreen.dispose()
    }

}
