package com.tomhempel.rbarometer.ui

import android.content.res.ColorStateList
import android.graphics.Typeface
import android.text.TextUtils
import android.view.Gravity
import android.widget.Button
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import com.tomhempel.rbarometer.R
import com.tomhempel.rbarometer.utils.ViewUtils
import com.google.android.material.button.MaterialButton
import kotlin.math.roundToInt

/**
 * Shared styling for questionnaire list cards on the opening screen.
 */
object QuestionnaireCardUi {

    data class CardParts(
        val title: TextView,
        val subtitle: TextView,
        val chip: TextView,
    )

    /** Keeps status chip readable without overlapping title/subtitle on long server strings. */
    fun styleStatusChip(activity: AppCompatActivity, chip: TextView) {
        val dm = activity.resources.displayMetrics
        chip.maxWidth = (dm.widthPixels * 0.36f).roundToInt()
            .coerceIn(ViewUtils.dp(activity, 96), ViewUtils.dp(activity, 200))
        chip.maxLines = 3
        chip.isSingleLine = false
        chip.ellipsize = TextUtils.TruncateAt.END
        chip.textAlignment = TextView.TEXT_ALIGNMENT_CENTER
        chip.gravity = Gravity.CENTER
        chip.includeFontPadding = true
        chip.typeface = Typeface.create(chip.typeface, Typeface.BOLD)
        chip.letterSpacing = 0.02f
    }

    fun styleTitle(activity: AppCompatActivity, title: TextView) {
        title.setTextSize(android.util.TypedValue.COMPLEX_UNIT_SP, 19f)
        title.setTextColor(color(activity, R.color.brand_text_dark))
        title.typeface = Typeface.create(title.typeface, Typeface.BOLD)
        title.letterSpacing = -0.01f
        title.maxLines = 3
        title.ellipsize = TextUtils.TruncateAt.END
        title.isSingleLine = false
    }

    fun styleSubtitle(activity: AppCompatActivity, subtitle: TextView) {
        subtitle.setTextSize(android.util.TypedValue.COMPLEX_UNIT_SP, 13f)
        subtitle.setTextColor(color(activity, R.color.brand_text_muted))
        subtitle.maxLines = 4
        subtitle.ellipsize = TextUtils.TruncateAt.END
        subtitle.isSingleLine = false
        subtitle.letterSpacing = 0.01f
    }

    fun setChipLabel(activity: AppCompatActivity, chip: TextView, label: String) {
        styleStatusChip(activity, chip)
        chip.text = label
    }

    fun applyChipStatus(
        activity: AppCompatActivity,
        parts: CardParts,
        label: String,
        backgroundRes: Int,
        textColorRes: Int = R.color.brand_text_on_dark,
    ) {
        setChipLabel(activity, parts.chip, label)
        parts.chip.setBackgroundResource(backgroundRes)
        parts.chip.setTextColor(color(activity, textColorRes))
    }

    fun applyDefaultCardTexts(activity: AppCompatActivity, parts: CardParts) {
        parts.title.setTextColor(color(activity, R.color.brand_text_dark))
        parts.subtitle.setTextColor(color(activity, R.color.brand_text_muted))
    }

    fun setLockedAppearance(activity: AppCompatActivity, button: Button, locked: Boolean) {
        val mb = button as? MaterialButton ?: return
        if (locked) {
            mb.backgroundTintList = ColorStateList.valueOf(color(activity, R.color.brand_card_locked))
            mb.strokeColor = ColorStateList.valueOf(color(activity, R.color.brand_stroke_disabled))
            mb.alpha = 0.92f
        } else {
            mb.backgroundTintList = ColorStateList.valueOf(color(activity, R.color.brand_control_chip_bg))
            mb.strokeColor = ColorStateList.valueOf(
                color(
                    activity,
                    if (button.isEnabled) R.color.brand_stroke_enabled else R.color.brand_control_chip_stroke,
                ),
            )
            mb.alpha = 1f
        }
    }

    fun applyLockedCardTexts(
        activity: AppCompatActivity,
        parts: CardParts,
        lockedLabel: String,
    ) {
        parts.title.setTextColor(color(activity, R.color.brand_card_locked_title))
        parts.subtitle.setTextColor(color(activity, R.color.brand_card_locked_subtitle))
        parts.subtitle.maxLines = 4
        parts.subtitle.isSingleLine = false
        applyLockedChipStyle(activity, parts, lockedLabel)
    }

    fun applyLockedChipStyle(activity: AppCompatActivity, parts: CardParts, lockedLabel: String) {
        setChipLabel(activity, parts.chip, lockedLabel)
        parts.chip.setBackgroundResource(R.drawable.bg_chip_locked)
        parts.chip.setTextColor(color(activity, R.color.brand_chip_locked_text))
    }

    fun applyEmphasisTint(
        activity: AppCompatActivity,
        button: Button,
        emphasize: Boolean,
    ) {
        val mb = button as? MaterialButton ?: return
        mb.backgroundTintList = ColorStateList.valueOf(
            color(activity, if (emphasize) R.color.brand_emphasis_bg else R.color.brand_control_chip_bg),
        )
        mb.strokeColor = ColorStateList.valueOf(
            color(activity, if (emphasize) R.color.brand_stroke_enabled else R.color.brand_control_chip_stroke),
        )
    }

    fun setClickableStroke(activity: AppCompatActivity, button: Button, enabled: Boolean) {
        val mb = button as? MaterialButton ?: return
        mb.strokeWidth = if (enabled) ViewUtils.dp(activity, 2) else ViewUtils.dp(activity, 1)
        mb.strokeColor = ColorStateList.valueOf(
            color(activity, if (enabled) R.color.brand_stroke_enabled else R.color.brand_stroke_disabled),
        )
    }

    private fun color(activity: AppCompatActivity, resId: Int): Int =
        ContextCompat.getColor(activity, resId)
}
