package com.ux.video.file.filerecovery.photo

import android.content.Context
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.PopupWindow
import android.widget.RelativeLayout
import androidx.core.view.forEach
import com.ux.video.file.filerecovery.databinding.CommonLayoutFilterItemBinding
import com.ux.video.file.filerecovery.databinding.PopwindowsFilterBinding
import com.ux.video.file.filerecovery.utils.Common
import com.ux.video.file.filerecovery.utils.Common.setItemSelect
import com.ux.video.file.filerecovery.utils.CustomTextView

class FilterPopupWindows(
    context: Context, list: Array<String>,
    var selectedPos: Int,
    onClickConfirm: (value: String) -> Unit,
    var onClickDismiss: () -> Unit
) :
    PopupWindow(context) {

    private val viewBinding: PopwindowsFilterBinding =
        PopwindowsFilterBinding.inflate(LayoutInflater.from(context))

    init {

        setContentView(viewBinding.root)


        width = ViewGroup.LayoutParams.MATCH_PARENT
        height = ViewGroup.LayoutParams.WRAP_CONTENT

        isFocusable = true
        isOutsideTouchable = true
        setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))

        // 动画（可选）
//        animationStyle = R.style.PopupAnimation

        viewBinding.viewMask.setOnClickListener {
            dismiss()
        }

        setData(list, selectedPos, onClickConfirm)
        Common.showLog("--------------FilterPopupWindows init")
    }

    fun updateSelectPos(index: Int) {
        selectedPos = index
        setPosSelect(selectedPos)
    }

    private fun setData(
        list: Array<String>,
        selectedPos: Int,
        onClickConfirm: (value: String) -> Unit
    ) {
        viewBinding.parentLayout.removeAllViews()
        list.forEachIndexed { index, item ->
            viewBinding.parentLayout.run {
                addView(
                    CommonLayoutFilterItemBinding.inflate(
                        LayoutInflater.from(context),
                        this,
                        false
                    ).apply {
                        textItem.text = item
                        if (index == selectedPos) {
                            textItem.isSelected = true
                            checkImage.isSelected = true
                        }
                    }.root
                )
            }

        }
        setMutualExclusion(onClickConfirm)

    }


    private fun setMutualExclusion(onClickConfirm: (value: String) -> Unit) {
        viewBinding.run {
            for (i in 0 until parentLayout.childCount) {
                val child = parentLayout.getChildAt(i)
                if (child is RelativeLayout) {
                    child.setOnClickListener {
                        //回调选中的文字
                        for (j in 0 until child.childCount) {
                            val other = child.getChildAt(j)
                            if (other is CustomTextView) {
                                other.text.toString().let {
                                    setPosSelect(i)
                                    dismiss()
                                }
                                onClickConfirm.invoke(other.text.toString())
                            }
                        }

                    }
                }
            }
        }

    }

    private fun setPosSelect(selected: Int) {
        viewBinding.run {
            for (i in 0 until parentLayout.childCount) {
                val child = parentLayout.getChildAt(i)
                if (child is RelativeLayout) {
                    if (i == selected) {
                        setItemSelect(child, true)
                    } else {
                        setItemSelect(child, false)
                    }
                }
            }
        }
    }


    fun show(anchor: View, xOff: Int = 0, yOff: Int = 0) {
        showAsDropDown(anchor, xOff, yOff)
        viewBinding.viewMask.apply {
            alpha = 0f
            animate().alpha(1f).setDuration(200).start()
            visibility = View.VISIBLE
        }
    }

    override fun dismiss() {
        Log.d("------------", "--------------dismiss")
        viewBinding.viewMask.animate().alpha(0f).setDuration(200)
            .withEndAction {
                super.dismiss()
                onClickDismiss.invoke()
            }.start()
    }


}