package com.tomhempel.rbarometer.ui

import android.content.Context
import android.content.res.ColorStateList
import android.util.TypedValue
import android.view.View
import android.widget.RadioButton
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.core.widget.TextViewCompat
import com.google.android.material.button.MaterialButton
import com.tomhempel.rbarometer.R

object SessionUi {

    fun applyQuestionScreen(context: Context, root: View?) {
        if (root == null) return
        root.setBackgroundResource(R.drawable.bg_question_screen)

        root.findViewById<TextView>(R.id.textView)?.let { tv ->
            TextViewCompat.setTextAppearance(tv, R.style.TextAppearance_Barometer_QuestionHint)
            tv.setLineSpacing(0f, 1.25f)
        }
        root.findViewById<TextView>(R.id.question)?.let { tv ->
            TextViewCompat.setTextAppearance(tv, R.style.TextAppearance_Barometer_QuestionTitle)
            tv.setLineSpacing(0f, 1.3f)
        }

        root.findViewById<MaterialButton>(R.id.Qfinish)?.let { btn ->
            btn.setTextAppearance(R.style.TextAppearance_Barometer_PrimaryButton)
            btn.elevation = context.resources.getDimension(R.dimen.session_btn_elevation)
        }
    }

    fun styleAnswerChoice(context: Context, radioButton: RadioButton) {
        val density = context.resources.displayMetrics.density
        val padH = (18 * density).toInt()
        val padV = (14 * density).toInt()
        radioButton.setPadding(padH, padV, padH, padV)
        radioButton.background = ContextCompat.getDrawable(context, R.drawable.bg_answer_choice)
        radioButton.buttonTintList = ColorStateList.valueOf(
            ContextCompat.getColor(context, R.color.brand_purple),
        )
        radioButton.setTextColor(ContextCompat.getColor(context, R.color.brand_text_dark))
        radioButton.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16f)
        radioButton.letterSpacing = 0.01f
    }
}
