package com.adealink.weparty.commonui.ext

import android.graphics.LinearGradient
import android.graphics.Shader
import android.graphics.Typeface
import android.text.TextPaint
import android.text.TextUtils
import android.widget.TextView
import kotlin.math.atan

fun TextView.setVerticalLinearGradient(colors: List<Int>, positions: List<Float>) {
    val lineHeight = this.lineHeight.toFloat()
    val shader = LinearGradient(
        0f, 0f, 0f, lineHeight,
        colors.toIntArray(), positions.toFloatArray(), Shader.TileMode.CLAMP
    )
    paint.shader = shader
}

fun TextView.setHorizontalLinearGradient(colors: List<Int>, positions: List<Float>) {
    val lineLength = this.paint.textSize * this.text.length
    val shader = LinearGradient(
        0f, 0f, lineLength, 0f,
        colors.toIntArray(), positions.toFloatArray(), Shader.TileMode.CLAMP
    )
    paint.shader = shader
}

/**
 * @param angle (0 is left to right, 90 is bottom to top)
 */
fun TextView.setLinearGradient(angle: Int, colors: List<Int>, positions: List<Float>) {
    val lineWidth = this.paint.textSize * this.text.length
    val lineHeight = this.lineHeight.toFloat()
    val realAngle = (angle + 360) % 360
    val shader = when (realAngle) {
        in 0 until 90 -> {
            LinearGradient(
                0f, lineHeight, //启点左下角
                lineWidth, lineWidth * atan(realAngle.toDouble()).toFloat(),
                colors.toIntArray(), positions.toFloatArray(), Shader.TileMode.CLAMP
            )
        }

        in 90 until 180 -> {
            LinearGradient(
                lineWidth, lineHeight, //启点右下角
                0f.coerceAtLeast((lineWidth - lineHeight * atan((realAngle - 90).toDouble())).toFloat()), 0f,
                colors.toIntArray(), positions.toFloatArray(), Shader.TileMode.CLAMP
            )
        }

        in 180 until 270 -> {
            LinearGradient(
                lineWidth, 0f, //启点右上角
                0f, lineWidth * atan((realAngle - 180).toDouble()).toFloat(),
                colors.toIntArray(), positions.toFloatArray(), Shader.TileMode.CLAMP
            )
        }

        else -> {
            //270 ~ 360
            LinearGradient(
                0f, 0f, //启点左上角
                lineHeight * atan((realAngle - 270).toDouble()).toFloat(), lineHeight,
                colors.toIntArray(), positions.toFloatArray(), Shader.TileMode.CLAMP
            )
        }
    }
    paint.shader = shader
}

fun TextView.setCanMarqueeScrolled(){
    setSingleLine()
    ellipsize = TextUtils.TruncateAt.MARQUEE
    isSelected = true
    marqueeRepeatLimit = -1
}

fun TextPaint.setHorizontalLinearGradient(text: String, colors: List<Int>, positions: List<Float>) {
    val lineLength = textSize * text.length
    val shader = LinearGradient(
        0f, 0f, lineLength, 0f,
        colors.toIntArray(), positions.toFloatArray(), Shader.TileMode.CLAMP
    )
    setShader(shader)
}

fun TextView.mediumText() {
    paint.isFakeBoldText = true
}

fun TextView.boldText() {
    typeface = Typeface.DEFAULT_BOLD
}

fun TextView.defaultText() {
    typeface = Typeface.DEFAULT
}