package com.dano.test1.questionnaire

import android.widget.Button
import com.dano.test1.LocalizationHelper
import com.dano.test1.R

open class QuestionnaireGeneric(private val questionnaireFileName: String) : QuestionnaireBase<Unit>() {

    override fun startQuestionnaire() {
        val (meta, questionsList) = loadQuestionnaireFromJson(questionnaireFileName)
        questionnaireMeta = meta
        questions = questionsList
        currentIndex = 0
        showCurrentQuestion()
    }

    override fun showCurrentQuestion() {
        val question = questions[currentIndex]

        val layoutResId = getLayoutResId(question.layout ?: "default_layout")

        if (layoutResId == 0) {
            showEmptyScreen()
            return
        }

        navigateTo(layoutResId) { layout ->

            LocalizationHelper.localizeViewTree(layout, languageID)

            layout.findViewById<Button>(R.id.Qprev)?.setOnClickListener {
                goToPreviousQuestion()
            }

            val handler = createHandlerForQuestion(question)

            if (handler != null) {
                handler.bind(layout, question)
            } else {
                showEmptyScreen()
            }
        }
    }
}
