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.isVisible
import com.ux.video.file.filerecovery.R
import com.ux.video.file.filerecovery.databinding.PopwindowsFilterDateBinding
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
import java.util.Date

/**
 * 日期筛选弹窗
 */
class DateFilterPopupWindows(
    context: Context,
    var selectedPos: Int,
    var onClickConfirm: (value: String, showDateDialog: Boolean) -> Unit,
    var onResetDate: (isStart: Boolean,currentDisplayDate: Date?) -> Unit,
    var onClickDismiss: () -> Unit
) :
    PopupWindow(context) {

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

    private lateinit var startDate: Date
    private lateinit var endDate: Date

    init {

        setContentView(viewBinding.root)


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

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

        viewBinding.viewMask.setOnClickListener {
            dismiss()
        }
        viewBinding.run {
            tvStartDate.setOnClickListener { onResetDate(true, Common.getDateFromFormat(tvStartDate.text.toString())) }
            tvEndDate.setOnClickListener { onResetDate(false,Common.getDateFromFormat(tvEndDate.text.toString())) }
        }

        setData(context.resources.getStringArray(R.array.filter_date), selectedPos)
        Common.showLog("--------------FilterPopupWindows init")
    }

    fun updateSelectPos(index: Int, start: Date, end: Date) {
        selectedPos = index
        setPosSelect(selectedPos)
        if (index == 4) {
            startDate = start
            endDate = end
            viewBinding.run {
                layoutStartEnd.isVisible = true
                tvStartDate.text = Common.getChineseFormatDate(start)
                tvEndDate.text = Common.getChineseFormatDate(end)
                Common.setItemSelect(layoutStartEnd, true)
            }
        }
    }

    fun updateStartEndDate(start: Date? = null, end: Date? = null) {
        start?.let { viewBinding.tvStartDate.text = Common.getChineseFormatDate(it) }
        end?.let { viewBinding.tvEndDate.text = Common.getChineseFormatDate(it) }
    }

    private fun setData(
        list: Array<String>,
        selectedPos: Int,
    ) {

        list.forEachIndexed { index, item ->
            viewBinding.run {
                when (index) {
                    0 -> tvAll.text = item
                    1 -> tv1.text = item
                    2 -> tv6.text = item
                    3 -> tv24.text = item
                    4 -> tvCustomize.text = item
                }
            }

        }
        setMutualExclusion()
        setPosSelect(selectedPos)

    }


    private fun setMutualExclusion() {
        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 {
                                    if (it == "Customize" && layoutStartEnd.isVisible == false) {
                                        //自定义日期
                                        onClickConfirm.invoke(it, true)
                                    } else {
                                        setPosSelect(i)
                                        dismiss()
                                        onClickConfirm.invoke(it, false)
                                    }
                                }
                            }
                        }

                    }
                }
            }
        }

    }

    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()
    }


}