package com.dano.test1.questionnaire.handlers

import android.view.View
import android.widget.*
import com.dano.test1.questionnaire.GlobalValues
import com.dano.test1.LanguageManager
import com.dano.test1.MyApp
import com.dano.test1.questionnaire.QuestionHandler
import com.dano.test1.questionnaire.QuestionItem
import com.dano.test1.R
import com.dano.test1.network.TokenStore
import com.dano.test1.utils.ViewUtils
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

/*
Zweck :
- Steuert die Eingabeseite für „Client Code“ und „Coach Code“ innerhalb des Fragebogen-Flows.
*/

class HandlerClientCoachCode(
    private val answers: MutableMap<String, Any>,
    private val languageID: String,
    private val goToNextQuestion: () -> Unit,
    private val goToPreviousQuestion: () -> Unit,
    private val showToast: (String) -> Unit,
) : QuestionHandler {

    private lateinit var question: QuestionItem.ClientCoachCodeQuestion
    private lateinit var layout: View

    override fun bind(layout: View, question: QuestionItem) {
        if (question !is QuestionItem.ClientCoachCodeQuestion) return

        this.layout = layout
        this.question = question

        val clientCodeField = layout.findViewById<EditText>(R.id.client_code)
        val coachCodeField  = layout.findViewById<EditText>(R.id.coach_code)
        val questionTextView = layout.findViewById<TextView>(R.id.question)
        val titleTextView    = layout.findViewById<TextView>(R.id.textView)

        questionTextView.text = question.question?.let { LanguageManager.getText(languageID, it) } ?: ""

        ViewUtils.setTextSizePercentOfScreenHeight(titleTextView,    0.03f)
        ViewUtils.setTextSizePercentOfScreenHeight(questionTextView, 0.03f)
        ViewUtils.setTextSizePercentOfScreenHeight(clientCodeField,  0.025f)
        ViewUtils.setTextSizePercentOfScreenHeight(coachCodeField,   0.025f)

        // Client-Code: nur verwenden, wenn bereits geladen
        val loadedClientCode = GlobalValues.LOADED_CLIENT_CODE
        if (!loadedClientCode.isNullOrBlank()) {
            clientCodeField.setText(loadedClientCode)
            clientCodeField.isEnabled = false
        } else {
            clientCodeField.setText("")
            clientCodeField.isEnabled = true
        }

        // Coach-Code immer aus dem Login (TokenStore) setzen und sperren
        val coachFromLogin = TokenStore.getUsername(layout.context)
        if (!coachFromLogin.isNullOrBlank()) {
            coachCodeField.setText(coachFromLogin)
            ViewUtils.lockEditField(coachCodeField) // optisch & technisch gesperrt
        } else {
            // Falls (theoretisch) kein Login-Username vorhanden ist, verhalten wie bisher
            coachCodeField.setText(answers["coach_code"] as? String ?: "")
            coachCodeField.isEnabled = true
        }

        layout.findViewById<Button>(R.id.Qnext).setOnClickListener {
            onNextClicked(clientCodeField, coachCodeField)
        }
        layout.findViewById<Button>(R.id.Qprev).setOnClickListener {
            onPreviousClicked(clientCodeField, coachCodeField)
        }
    }

    private fun onNextClicked(clientCodeField: EditText, coachCodeField: EditText) {
        val loadedClientCode = GlobalValues.LOADED_CLIENT_CODE

        if (!validate()) {
            val message = LanguageManager.getText(languageID, "fill_both_fields")
            showToast(message)
            return
        }

        val clientCode = clientCodeField.text.toString()
        // Erzwinge Coach-Code aus Login (falls vorhanden)
        val coachCode = TokenStore.getUsername(layout.context) ?: coachCodeField.text.toString()

        // Prüfen, ob die DB-Datei vor dem Zugriff existiert
        val dbPath = layout.context.getDatabasePath("questionnaire_database")
        val dbExistedBefore = dbPath.exists()

        CoroutineScope(Dispatchers.IO).launch {
            val existingClient = MyApp.database.clientDao().getClientByCode(clientCode)

            withContext(Dispatchers.Main) {
                if (existingClient != null && clientCodeField.isEnabled) {
                    val message = LanguageManager.getText(languageID, "client_code_exists")
                    showToast(message)
                } else {
                    saveAnswers(clientCode, coachCode)
                    goToNextQuestion()

                    if (!dbExistedBefore) {
                        MyApp.database.close()
                        dbPath.delete()
                        val journalFile = layout.context.getDatabasePath("questionnaire_database-journal")
                        journalFile.delete()
                    }
                }
            }
        }
    }

    private fun onPreviousClicked(clientCodeField: EditText, coachCodeField: EditText) {
        val clientCode = clientCodeField.text.toString()
        val coachCode = TokenStore.getUsername(layout.context) ?: coachCodeField.text.toString()
        saveAnswers(clientCode, coachCode)
        goToPreviousQuestion()
    }

    override fun validate(): Boolean {
        val clientCode = layout.findViewById<EditText>(R.id.client_code).text
        val coachText   = layout.findViewById<EditText>(R.id.coach_code).text
        return clientCode.isNotBlank() && coachText.isNotBlank()
    }

    private fun saveAnswers(clientCode: String, coachCode: String) {
        GlobalValues.LAST_CLIENT_CODE = clientCode
        answers["client_code"] = clientCode
        // Speichere garantierten Coach-Code aus Login bevorzugt
        val loginCoach = TokenStore.getUsername(layout.context)
        answers["coach_code"] = loginCoach ?: coachCode
    }

    override fun saveAnswer() {
        // Not used
    }

}
