package uk.co.rossbinden.transferencevoid.controller

import com.badlogic.ashley.core.Entity
import com.badlogic.gdx.math.Vector2
import org.eclipse.jetty.websocket.api.Session
import org.eclipse.jetty.websocket.api.WebSocketListener
import uk.co.rossbinden.transferencevoid.component.BODY
import uk.co.rossbinden.transferencevoid.component.DATA
import uk.co.rossbinden.transferencevoid.component.SPEED
import uk.co.rossbinden.transferencevoid.screen.MainScreen

class ControllerWebSocket: WebSocketListener {

    lateinit var player: Entity
    var session: Session? = null

    override fun onWebSocketError(cause: Throwable) {

    }

    override fun onWebSocketClose(statusCode: Int, reason: String?) {
        this.session = null
    }

    override fun onWebSocketConnect(session: Session) {
        this.session = session
    }

    override fun onWebSocketText(message: String) {

    }

    override fun onWebSocketBinary(payload: ByteArray, offset: Int, len: Int) {
        val body = BODY.get(player).body
        val speedComponent = SPEED.get(player)
        when (payload[1].toInt()) {
            1 -> {
                when (payload[0].toInt()) {
                    1 -> {
                        body.angularVelocity = 2f
                        body.linearVelocity = Vector2(speedComponent.speed * Math.cos(body.angle.toDouble()).toFloat(), speedComponent.speed * Math.sin(body.angle.toDouble()).toFloat())
                    }
                    2 -> {
                        body.angularVelocity = -2f
                        body.linearVelocity = Vector2(speedComponent.speed * Math.cos(body.angle.toDouble()).toFloat(), speedComponent.speed * Math.sin(body.angle.toDouble()).toFloat())
                    }
                    3 -> {
                        speedComponent.speed = 3f
                        body.linearVelocity = Vector2(speedComponent.speed * Math.cos(body.angle.toDouble()).toFloat(), speedComponent.speed * Math.sin(body.angle.toDouble()).toFloat())
                    }
                    4 -> {
                        if (DATA.get(player).data > 0) {
                            MainScreen.createRay(player)
                        }
                    }
                }
            }
            2 -> {
                when (payload[0].toInt()) {
                    1 -> {
                        body.angularVelocity = 0f
                        body.linearVelocity = Vector2(speedComponent.speed * Math.cos(body.angle.toDouble()).toFloat(), speedComponent.speed * Math.sin(body.angle.toDouble()).toFloat())
                    }
                    2 -> {
                        body.angularVelocity = 0f
                        body.linearVelocity = Vector2(speedComponent.speed * Math.cos(body.angle.toDouble()).toFloat(), speedComponent.speed * Math.sin(body.angle.toDouble()).toFloat())
                    }
                    3 -> {
                        speedComponent.speed = 0f
                        body.linearVelocity = Vector2(speedComponent.speed * Math.cos(body.angle.toDouble()).toFloat(), speedComponent.speed * Math.sin(body.angle.toDouble()).toFloat())
                    }
                }
            }
        }
    }

}