package app.furmeet.events.system.helper

import android.text.SpannableString
import android.text.SpannableStringBuilder
import android.text.Spanned
import android.text.SpannedString

class TextualJoiner(private val language: Language) {
    private val middle = ", "
    private val final = when (language) {
        Language.EN -> " and "
        Language.PT -> " e "
        Language.ES -> " y "
    }

    constructor() : this(resources.runtimeLanguage)

    operator fun get(list: List<String>): String = when (list.size) {
        0 -> ""
        1 -> list[0]
        else -> list.subList(0, list.size - 1).joinToString(separator = middle, postfix = final + list.last())
    }

    operator fun get(list: List<Spanned>): Spanned = when (list.size) {
        0 -> SpannedString("")
        1 -> list[0]
        2 -> {
            val ssb = SpannableStringBuilder()
            ssb.append(list[0])
            ssb.append(SpannableString(final))
            ssb.append(list[1])
            ssb
        }
        else -> {
            val ssm = SpannedString(middle)
            val mls = list.flatMap { listOf(it, ssm) }.dropLast(1).toMutableList()
            mls[mls.size - 2] = SpannedString(final)
            val ssb = SpannableStringBuilder()
            mls.forEach { ssb.append(it) }
            ssb
        }

    }
}