package com.remax.visualnovel.widget.ui

import android.content.Context
import android.util.AttributeSet
import com.remax.visualnovel.R
import com.remax.visualnovel.utils.spannablex.utils.dp
import com.remax.visualnovel.widget.uitoken.handleUIToken
import com.remax.visualnovel.widget.uitoken.view.UITokenTextView
import androidx.core.content.withStyledAttributes

/**
 * Created by wl on 2022/9/6
 * IconFont控件 可用于icon+文字、纯icon、纯文字
 */
open class IconFontTextView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0) :
    UITokenTextView(context, attrs, defStyleAttr) {

    private var iconFontStart: String? = null //加在文字前面的icon
    private var iconFontEnd: String? = null //文字后面icon
    private var iconFontTop: String? = null
    private var iconFontBottom: String? = null

    private var iconFontDrawablePadding: Int = 0 //drawable间距 px
    private var iconFontSize: Int = 0 //iconFont的大小
    private var iconFontColorToken: String? = null //iconFont颜色

    init {
        context.withStyledAttributes(attrs, R.styleable.IconFontTextView) {
            iconFontStart = getString(R.styleable.IconFontTextView_iconFontStart)
            iconFontEnd = getString(R.styleable.IconFontTextView_iconFontEnd)
            iconFontTop = getString(R.styleable.IconFontTextView_iconFontTop)
            iconFontBottom = getString(R.styleable.IconFontTextView_iconFontBottom)
            iconFontDrawablePadding = getDimension(R.styleable.IconFontTextView_iconFontDrawablePadding, 0f).toInt()
            iconFontSize = getDimensionPixelOffset(R.styleable.IconFontTextView_iconFontSize, 0)
            iconFontColorToken = getString(R.styleable.IconFontTextView_iconFontColorToken)
        }

        setIconFontDrawable(iconFontStart, iconFontTop, iconFontEnd, iconFontBottom, iconSize = iconFontSize, isDp = false)

        transformationMethod = null
    }

    /**
     * 设置iconFont
     * @param startIconFont 左图标
     * @param topIconFont 上图标
     * @param endIconFont 右图标
     * @param bottomIconFont 下图标
     * @param iconColorToken icon的color token
     * @param iconSize icon的textSize
     * @param iconPadding icon与文字的间距
     */
    fun setIconFontDrawable(
        startIconFont: String? = null,
        topIconFont: String? = null,
        endIconFont: String? = null,
        bottomIconFont: String? = null,
        iconColorToken: String? = null,
        iconSize: Int = 0,
        isDp: Boolean = true,
        iconPadding: Int? = null
    ) {
        var startDrawable: IconFontDrawable? = null
        var endDrawable: IconFontDrawable? = null
        var topDrawable: IconFontDrawable? = null
        var bottomDrawable: IconFontDrawable? = null
        if (!startIconFont.isNullOrEmpty()) {
            startDrawable = IconFontDrawable(context, startIconFont).color(getFontColor(iconColorToken)).sizePx(getFontSize(iconSize, isDp))
        }
        if (!endIconFont.isNullOrEmpty()) {
            endDrawable =
                IconFontDrawable(context, endIconFont).color(getFontColor(iconColorToken)).sizePx(getFontSize(iconSize, isDp))
        }
        if (!topIconFont.isNullOrEmpty()) {
            topDrawable = IconFontDrawable(context, topIconFont).color(getFontColor(iconColorToken)).sizePx(getFontSize(iconSize, isDp))
        }
        if (!bottomIconFont.isNullOrEmpty()) {
            bottomDrawable = IconFontDrawable(context, bottomIconFont).color(getFontColor(iconColorToken)).sizePx(getFontSize(iconSize, isDp))
        }
        if (startIconFont == null && topIconFont == null && endIconFont == null && bottomIconFont == null) {
            compoundDrawablePadding = 0
            setCompoundDrawablesRelativeWithIntrinsicBounds(0, 0, 0, 0)
        } else {
            if (iconPadding != null) {
                iconFontDrawablePadding = iconPadding.dp
                compoundDrawablePadding = iconFontDrawablePadding
            } else {
                compoundDrawablePadding = iconFontDrawablePadding
            }
            setCompoundDrawablesRelative(startDrawable, topDrawable, endDrawable, bottomDrawable)
        }
    }

    private fun getFontColor(iconColorToken: String?): Int {
        return if (iconFontColorToken.isNullOrBlank() && iconColorToken.isNullOrBlank()) {
            currentTextColor
        } else {
            if (!iconColorToken.isNullOrBlank()) {
                iconFontColorToken = iconColorToken
                iconColorToken.handleUIToken(context)?.color ?: 0
            } else {
                iconFontColorToken?.handleUIToken(context)?.color ?: 0
            }
        }
    }

    private fun getFontSize(iconSize: Int, isDp: Boolean): Int = if (iconSize == 0 && iconFontSize == 0) textSize.toInt() else {
        if (iconSize != 0) {
            if (isDp) iconSize.dp else iconSize
        } else iconFontSize
    }

}

