package com.xty.common.weight

import android.content.Context
import android.text.InputType
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.EditText
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.content.ContextCompat
import com.xty.common.R

class InputView(context: Context, attrs: AttributeSet?) : ConstraintLayout(context, attrs) {
    var leftTitle=""
    var leftTitleColor=0
    var rightTitle=""
    var rightHint=""
    var unit=""
    var isInput=false
    var hasNexIcon=false
    var inputDecimal=false
    init {
        LayoutInflater.from(context).inflate(R.layout.view_input,this,true)

        var obtain=context.obtainStyledAttributes(attrs,R.styleable.InputView)
        leftTitle=obtain.getString(R.styleable.InputView_leftTitle)?:""
        leftTitleColor=obtain.getColor(R.styleable.InputView_leftTitleCol,ContextCompat.getColor(context,R.color.col_313))
        rightTitle=obtain.getString(R.styleable.InputView_rightTitle)?:""
        rightHint=obtain.getString(R.styleable.InputView_rightHint)?:""
        unit=obtain.getString(R.styleable.InputView_rightUnit)?:""
        isInput=obtain.getBoolean(R.styleable.InputView_isInput,false)
        hasNexIcon=obtain.getBoolean(R.styleable.InputView_hasNexIcon,false)
        inputDecimal=obtain.getBoolean(R.styleable.InputView_inputDecimal,false)


        obtain.recycle()
    }

    override fun onFinishInflate() {
        super.onFinishInflate()
        findViewById<TextView>(R.id.mLeft).apply {
            text=leftTitle
            setTextColor(leftTitleColor)
        }


        findViewById<TextView>(R.id.mTvUnit).text=unit
        var nextIcon=ContextCompat.getDrawable(context,R.mipmap.ic_next)!!
        if(isInput){
            findViewById<TextView>(R.id.mTv).visibility= View.GONE
            findViewById<EditText>(R.id.mEdit).apply {
                 visibility= View.VISIBLE
                if(inputDecimal){
                    inputType = InputType.TYPE_CLASS_NUMBER or InputType.TYPE_NUMBER_FLAG_DECIMAL
                }
                hint=rightHint
                setText(rightTitle)
                if(hasNexIcon){
                    nextIcon.setBounds(0,0,nextIcon.minimumWidth,nextIcon.minimumHeight)
                    setCompoundDrawables(null,null,nextIcon,null)
                }
            }

        }else{
            findViewById<TextView>(R.id.mTv).apply {
                visibility= View.VISIBLE
                hint=rightHint
                text=rightTitle
                if(hasNexIcon){
                    nextIcon.setBounds(0,0,nextIcon.minimumWidth,nextIcon.minimumHeight)
                    setCompoundDrawables(null,null,nextIcon,null)
                }
            }
            findViewById<TextView>(R.id.mEdit).visibility= View.GONE
        }
    }


    /**
    * 设置界面信息
     * newStatus 是否只是展示  仅用于输入状态下的修改
    */
    fun setValues(str:String?,newStatus:Boolean=isInput){
        if(newStatus==isInput){
            if(isInput){
                findViewById<EditText>(R.id.mEdit).setText(str?:"")
            }else{
                findViewById<TextView>(R.id.mTv).text=str?:""
            }
        }else{
            isInput=newStatus
            if(isInput){
                findViewById<TextView>(R.id.mTv).visibility= View.GONE
                findViewById<EditText>(R.id.mEdit).apply {
                    visibility=View.VISIBLE
                   setText(str?:"")
                }
            }else{
                findViewById<TextView>(R.id.mTv).apply {
                    visibility=View.VISIBLE
                    text=str?:""
                }
                findViewById<EditText>(R.id.mEdit).visibility= View.GONE
            }

        }
    }

    fun gonNextIcon(){
        findViewById<TextView>(R.id.mTv).setCompoundDrawables(null,null,null,null)
    }

    fun getValues():String{
      return  if(isInput){
            findViewById<EditText>(R.id.mEdit).text.trim().toString()
        }else{
            findViewById<TextView>(R.id.mTv).text.toString()
        }
    }


}