package com.example.tolucagraphics

import android.annotation.SuppressLint
import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import com.example.tolucagraphics.datas.GetRooms
import com.example.tolucagraphics.events.EventGetRoom
import com.google.gson.Gson
import kotlinx.android.synthetic.main.activity_lobby.*
import okhttp3.*
import java.util.concurrent.TimeUnit

class LobbyActivity : AppCompatActivity(){

    val conection = Socket
    private val SERVER_PATH = "ws://3.222.70.21:5000/ws?"
    var username : String? = null
    private val gson = Gson()
    lateinit var campoIDroom : TextView
    lateinit var campotestNameRoom : TextView

    @SuppressLint("SetTextI18n")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_lobby)
        username = intent.getStringExtra("USERNAME")

        initiateSocketConnection() // inicia conexion con websocket\

        /*********peticion de ROOMS**********/
        val getRooms = GetRooms("get_rooms")
        val getRoomsJSON = gson.toJson(getRooms)
        Log.d("request",getRoomsJSON)
        //webSocket.send(getRoomsJSON)
        //webSocket.close(1000, "finish")
        conection.webSocketSingle?.send(getRoomsJSON)


        btnSelect.setOnClickListener {
            actualizarView()
            val segundaPantalla = Intent(this, MesasActivity::class.java)
            startActivity(segundaPantalla)
        }
        //////////////////////webSocket.close(1000, "Finish")
    }

    private fun actualizarView() {
        lobbyRooms.visibility = View.INVISIBLE
    }

    private fun initiateSocketConnection() {
        //////////////////////////
        conection.SERVER_PATH = SERVER_PATH
        conection.username = username
        conection.realizarConex()
        /*val wsListener = WSListener ()
        webSocket = conection.client.newWebSocket(conection.request!!, wsListener) // realiza la conexion webSocket*/
        conection.listener = WSListener()
        conection.conectarconWS()
    }

    inner class WSListener : WebSocketListener() {
        override fun onOpen(webSocket: WebSocket, response: Response) {
            //Log.d("request", "connection successfully")
            Log.d("request","Conection Success desde onOpen")
            //webSocket.close(1000, "finish")
        }
        @SuppressLint("SetTextI18n")
        override fun onMessage(webSocket: WebSocket, text: String) {
            //usernameReceived.text = "cargando Rooms"
            Log.d("request", "Receiving : $text")
            val gsonreceived = Gson()
            runOnUiThread {
                val eventReceived = gsonreceived.fromJson(text, EventGetRoom::class.java)
                campoIDroom = findViewById(R.id.campoID)
                campotestNameRoom = findViewById(R.id.campoNAME)
                if (eventReceived != null) {
                    campoIDroom.text = eventReceived.data?.rooms?.get(0)?.id ?: "0"
                    campotestNameRoom.text = eventReceived.data?.rooms?.get(0)?.name ?: "-"
                }
            }
        }
        override fun onClosing(webSocket: WebSocket, code: Int, reason: String) {
            Log.d("request","Closing : $code / $reason")
        }
        override fun onFailure(webSocket: WebSocket, t: Throwable, response: Response?) {
            Log.d("request","Error : " + t.message)
        }
    }
}