package com.adealink.weparty.playmate.list.comp

import androidx.lifecycle.LifecycleOwner
import com.adealink.frame.mvvm.view.ViewComponent
import com.adealink.frame.router.Router
import com.adealink.frame.util.onClick
import com.adealink.weparty.commonui.DEFAULT_FONT_BOLD
import com.adealink.weparty.commonui.DEFAULT_FONT_REGULAR
import com.adealink.weparty.commonui.ext.gone
import com.adealink.weparty.commonui.ext.hide
import com.adealink.weparty.commonui.ext.show
import com.adealink.weparty.module.category.Category
import com.adealink.weparty.module.category.data.CategoryAction
import com.adealink.weparty.module.playmate.data.PlaymateCategoryData
import com.adealink.weparty.playmate.R
import com.adealink.weparty.playmate.databinding.LayoutPlaymateActivityHomeListTopBinding
import com.adealink.weparty.playmate.databinding.LayoutPlaymateActivityHomeListTopItemBinding

class GuestActivityPlaymateTopCategoryComp(
    lifecycleOwner: LifecycleOwner,
    val topBinding: LayoutPlaymateActivityHomeListTopBinding,
    val category: PlaymateCategoryData?,
    val onCategorySelected: (category: PlaymateCategoryData) -> Unit
) : ViewComponent(lifecycleOwner) {

    private val categoryViewMap =
        mutableMapOf<PlaymateCategoryData, LayoutPlaymateActivityHomeListTopItemBinding>()

    override fun onCreate() {
        super.onCreate()
        initView()
        loadData()
    }

    private fun initView() {
        topBinding.clMore.onClick {
            clickMore()
        }
    }

    private fun loadData() {
        updateTopCategory(category?.subCategoryList)
    }

    fun selectCategory(category: PlaymateCategoryData?) {
        if (!isValid) {
            return
        }
        if (category == null) {
            for (itemBinding in categoryViewMap.values) {
                itemBinding.vBg.setBackgroundResource(R.drawable.playmate_activity_home_list_top_item_bg)
                itemBinding.tvItem.typeface = DEFAULT_FONT_REGULAR
            }
            return
        }
        for (entry in categoryViewMap) {
            if (entry.key == category) {
                entry.value.vBg.setBackgroundResource(R.drawable.playmate_activity_home_list_top_item_selected_bg)
                entry.value.tvItem.typeface = DEFAULT_FONT_BOLD
            } else {
                entry.value.vBg.setBackgroundResource(R.drawable.playmate_activity_home_list_top_item_bg)
                entry.value.tvItem.typeface = DEFAULT_FONT_REGULAR
            }
        }
    }

    private fun updateTopCategory(category: List<PlaymateCategoryData>?) {
        if (category.isNullOrEmpty()) {
            topBinding.root.gone()
            categoryViewMap.clear()
            return
        }
        topBinding.root.show()
        categoryViewMap.clear()

        val item2 = category.getOrNull(1)
        if (item2 == null) {
            topBinding.vItem2.root.hide()
            topBinding.vItem2.root.setOnClickListener(null)
        } else {
            categoryViewMap[item2] = topBinding.vItem2

            topBinding.vItem2.root.show()
            topBinding.vItem2.root.onClick {
                onCategorySelected.invoke(item2)
            }
            topBinding.vItem2.ivItem.setImageUrl(item2.icon)
            topBinding.vItem2.tvItem.text = item2.name
        }

        val item1 = category.getOrNull(0)
        if (item1 == null) {
            topBinding.vItem1.root.hide()
            topBinding.vItem1.root.setOnClickListener(null)
        } else {
            categoryViewMap[item1] = topBinding.vItem1

            topBinding.vItem1.root.show()
            topBinding.vItem1.root.onClick {
                onCategorySelected.invoke(item1)
            }
            topBinding.vItem1.ivItem.setImageUrl(item1.icon)
            topBinding.vItem1.tvItem.text = item1.name
        }
    }

    private fun clickMore() {
        val act = activity ?: return
        Router.build(act, Category.CategoryList.PATH)
            .putExtra(
                Category.CategoryList.EXTRA_SELECT_ACTION,
                CategoryAction.FIND_PLAYMATE.action
            )
            .start()
    }
}