package com.gh.gamecenter.common.view

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.gh.gamecenter.common.R
import com.gh.gamecenter.common.databinding.ItemLineAxisFilterBinding
import com.gh.gamecenter.common.databinding.ViewLineAxisFilterBinding
import com.gh.gamecenter.common.utils.dip2px
import com.gh.gamecenter.common.utils.toBinding
import com.gh.gamecenter.common.utils.toColor
import com.gh.gamecenter.common.view.divider.VerticalDividerItemDecoration
import com.gh.gamecenter.core.utils.DisplayUtils

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

    private var mBinding: ViewLineAxisFilterBinding? = null
    private var mSelectedFilter: String = ""
    private var mOnClickCallback: ((String) -> Unit)? = null

    init {
        val view = LayoutInflater.from(context).inflate(R.layout.view_line_axis_filter, this, true)
        mBinding = ViewLineAxisFilterBinding.bind(view)
    }

    fun setupFilter(
        filterList: ArrayList<String>,
        defaultSelectedFilter: String?,
        onClickCallback: ((String) -> Unit)?
    ) {
        if (filterList.isEmpty()) return
        mSelectedFilter = defaultSelectedFilter ?: filterList[0]
        mOnClickCallback = onClickCallback
        mBinding?.filterRv?.run {
            isNestedScrollingEnabled = false
            layoutManager = LinearLayoutManager(context, RecyclerView.HORIZONTAL, false)
            adapter = FilterAdapter(filterList)
            val dividerWidth = (DisplayUtils.getScreenWidth() - 56F.dip2px() * filterList.size) / (filterList.size + 1)
            val itemDecoration = VerticalDividerItemDecoration.Builder(context)
                .size(dividerWidth)
                .color(R.color.transparent.toColor(context))
                .build()
            addItemDecoration(itemDecoration)
        }
    }

    fun updateSelectedFilter(filter: String) {
        if (mSelectedFilter != filter) {
            mSelectedFilter = filter
            mBinding?.filterRv?.adapter?.notifyDataSetChanged()
        }
    }

    fun updateFilterRecyclerView() {
        mBinding?.filterRv?.recycledViewPool?.clear()
        mBinding?.filterRv?.adapter?.run {
            notifyItemRangeChanged(0, itemCount)
        }
    }

    inner class FilterAdapter(private val mFilterList: ArrayList<String>) :
        RecyclerView.Adapter<FilterViewHolder>() {

        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): FilterViewHolder {
            return FilterViewHolder(parent.toBinding())
        }

        override fun getItemCount(): Int = mFilterList.size

        override fun onBindViewHolder(holder: FilterViewHolder, position: Int) {
            val filterText = mFilterList[position]
            holder.binding.run {
                filterName.text = filterText
                filterName.setTextColor(ContextCompat.getColorStateList(context, R.color.line_axis_text_selector))
                root.isChecked = mSelectedFilter == filterText
                (dotView.layoutParams as MarginLayoutParams).apply {
                    setMargins(0, 0, 0, if (mSelectedFilter == filterText) 1F.dip2px() else 4F.dip2px())
                    dotView.layoutParams = this
                }
                root.setOnClickListener {
                    if (mSelectedFilter != filterText) {
                        mSelectedFilter = filterText
                        mOnClickCallback?.invoke(mSelectedFilter)
                        notifyDataSetChanged()
                    }
                }
            }
        }

    }

    class FilterViewHolder(var binding: ItemLineAxisFilterBinding) : RecyclerView.ViewHolder(binding.root)
}