package com.gh.gamecenter.forum.home.follow

import android.content.Context
import android.content.res.ColorStateList
import android.view.LayoutInflater
import android.view.ViewGroup.LayoutParams
import android.view.ViewGroup.MarginLayoutParams
import android.widget.CheckedTextView
import android.widget.PopupWindow
import androidx.core.view.children
import com.gh.gamecenter.R
import com.gh.gamecenter.common.utils.*
import com.gh.gamecenter.core.utils.DisplayUtils
import com.gh.gamecenter.databinding.LayoutForumActivityCategoryItemBinding
import com.gh.gamecenter.databinding.PopFollowFilterBinding
import com.google.android.flexbox.FlexboxLayout

class FollowHomeFilterPopWindow(
    private val binding: PopFollowFilterBinding
) : PopupWindow(binding.root, LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT) {

    private val data = arrayListOf("全部", "关注的人", "关注的游戏")

    init {
        isOutsideTouchable = true
        isFocusable = true

        val layoutParams = binding.ivTopBackground.layoutParams as MarginLayoutParams
        layoutParams.topMargin = -52F.dip2px() - DisplayUtils.getStatusBarHeight(binding.root.context.resources)
        binding.ivTopBackground.layoutParams = layoutParams

        val inflater = LayoutInflater.from(binding.root.context)
        data.forEachIndexed { index, entity ->
            LayoutForumActivityCategoryItemBinding.inflate(inflater).apply {
                val params =
                    FlexboxLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)
                params.setMargins(0, 8F.dip2px(), 8F.dip2px(), 0)
                params.height = 24F.dip2px()
                root.layoutParams = params

                name.text = entity
                name.isChecked = index == 0
                root.setOnClickListener {
                    updateCategory(index)
                }
                binding.fblFilter.addView(root)
            }
        }
        binding.vBackground.setOnClickListener {
            dismiss()
        }

        if (DarkModeUtils.isDarkModeOn(binding.root.context)) {
            binding.clFilterContainer.setBackgroundColor(com.gh.gamecenter.common.R.color.ui_background.toColor(binding.root.context))
            binding.ivTopBackground.goneIf(true)
        } else {
            binding.clFilterContainer.background = null
            binding.ivTopBackground.goneIf(false)
        }
    }

    private fun updateCategory(index: Int) {
        filteredListener?.filterFollowed(index)
        binding.fblFilter.children.forEachIndexed { position, child ->
            (child as? CheckedTextView)?.isChecked = (position == index)
        }
        dismiss()
    }

    private var filteredListener: OnFilteredChangedListener? = null
    fun setFilterListener(filteredListener: OnFilteredChangedListener) {
        this.filteredListener = filteredListener
    }

    fun onDarkModeChanged() {
        if (DarkModeUtils.isDarkModeOn(binding.root.context)) {
            binding.clFilterContainer.setBackgroundColor(com.gh.gamecenter.common.R.color.ui_background.toColor(binding.root.context))
            binding.ivTopBackground.goneIf(true)
        } else {
            binding.clFilterContainer.background = null
            binding.ivTopBackground.goneIf(false)
        }
        val context = binding.root.context
        binding.fblFilter.children.forEach {
            if (it is CheckedTextView) {
                val colorInt = R.color.forum_category_selector.toColor(context)
                it.setTextColor(ColorStateList.valueOf(colorInt))
                val drawable = R.drawable.selector_bg_forum_activity_category.toDrawable(context)
                it.background = drawable
            }
        }
    }

    companion object {

        fun create(context: Context): FollowHomeFilterPopWindow {
            val inflater = LayoutInflater.from(context)
            val binding = PopFollowFilterBinding.inflate(inflater, null, false)
            return FollowHomeFilterPopWindow(binding)
        }
    }

    interface OnFilteredChangedListener {

        fun filterFollowed(position: Int)
    }
}