package cn.muchinfo.rma.view.autoWidget

import android.content.Context
import android.content.res.TypedArray
import android.util.AttributeSet
import android.view.View
import androidx.annotation.StyleRes
import androidx.constraintlayout.widget.ConstraintLayout
import cn.muchinfo.rma.R
import kotlinx.android.synthetic.main.detail_item.view.*

/**
 * 详情等页面的item
 * 才疏学浅，自定义style穿不进来。
 */
class DetailItem @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
): ConstraintLayout(context, attrs, defStyleAttr) {
    private var leftText: String = "" // 左边的文字
    private var rightText: String = "" // 右边的文字
    private var rightTextPadding: Int = 0 // 距离左边的padding
    private var showLine: Boolean = false // 是否显示下部的分割线

    init {
        val typedArray = context.obtainStyledAttributes(attrs, R.styleable.DetailItem)
        initViews(typedArray)
        typedArray.recycle()
    }

    private fun initViews(typedArray: TypedArray) {
        View.inflate(context, R.layout.detail_item, this)
        leftText = typedArray.getString(R.styleable.DetailItem_dt_LeftText).toString()
        rightText = typedArray.getString(R.styleable.DetailItem_dt_rightText).toString()
        rightTextPadding = typedArray.getLayoutDimension(R.styleable.DetailItem_dt_leftPadding, 200)

        showLine = typedArray.getBoolean(R.styleable.DetailItem_dt_showLine, false)
        showLine(showLine)

        if ("null" == leftText) {
            id_title?.text = ""
        } else {
            id_title?.text = leftText
        }
        if ("null" == rightText) {
            id_value?.text = ""
        } else {
            id_value?.text = rightText
        }
        id_title?.setTextAppearance(R.style.CostTitleTextStyle)
        id_value?.setTextAppearance(R.style.CostValueTextStyle)
        id_value?.setPadding(rightTextPadding,
            id_value?.paddingTop!!,
            id_value?.paddingRight!!,
            id_value?.paddingBottom!!)
    }

//region publish

    fun setRightText(rightText: String?) {
        id_value?.text = rightText
    }

    fun setLeftText(leftText: String?) {
        id_title?.text = leftText
    }

    fun setRightValuePadding(leftPadding: Int) {
        id_value?.setPadding(leftPadding,
            id_value?.paddingTop!!,
            id_value?.paddingRight!!,
            id_value?.paddingBottom!!)
    }

    fun setLeftValuePadding(leftValuePadding: Int) {
        id_title?.setPadding(leftValuePadding,
            id_title?.paddingTop!!,
            id_title?.paddingRight!!,
            id_title?.paddingBottom!!)
    }

    fun showLine(show: Boolean) {
        if (show) id_line?.visibility = View.VISIBLE else id_line?.visibility = GONE
    }

    /**
     * 改变左侧的文字样式，参考styles.xml里自定的样式
     */
    fun setLeftStyle(@StyleRes style: Int) {
        id_title?.setTextAppearance(style)
    }

    /**
     * 改变右侧的文字样式，参考styles.xml里自定的样式
     */
    fun setRightStyle(@StyleRes style: Int) {
        id_value?.setTextAppearance(style)
    }

    fun setPaddingLeft(leftPadding: Int?) {
        this.setPadding(leftPadding!!, this.paddingTop, this.paddingRight, this.paddingBottom)
    }
    fun setPaddingTop(topPadding: Int) {
        this.setPadding(this.paddingLeft, topPadding, this.paddingRight, this.paddingBottom)
    }
    fun setPaddingRight(rightPadding: Int) {
        this.setPadding(this.paddingLeft, this.paddingTop, rightPadding, this.paddingBottom)
    }
    fun setPaddingBottom(BottomPadding: Int) {
        this.setPadding(this.paddingLeft, this.paddingTop, this.paddingRight, BottomPadding)
    }
//endregion
}
