package com.guadou.cs_cptservices.binding

import android.graphics.Paint
import android.graphics.drawable.Drawable
import android.os.Build
import android.text.Html
import android.text.TextUtils
import android.widget.TextView
import androidx.databinding.BindingAdapter
import com.guadou.lib_baselib.font_text_view.TypefaceUtil

/**
 * 文本的设置
 */

//为空的时候设置默认值
@BindingAdapter("text", "default", requireAll = false)
fun setText(view: TextView, text: CharSequence?, default: String?) {
    if (text == null || text.trim() == "" || text.contains("null")) {
        view.text = default
    } else {
        view.text = text
    }
}

//设置Html字体
@BindingAdapter("textHtml")
fun setTextHtml(textView: TextView, text: String?) {
    if (!TextUtils.isEmpty(text)) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            textView.text = Html.fromHtml(text, Html.FROM_HTML_MODE_LEGACY)
        } else {
            textView.text = Html.fromHtml(text)
        }
    } else {
        textView.text = ""
    }
}

/**
 * 下划线
 */
@BindingAdapter("isUnderline")
fun setUnderline(textView: TextView, isUnderline: Boolean) {
    if (isUnderline) {
        textView.paint.flags = Paint.UNDERLINE_TEXT_FLAG//下划线
        textView.paint.isAntiAlias = true //抗锯齿
    } else {
        textView.paint.flags = 0
    }
}

/**
 * 中划线
 */
@BindingAdapter("isCenterLine")
fun isCenterLine(textView: TextView, isUnderline: Boolean) {
    if (isUnderline) {
        textView.paint.flags = Paint.STRIKE_THRU_TEXT_FLAG
        textView.paint.isAntiAlias = true //抗锯齿
    } else {
        textView.paint.flags = 0
    }
}

/**
 * 设置字体
 */
@BindingAdapter("typefaceLight")
fun typefaceLight(textView: TextView, boolean: Boolean) {
    textView.typeface = TypefaceUtil.getLight()
}

@BindingAdapter("typefaceRegular")
fun typefaceRegular(textView: TextView, boolean: Boolean) {
    textView.typeface = TypefaceUtil.getRegular()
}

@BindingAdapter("typefaceMedium")
fun typefaceMedium(textView: TextView, boolean: Boolean) {
    textView.typeface = TypefaceUtil.getMedium()
}

@BindingAdapter("typefaceBold")
fun typefaceSFBold(textView: TextView, boolean: Boolean) {
    textView.typeface = TypefaceUtil.getBold()
}

@BindingAdapter("typefaceMediumOrBold")
fun typefaceMediumOrBold(textView: TextView, boolean: Boolean) {
    if (boolean) {  //true为粗体
        textView.typeface = TypefaceUtil.getBold()
    } else {
        textView.typeface = TypefaceUtil.getMedium()
    }
}

/**
 * 设置左右的Drawable图标
 */
@BindingAdapter("setRightDrawable")
fun setRightDrawable(textView: TextView, drawable: Drawable?) {
    if (drawable == null) {
        textView.setCompoundDrawables(null, null, null, null)
    } else {
        drawable.setBounds(0, 0, drawable.minimumWidth, drawable.minimumHeight)
        textView.setCompoundDrawables(null, null, drawable, null)
    }
}

@BindingAdapter("setLeftDrawable")
fun setLeftDrawable(textView: TextView, drawable: Drawable?) {
    if (drawable == null) {
        textView.setCompoundDrawables(null, null, null, null)
    } else {
        drawable.setBounds(0, 0, drawable.minimumWidth, drawable.minimumHeight)
        textView.setCompoundDrawables(drawable, null, null, null)
    }
}
