package com.xty.common.weight

import android.content.Context
import android.graphics.Paint
import android.util.AttributeSet
import android.util.TypedValue
import android.widget.RelativeLayout
import android.widget.TextView

/**
* 自定义控件摆放 目前只支持2个控件的摆放
 * 功能：支持换行文字后面紧跟控件
*/
class MyTitleTipView(context: Context?, attrs: AttributeSet?) : RelativeLayout(context, attrs) {

    var paint= Paint().apply {
        val textSizePixels: Float = TypedValue.applyDimension(
            TypedValue.COMPLEX_UNIT_SP, 21f, resources.displayMetrics
        )
        textSize=textSizePixels
    }

    private var tipLeft=10
    private  var str=""
    private  var letterSize=0  //单个文字的长度
    private  var lastLineWidth=0  //最后一行文字的长度
    private  var moreLineSIze=0  //多行文字的 行数
    private  var remainLine=0  //多行的时候是否领启一行



    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)

        val width = MeasureSpec.getSize(widthMeasureSpec)
        if(childCount<2){
            super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        }else{
            str=(getChildAt(0) as TextView).text.toString()

            var maxWidth=paint.measureText(str)
            letterSize=(maxWidth/str.length).toInt()
            var child1Width=getChildAt(0).measuredWidth
            var child1Height=getChildAt(0).measuredHeight
            var child2Width=getChildAt(1).measuredWidth
            var child2Height=getChildAt(1).measuredHeight
            if(child1Width+child2Width+tipLeft<=width){
                setMeasuredDimension(width, child1Height)
            }else{
                var maxLetter=width/letterSize //一行的最多文字
                moreLineSIze=str.length/maxLetter  //共有多少行

                remainLine=str.length%maxLetter //是否有多余的行数
                //刚好一行填满领起一行
                if(remainLine==0){
                    setMeasuredDimension(width, child1Height+child2Height)
                }else{
                    var lastLetterNum=str.length-(moreLineSIze*maxLetter)  //多行文字时  最后一行文字数量
                    lastLineWidth=lastLetterNum*letterSize

                    if(child2Width+lastLineWidth+tipLeft<=width){
                        setMeasuredDimension(width, child1Height)
                    }else{
                        setMeasuredDimension(width, child1Height+child2Height)
                    }
                }
            }
            getChildAt(0).measure(MeasureSpec.makeMeasureSpec(child1Width, MeasureSpec.EXACTLY),
                MeasureSpec.makeMeasureSpec(child1Height, MeasureSpec.EXACTLY));
            getChildAt(1).measure(MeasureSpec.makeMeasureSpec(child2Width, MeasureSpec.EXACTLY),
                MeasureSpec.makeMeasureSpec(child2Height, MeasureSpec.EXACTLY));
        }

    }
    override fun onLayout(changed: Boolean, l: Int, t: Int, r: Int, b: Int) {
        super.onLayout(changed, l, t, r, b)
        if(childCount<2){
            return
        }
    var child1=getChildAt(0)
    var child2=getChildAt(1)
        child1.layout(0, 0, child1.measuredWidth, child1.measuredHeight)
        if(child1.measuredWidth+child2.measuredWidth+tipLeft<=width){
            var top=child1.measuredHeight/2-child2.measuredHeight/2
            child2.layout( child1.measuredWidth+tipLeft, top, child1.measuredWidth+child2.measuredWidth+tipLeft,  top+child2.measuredHeight)
        }else{
            if(remainLine==0){
                child2.layout(0, child1.measuredHeight, child2.measuredWidth,  child1.measuredHeight+child2.measuredHeight)
            }else{
                if(child2.measuredWidth+lastLineWidth+tipLeft<=width){
                    var singleLinHeight=child1.measuredHeight/ (moreLineSIze+1)
                    var top=singleLinHeight/2-child2.measuredHeight/2

                    var newTop=singleLinHeight*moreLineSIze+top

                    child2.layout( lastLineWidth+tipLeft, newTop, lastLineWidth+tipLeft+child2.measuredWidth,  newTop+child2.measuredHeight)


                }else{
                    child2.layout(0, child1.measuredHeight, child2.measuredWidth,  child1.measuredHeight+child2.measuredHeight)
                }

            }


        }





    }



}