package com.example.mathseduc

import android.content.Intent
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.example.mathseduc.controllers.ControllerPlayer
import org.mindrot.jbcrypt.BCrypt

class ConnexionPlayerActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_connexion_player)

        val buttonLogin = findViewById<Button>(R.id.buttonLogin)
        val editTextNickname = findViewById<EditText>(R.id.editTextNickname)
        val editTextPassword = findViewById<EditText>(R.id.editTextPassword)

        buttonLogin.setOnClickListener {
            val nickname = editTextNickname.text.toString()
            val password = editTextPassword.text.toString()

            val isAuthenticated = ControllerPlayer.authenticateUser(nickname,password)
            if (isAuthenticated != -1) {
                // Save authentication status for global accessibility
                val intent = Intent(this, MainActivity::class.java)
                startActivity(intent)

                MainActivity.idPlayerConnected = isAuthenticated

                Toast.makeText(this, "Connexion réussi, bienvenue $nickname !!", Toast.LENGTH_SHORT).show()
            } else {
                // Show an error toast
                Toast.makeText(this, "Connexion échoué. Veuillez réessayer.", Toast.LENGTH_SHORT).show()
            }
        }
    }
}