package com.gh.gamecenter.category

import android.content.Context
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import androidx.recyclerview.widget.RecyclerView
import com.gh.common.view.SubCategoryView
import com.gh.gamecenter.R
import com.gh.gamecenter.common.baselist.ListAdapter
import com.gh.gamecenter.category.CategoryListActivity.Companion.getIntent
import com.gh.gamecenter.common.utils.ImageUtils
import com.gh.gamecenter.common.utils.toBinding
import com.gh.gamecenter.common.utils.toColor
import com.gh.gamecenter.core.utils.DisplayUtils
import com.gh.gamecenter.databinding.ItemCategoryBinding
import com.gh.gamecenter.entity.CategoryEntity
import net.cachapa.expandablelayout.ExpandableLayout

class CategoryDirectoryAdapter(context: Context, var categoryTitle: String) : ListAdapter<CategoryEntity>(context) {

    var recyclerView: RecyclerView? = null
    var expandStatusMap: HashMap<Int, Boolean> = HashMap()

    private var sixteenDp: Int = 0

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        if (sixteenDp == 0) sixteenDp = DisplayUtils.dip2px(parent.context, 16f)
        return CategoryViewHolder(parent.toBinding())
    }

    override fun getItemCount(): Int {
        return mEntityList.size
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        when (holder) {
            is CategoryViewHolder -> {
                val category = mEntityList[position]
                holder.binding.run {
                    divider.setBackgroundColor(R.color.background.toColor(mContext))
                    containerPrimaryCategory.setOnClickListener {
                        root.context.startActivity(getIntent(root.context, categoryTitle, category, "全部"))
                    }
                    ImageUtils.display(iconIv, category.icon)
                    categoryName.text = category.name
                }
                holder.bindCategory(
                    category = mEntityList[position],
                    expandableStatusMap = expandStatusMap,
                    isExpended = expandStatusMap[position] != null && expandStatusMap[position] == true,
                    marginTop = sixteenDp,
                    categoryTitle = categoryTitle,
                    expandedAction = { recyclerView?.smoothScrollToPosition(position) })
            }
        }
    }

    internal class CategoryViewHolder(var binding: ItemCategoryBinding) : RecyclerView.ViewHolder(binding.root) {
        fun bindCategory(
            category: CategoryEntity,
            expandableStatusMap: HashMap<Int, Boolean>,
            isExpended: Boolean,
            marginTop: Int,
            categoryTitle: String,
            expandedAction: () -> Unit
        ) {
            category.data?.let {
                var subCategoryView: SubCategoryView? = null

                binding.containerUnexpandable.removeAllViews()
                val unexpandableSize = if (it.size > 6) 6 else it.size
                val unexpandableCategoryList = it.subList(0, unexpandableSize)
                unexpandableCategoryList.forEachIndexed { index, c ->
                    when (index % 3) {
                        0 -> {
                            val params = LinearLayout.LayoutParams(
                                LinearLayout.LayoutParams.MATCH_PARENT,
                                LinearLayout.LayoutParams.MATCH_PARENT
                            )
                            if (subCategoryView != null) params.setMargins(0, marginTop, 0, 0)

                            subCategoryView = SubCategoryView(binding.root.context)

                            subCategoryView?.categoryTitle = categoryTitle
                            subCategoryView?.primeCategory = category
                            subCategoryView?.layoutParams = params

                            binding.containerUnexpandable.addView(subCategoryView)
                            subCategoryView?.setLeftCategory(c)
                        }
                        1 -> subCategoryView?.setCenterCategory(c)
                        2 -> subCategoryView?.setRightCategory(c)
                    }
                }

                if (it.size >= 7) {
                    val extraCategoryList = it.subList(6, it.size)

                    binding.ivToggle.visibility = View.VISIBLE
                    binding.ivToggle.setOnClickListener { binding.containerExpandable.toggle() }
                    when {
                        isExpended -> binding.ivToggle.setImageResource(R.drawable.ic_category_arrow_up)
                        else -> binding.ivToggle.setImageResource(R.drawable.ic_category_arrow_down)
                    }

                    val subCategoryViewLinearLayout = LinearLayout(binding.root.context)
                    subCategoryViewLinearLayout.orientation = LinearLayout.VERTICAL
                    subCategoryView = SubCategoryView(binding.root.context)

                    binding.containerExpandable.removeAllViews()
                    binding.containerExpandable.addView(subCategoryViewLinearLayout)
                    binding.containerExpandable.setExpanded(isExpended, false)
                    binding.containerExpandable.setOnExpansionUpdateListener { _, state ->
                        when (state) {
                            ExpandableLayout.State.COLLAPSED -> {
                                binding.ivToggle.setImageResource(R.drawable.ic_category_arrow_down)
                                expandableStatusMap[adapterPosition] = false
                            }
                            ExpandableLayout.State.EXPANDED -> {
                                binding.ivToggle.setImageResource(R.drawable.ic_category_arrow_up)
                                expandableStatusMap[adapterPosition] = true
                                expandedAction.invoke()
                            }
                        }
                    }

                    extraCategoryList.forEachIndexed { index, c ->
                        when (index % 3) {
                            0 -> {
                                subCategoryView = SubCategoryView(binding.root.context)

                                val params = LinearLayout.LayoutParams(
                                    LinearLayout.LayoutParams.MATCH_PARENT,
                                    LinearLayout.LayoutParams.MATCH_PARENT
                                )
                                params.setMargins(0, marginTop, 0, 0)

                                subCategoryView?.categoryTitle = categoryTitle
                                subCategoryView?.primeCategory = category
                                subCategoryView?.layoutParams = params

                                subCategoryViewLinearLayout.addView(subCategoryView)
                                subCategoryView?.setLeftCategory(c)
                            }
                            1 -> subCategoryView?.setCenterCategory(c)
                            2 -> subCategoryView?.setRightCategory(c)
                        }
                    }
                } else {
                    binding.ivToggle.visibility = View.GONE
                    binding.containerExpandable.removeAllViews()
                }
            }
        }
    }
}