package com.example.mathseduc

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ArrayAdapter
import android.widget.Button
import android.widget.ListView
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import com.example.mathseduc.controllers.ControllerLobby
import com.example.mathseduc.controllers.ControllerUtiliser
import com.example.mathseduc.models.Lobby
import okhttp3.MultipartBody

class MultiActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_multi)

        var btnAddLobby = findViewById<Button>(R.id.btnAddLobby)

        val serverList = ControllerLobby.getLobbies()

        if (serverList != null) {

            val listView = findViewById<ListView>(R.id.listView)

            val adapter = LobbyAdapter(this, serverList)
            listView.adapter = adapter

            listView.setOnItemClickListener { _, _, position, _ ->
                val intent = Intent(this, ServerDetailsActivity::class.java)
                intent.putExtra("serverName", serverList[position].name)
                intent.putExtra("lobbyId", serverList[position].id)

                val formDataBuilder = MultipartBody.Builder().setType(MultipartBody.FORM)
                formDataBuilder.addFormDataPart("idplayer", MainActivity.idPlayerConnected.toString())
                formDataBuilder.addFormDataPart("idlobby", serverList[position].id.toString())
                formDataBuilder.addFormDataPart("playertime", "0")

                ControllerUtiliser.createUtiliserByIdLobby(formDataBuilder)
                startActivity(intent)
            }
        } else {
            Log.e("MultiActivity", "Error fetching server list")
        }

        btnAddLobby.setOnClickListener {
            val intent = Intent(this, CreateLobbyActivity::class.java)
            startActivity(intent)
        }
    }

    class LobbyAdapter(context: Context, lobbies: ArrayList<Lobby>): ArrayAdapter<Lobby>(context, 0, lobbies), View.OnClickListener {

        private val mContext: Context
        private val mLobbies: ArrayList<Lobby>

        private var selectedItem = -1;

        // Get Access to Context and Data Array
        init {
            mContext = context
            mLobbies = lobbies
        }

        override fun getCount(): Int {
            return mLobbies.size
        }

        override fun getItemId(position: Int): Long {
            return mLobbies[position].id.toLong()
        }

        override fun getItem(position: Int): Lobby {
            return mLobbies[position]
        }

        override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
            //return super.getView(position, convertView, parent)

            var currentLobbyView = convertView
            if (currentLobbyView == null) {
                currentLobbyView =
                    LayoutInflater.from(context).inflate(R.layout.list_view_lobby, parent, false)
            }

            val name = currentLobbyView!!.findViewById<TextView>(R.id.name)
            val nbplayers = currentLobbyView!!.findViewById<TextView>(R.id.nbplayers)
            val idchapter = currentLobbyView!!.findViewById<TextView>(R.id.idchapter)
            val difficulty = currentLobbyView!!.findViewById<TextView>(R.id.difficulty)

            name.text = mLobbies[position].name
            nbplayers.text = mLobbies[position].nbplayers.toString()
            idchapter.text = mLobbies[position].idchapter.toString()
            difficulty.text = mLobbies[position].difficulty.toString()

            // Visual Feedback for Selection
            /*
            if (selectedItem == position)
                currentLobbyView.setBackgroundColor(Color.rgb(255, 255, 128))
            else
                currentLobbyView.setBackgroundColor(Color.rgb(255, 255, 192))

            if (mProducts[position].prixht != null)
                pxht.text = "%.2f".format(mLobbies[position].prixht.toFloat()) + "€ HT"
            else
                pxht.text = "Gratuit !"
             */

            return currentLobbyView
        }

        public fun setSelectedItem(position: Int) {
            selectedItem = position
            notifyDataSetChanged();
        }

        public fun getSelectedItem(): Lobby? {

            if (selectedItem != -1)
                return mLobbies[selectedItem]
            else
                return null
        }

        override fun onClick(p0: View?) {
        }
    }
}
