package ch.threema.app.multidevice.wizard.steps

import android.annotation.SuppressLint
import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.ImageView
import ch.threema.app.R
import ch.threema.base.utils.getThreemaLogger
import com.google.android.material.card.MaterialCardView

private val logger = getThreemaLogger("LinkNewDeviceEmojiSelectionView")

/**
 * A button-like view that displays a sequence of three rendezvous emojis
 */
class LinkNewDeviceEmojiSelectionView : MaterialCardView {
    constructor(context: Context) : super(context)

    constructor(context: Context, attrs: AttributeSet) : super(context, attrs)

    constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr,
    )

    private val emojiViews = arrayOfNulls<ImageView>(3)
    private val rendezvousEmojis = RendezvousEmojis()

    init {
        LayoutInflater.from(context).inflate(R.layout.view_emoji_selection, this, true)

        strokeWidth = 0
        cardElevation = 0F

        emojiViews[0] = findViewById(R.id.emoji1)
        emojiViews[1] = findViewById(R.id.emoji2)
        emojiViews[2] = findViewById(R.id.emoji3)
    }

    /**
     * Display the provided emojis in this view.
     */
    fun setEmojis(emojiIndexes: IntArray) {
        if (emojiIndexes.size == emojiViews.size) {
            repeat(emojiViews.size) { i ->
                fillEmojiViewContents(emojiViews[i], emojiIndexes[i])
            }
        } else {
            logger.debug("Invalid number of emoji indexes")
        }
    }

    /**
     * Display a set of random emojis in this view. The emojis are not unique so the sequence may contain duplicates
     */
    fun setRandomEmojis() {
        repeat(emojiViews.size) { i ->
            val emojiIndex = (0 until rendezvousEmojis.emojiList.size).random()
            fillEmojiViewContents(emojiViews[i], emojiIndex)
        }
    }

    private fun fillEmojiViewContents(emojiView: ImageView?, emojiIndex: Int) {
        emojiView?.let {
            it.setImageResource(getEmojiResourceID(emojiIndex))
            it.contentDescription = resources.getString(getEmojiStringID(emojiIndex))
        }
    }

    @SuppressLint("DiscouragedApi")
    fun getEmojiResourceID(emojiIndex: Int): Int {
        return resources.getIdentifier(
            "ic_emoji_" + rendezvousEmojis.emojiList[emojiIndex].lowercase(),
            "drawable",
            context.packageName,
        )
    }

    @SuppressLint("DiscouragedApi")
    fun getEmojiStringID(emojiIndex: Int): Int {
        return resources.getIdentifier(
            "rendezvous_emoji_" + rendezvousEmojis.emojiList[emojiIndex],
            "string",
            context.packageName,
        )
    }
}
