package com.tomhempel.rbarometer.questionnaire

import android.view.View
import android.widget.TextView
import com.tomhempel.rbarometer.LanguageManager
import com.tomhempel.rbarometer.R
import com.tomhempel.rbarometer.utils.ViewUtils

/** Renders optional noteBeforeKey / noteAfterKey above and below the main question text. */
object QuestionNotesHelper {

    fun bind(layout: View, languageId: String, noteBeforeKey: String?, noteAfterKey: String?) {
        val before = layout.findViewById<TextView>(R.id.note_before)
        val after = layout.findViewById<TextView>(R.id.note_after)
        bindView(before, languageId, noteBeforeKey)
        bindView(after, languageId, noteAfterKey)
    }

    private fun bindView(view: TextView?, languageId: String, key: String?) {
        if (view == null) return
        val text = key?.takeIf { it.isNotBlank() }?.let { LanguageManager.getText(languageId, it) }.orEmpty()
        if (text.isBlank()) {
            view.visibility = View.GONE
            view.text = ""
        } else {
            view.visibility = View.VISIBLE
            view.text = text
            ViewUtils.setTextSizePercentOfScreenHeight(view, 0.025f)
        }
    }
}

interface QuestionWithNotes {
    val noteBeforeKey: String?
    val noteAfterKey: String?
}
