package com.remax.visualnovel.extension

import android.content.Context
import android.graphics.LinearGradient
import android.graphics.Shader
import android.text.Layout
import android.text.StaticLayout
import android.view.View
import android.widget.TextView
import androidx.annotation.StringRes
import androidx.core.view.isVisible
import com.remax.visualnovel.R
import com.remax.visualnovel.app.base.app.CommonApplicationProxy
import com.remax.visualnovel.constant.Gender
import com.remax.visualnovel.utils.SpanUtils
import com.remax.visualnovel.utils.spannablex.SpanDsl
import com.remax.visualnovel.widget.uitoken.handleUIToken
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext


fun TextView.setUnReadCount(num: Int?) {
    val realNum = num ?: 0
    text = if (realNum > 99) "···" else realNum.toString()
    isVisible = realNum > 0
}

fun TextView.setGenderLeft(gender: Int?) {
    when (gender) {
        Gender.MALE.value -> drawableLeft(R.mipmap.ic_gender_male)

        Gender.FEMALE.value -> drawableLeft(R.mipmap.ic_gender_female)

        Gender.NONCONFORMING.value -> drawableLeft(R.mipmap.ic_gender_nonconforming)

        Gender.OTHER.value -> drawableLeft(R.mipmap.ic_gender_nonconforming)
    }
}

fun TextView.drawableLeft(resId: Int) {
    setCompoundDrawablesRelativeWithIntrinsicBounds(resId, 0, 0, 0)
}

fun TextView.drawableRight(resId: Int) {
    setCompoundDrawablesRelativeWithIntrinsicBounds(0, 0, resId, 0)
}

fun Int?.getSexText() = when (this) {
    Gender.MALE.value -> CommonApplicationProxy.application.getString(Gender.MALE.txtRes)

    Gender.FEMALE.value -> CommonApplicationProxy.application.getString(Gender.FEMALE.txtRes)

    Gender.NONCONFORMING.value -> CommonApplicationProxy.application.getString(Gender.NONCONFORMING.txtRes)

    Gender.OTHER.value -> CommonApplicationProxy.application.getString(Gender.OTHER.txtRes)

    else -> ""
}

/**
 * 快速根据token设置字体
 * @receiver Context
 * @param fontToken Int token资源
 */
fun SpanDsl.setSpanTypeFace(context: Context, @StringRes fontToken: Int = 0) {
    context.handleUIToken(fontToken)?.textFont?.run {
        typeface(context.getTextFontTypeface(this.typeFace))
        absoluteSize(this.textFontSize.toInt(), false)
    }
}

fun TextView.showGradientColor(colors: IntArray, content: String? = null) {
    SpanUtils.with(this).append(content ?: "").setShader(
        LinearGradient(
            0f,
            0f,
            paint.measureText(content ?: ""),
            0f,
            colors,
            null,
            Shader.TileMode.CLAMP
        )
    ).create()
}

fun TextView.calculateLineCount(content: String?, availableWidth: Int, introAll: View) {
    introAll.isVisible = false
    CoroutineScope(Dispatchers.IO).launch {
        val staticLayout = StaticLayout.Builder
            .obtain(content ?: "", 0, content?.length ?: 0, paint, availableWidth)
            .setAlignment(Layout.Alignment.ALIGN_NORMAL)
            .setLineSpacing(lineSpacingExtra, lineSpacingMultiplier)
            .setIncludePad(includeFontPadding)
            .build()
        val lineCount = staticLayout.lineCount
        withContext(Dispatchers.Main) {
            if (lineCount > 3) {
                maxLines = 3
                introAll.isVisible = true
            }
            text = content
        }
    }
}