package com.remax.visualnovel.widget.uitoken.expend.dsl

import android.annotation.SuppressLint
import android.graphics.Color
import android.graphics.Rect
import android.graphics.drawable.ColorDrawable
import android.text.InputFilter
import android.view.MotionEvent
import android.view.TouchDelegate
import android.view.View
import android.view.ViewGroup
import android.view.WindowManager
import android.widget.TextView
import androidx.coordinatorlayout.widget.ViewGroupUtils
import androidx.core.view.isVisible
import androidx.fragment.app.DialogFragment
import com.remax.visualnovel.utils.spannablex.utils.dp

fun DialogFragment.fullScreenMode() {
    dialog?.window?.apply {
        attributes?.apply {
            width = WindowManager.LayoutParams.MATCH_PARENT
            height = WindowManager.LayoutParams.MATCH_PARENT
        }
        decorView.setPadding(0, 0, 0, 0)
        setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
    }
}

inline var TextView.maxLength: Int
    get() {
        return 1
    }
    set(value) {
        /**
         * 需要把已有的fitters规则一起添加上，否则就只剩下长度Filter了
         */
        val list = mutableListOf<InputFilter>().apply {
            filters.forEach {
                if(it !is InputFilter.LengthFilter){
                    add(it)
                }
            }
            add(InputFilter.LengthFilter(value))
        }
        filters = list.toTypedArray()
    }

@SuppressLint("RestrictedApi")
fun View.expandRound(left: Int = 0, top: Int = 0, right: Int = 0, bottom: Int = 0) {
    class MultiTouchDelegate(bound: Rect? = null, delegateView: View) : TouchDelegate(bound, delegateView) {
        val delegateViewMap = mutableMapOf<View, Rect>()
        private var delegateView: View? = null
//        var delegateParentViewMap = mutableMapOf<View, ArrayList<View>>()

        override fun onTouchEvent(event: MotionEvent): Boolean {
            val x = event.x.toInt()
            val y = event.y.toInt()
            var handled = false
            when (event.actionMasked) {
                MotionEvent.ACTION_DOWN -> {
                    delegateView = findDelegateViewUnder(x, y)
                }

                MotionEvent.ACTION_CANCEL -> {
                    delegateView = null
                }
            }
            delegateView?.let {
                event.setLocation(it.width / 2f, it.height / 2f)
                if (it.measuredHeight != 0 && it.measuredWidth != 0 && it.isVisible && it.hasOnClickListeners()) {
                    handled = it.dispatchTouchEvent(event)
                }
            }
            return handled
        }

        private fun findDelegateViewUnder(x: Int, y: Int): View? {
            delegateViewMap.forEach { entry -> if (entry.value.contains(x, y)) return entry.key }
            return null
        }
    }

    val delegateParentViewValue = ArrayList<View>()

    fun addTouchDelegate(view: View) {
        post {
            val parentView = view.parent as? ViewGroup
            parentView ?: return@post
            delegateParentViewValue.add(parentView)
            if (parentView.touchDelegate == null) parentView.touchDelegate = MultiTouchDelegate(delegateView = this).apply {
//                delegateParentViewMap[this@expandRound] = delegateParentViewValue
            }
            val rect = Rect()
            ViewGroupUtils.getDescendantRect(parentView, this, rect)
            rect.left += -left
            rect.top += -top
            rect.right -= -right
            rect.bottom -= -bottom
            (parentView.touchDelegate as? MultiTouchDelegate)?.delegateViewMap?.put(this, rect)

            val viewRect = Rect()
            parentView.getDrawingRect(viewRect)
            //不在父布局内，就再扩一层出去
            if (
                !viewRect.contains(rect.left, rect.top) ||
                !viewRect.contains(rect.left, rect.bottom) ||
                !viewRect.contains(rect.right, rect.top) ||
                !viewRect.contains(rect.right, rect.bottom)
            ) {
                addTouchDelegate(parentView)
            }
        }
    }

    addTouchDelegate(this)
}

@SuppressLint("RestrictedApi")
fun View.expand(dx: Int, dy: Int) {
    if (dx != 0 || dy != 0) {
        expandRound(dx, dy, dx, dy)
    }
}

@SuppressLint("RestrictedApi")
fun View.expandDp(dx: Int, dy: Int) {
    if (dx != 0 || dy != 0) {
        val expandX = dx.dp
        val expandY = dy.dp
        expandRound(expandX, expandY, expandX, expandY)
    }

}

