package com.qimai.android.widget

import android.content.Context
import android.util.AttributeSet
import android.view.View
import android.view.ViewGroup


/**
 *   created by wangwei   ON 4/17/20  email:wangwei_5521@163.com
 *   @version 1.1.1
 *   @Description
 **/
class StoreLocalLayout : ViewGroup {
    constructor(context: Context?) : this(context, null)
    constructor(context: Context?, attrs: AttributeSet?) : this(context, attrs, 0)
    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    )

    private val mAllView = mutableListOf<List<View>>()
    private val mLineHeight = mutableListOf<Int>()
    override fun onLayout(changed: Boolean, l: Int, t: Int, r: Int, b: Int) {
        if (childCount == 0) return
        var left = 0
        var top = 0
        mAllView.clear()
        mLineHeight.clear()
        var lineHeight = 0
        var lineViews = arrayListOf<View>()
        if (childCount == 1){
            val child = getChildAt(0)
            val lc = left
            val tc = top
            val rc =  measuredWidth
            val bc = tc + child.measuredHeight
            child.layout(lc, tc, rc, bc)
            return
        }

        for (index in 0 until  childCount) {
            val child = getChildAt(index)
            lineHeight = Math.max(child.measuredHeight, lineHeight)
            lineViews.add(child)
            if ((index + 1) % 2 == 0) {
                mLineHeight.add(lineHeight)
                mAllView.add(lineViews)
                lineViews = arrayListOf<View>()
            }
        }
        mLineHeight.add(lineHeight)
        mAllView.add(lineViews)

        mAllView.forEachIndexed { index, list ->
            val mLineHeight = mLineHeight[index]
            list.forEachIndexed { index, view ->
                val child = view
                val lc = left
                val tc = top
                val rc = lc + measuredWidth/2
                val bc = tc + child.measuredHeight
                child.layout(lc, tc, rc, bc)
                left +=  measuredWidth/2
            }
            left = 0
            top += mLineHeight
        }

    }

    override fun measureChild(
        child: View?,
        parentWidthMeasureSpec: Int,
        parentHeightMeasureSpec: Int
    ) {
        super.measureChild(child, parentWidthMeasureSpec, parentHeightMeasureSpec)
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        if (childCount == 0) return

        val heightSize = MeasureSpec.getSize(heightMeasureSpec)
        val widthSize = MeasureSpec.getSize(widthMeasureSpec)
        for (i in 0 until childCount ) {
            if (i == 0 && childCount == 1) {
                val widthSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.AT_MOST)
                val heightSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)
                measureChild(getChildAt(i), widthSpec, heightSpec)
            }
            if (childCount > 1) {
                val widthSpec = MeasureSpec.makeMeasureSpec(widthSize / 2, MeasureSpec.EXACTLY)
                val heightSpec = MeasureSpec.makeMeasureSpec(300, MeasureSpec.EXACTLY)
                measureChild(getChildAt(i), widthSpec, heightSpec)
            }
        }
        setMeasuredDimension(widthSize, heightSize)

    }


}