package com.xty.common.weight

import android.content.Context
import android.graphics.drawable.Drawable
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.content.ContextCompat
import com.xty.common.R

class VisceraView(context: Context, attrs: AttributeSet?) : ConstraintLayout(context, attrs) {

    val statusStr by lazy{resources.getStringArray(R.array.status_array)}

    var image:Drawable?=null
    var status:String=""
    var viscera:String=""
    var isRight=false
    lateinit var mTvStatus:TextView
    lateinit var mSrc:ImageView
    lateinit var mViscera:TextView

    lateinit var mTvRightStatus:TextView
    lateinit var mRightSrc:ImageView
    lateinit var mRightViscera:TextView


    lateinit var mLeftLin:LinearLayout
    lateinit var mLin:LinearLayout
    init {
        LayoutInflater.from(context).inflate(R.layout.item_viscera,this,true)
        var recycle=context.obtainStyledAttributes(attrs, R.styleable.VisceraView)
        image=recycle.getDrawable(R.styleable.VisceraView_image)
        status=recycle.getString(R.styleable.VisceraView_status_txt).toString()
        viscera=recycle.getString(R.styleable.VisceraView_viscera).toString()
        isRight=recycle.getBoolean(R.styleable.VisceraView_is_right,false)
        recycle.recycle()

    }

    override fun onFinishInflate() {
        super.onFinishInflate()
        mTvStatus=findViewById(R.id.mTvStatus)
        mSrc=findViewById(R.id.mSrc)
        mViscera=findViewById(R.id.mViscera)
        mLin=findViewById(R.id.mLin)

        mTvRightStatus=findViewById(R.id.mTvRightStatus)
        mRightSrc=findViewById(R.id.mRightSrc)
        mRightViscera=findViewById(R.id.mRightViscera)
        mLeftLin=findViewById(R.id.mLeftLin)

        if(isRight){
            mTvStatus.visibility= View.GONE
            mLin.visibility= View.GONE

            mTvRightStatus.text=status
            mRightViscera.text=viscera
            mRightSrc.setImageDrawable(image)
        }else{
            mTvRightStatus.visibility= View.GONE
            mLeftLin.visibility= View.GONE
            mTvStatus.text=status
            mViscera.text=viscera
            mSrc.setImageDrawable(image)
        }
    }

    /**
     * 状态设置
     */
    fun setInfo(  status:Int, statusSrc:Array<Int>){
        if(status>4){
            return
        }
        var drawbles= ContextCompat.getDrawable(context,statusSrc[status])!!
        if(isRight){
            mTvRightStatus.text=statusStr[status]
            mRightSrc.setImageDrawable(drawbles)
        }else{
            mTvStatus.text=statusStr[status]
            mSrc.setImageDrawable(drawbles)
        }
    }

    /**
     * 状态设置
     */
    fun setInfo(status: Int, statusSrc: Array<Int>, viscera: String) {
        if (status > 4) {
            return
        }
        this.viscera = viscera
        var drawbles = ContextCompat.getDrawable(context, statusSrc[status])!!
        if (isRight) {
            mTvRightStatus.text = statusStr[status]
            mRightSrc.setImageDrawable(drawbles)
            mRightViscera.text=viscera
        } else {
            mTvStatus.text = statusStr[status]
            mSrc.setImageDrawable(drawbles)
            mViscera.text=viscera
        }
    }

    /**
     * 设置字体样式
     */
    fun setTextStyle(fontSize: Float, color: Int) {
        if (isRight) {
            mRightViscera.textSize = fontSize
            mRightViscera.setTextColor(color)
        } else {
            mViscera.textSize = fontSize
            mViscera.setTextColor(color)
        }
    }
}