package com.adealink.weparty.commonui.drag

import android.content.Context
import android.graphics.Rect
import android.util.AttributeSet
import android.view.View
import android.view.ViewGroup
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.view.doOnLayout
import com.adealink.frame.log.Log
import com.adealink.weparty.R

open class DragViewContainer @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null) :
    ConstraintLayout(context, attrs) {

    private val mDragViewMaker: DragViewMaker = DragViewMaker()

    private var availablePaddingRect = Rect()

    init {
        initAttrs(context, attrs)
        doOnLayout {
            Log.d(TAG_DRAG_VIEW_CONTAINER, "doOnLayout")
            val count = childCount
            for (i in 0 until count) {
                getChildAt(i)?.let {
                    if (it is IDragView) {
                        setDragViewAvailableArea(it)
                    }
                }
            }
        }
    }

    private fun initAttrs(context: Context, attrs: AttributeSet?) {
        if (isInEditMode) {
            return
        }
        context.theme.obtainStyledAttributes(
            attrs,
            R.styleable.FloatViewContainer,
            0, 0
        ).apply {
            val top = getDimensionPixelSize(R.styleable.FloatViewContainer_float_rect_top, 0)
            val bottom = getDimensionPixelSize(R.styleable.FloatViewContainer_float_rect_bottom, 0)
            val left = getDimensionPixelSize(R.styleable.FloatViewContainer_float_rect_left, 0)
            val right = getDimensionPixelSize(R.styleable.FloatViewContainer_float_rect_right, 0)
            availablePaddingRect.set(left, top, right, bottom)
        }
    }

    override fun addView(child: View, index: Int, params: ViewGroup.LayoutParams) {
        super.addView(child, index, params)
        if (child is IDragView) {
            mDragViewMaker.makeViewFloat(child)
            doOnLayout {
                setDragViewAvailableArea(child)
            }
        }
    }

    override fun removeView(view: View?) {
        super.removeView(view)
        if (view is IDragView) {
            mDragViewMaker.remove(view)
        }
    }

    override fun onFinishInflate() {
        super.onFinishInflate()
        makeChildrenFloat()
    }

    override fun onDetachedFromWindow() {
        super.onDetachedFromWindow()
        removeChildrenFromCache()
    }

    private fun removeChildrenFromCache() {
        mDragViewMaker.clear()
    }

    private fun makeChildrenFloat() {
        Log.d(TAG_DRAG_VIEW_CONTAINER, "makeChildrenFloat invoked..")
        val count = childCount
        for (i in 0 until count) {
            getChildAt(i)?.let {
                if (it is IDragView) {
                    mDragViewMaker.makeViewFloat(it as IDragView)
                }
            }
        }
    }

    private fun setDragViewAvailableArea(view: IDragView) {
        val rect = Rect(
            availablePaddingRect.left,
            availablePaddingRect.top,
            width - availablePaddingRect.right,
            height - availablePaddingRect.bottom
        )
        Log.d(TAG_DRAG_VIEW_CONTAINER, "setChildAvailableArea, $rect")
        view.setAvailableArea(rect)
    }
}
